55 Commits

Author SHA1 Message Date
Miguel Pires
d097436c1c many: fix formatting w/ gofmt 1.19
Go 1.19 includes some changes to gofmt which intend to make lists and
heading clearer when rendered (https://go.dev/doc/go1.19). This commit
is the result of running the new gofmt and manually fixing some of it.
This was necessary because the new gofmt assumed lines beginning w/ tabs
to start lists or examples. While this is often true in our codebase,
we occasionally also use tabs to indent the lines after a TODO or FIXME
prefix or in yaml (e.g., excerpts of a snap.yaml). This meant that a lot of the
reformatted comments were broken and had to be fixed manually.

Signed-off-by: Miguel Pires <miguel.pires@canonical.com>
2023-01-16 14:23:11 +01:00
Samuele Pedroni
f83b1f1560 many: progress bars should use the overridable stdouts (#12047)
making progress bars should take a stdout io.Writer

ToolingStore should carry an overridable stdout

make sure in image, preseed and cmd/snap code the overridable
top-level Stdout is passed along to make progress bars
2022-08-18 11:34:35 +02:00
Michael Vogt
53d63d65bd many: run gofmt to match go-1.13 2021-06-24 11:42:13 +02:00
Maciej Borzecki
d127b295c1 many: use the helper for checking current binary is a test one
Signed-off-by: Maciej Borzecki <maciej.zenon.borzecki@canonical.com>
2020-07-17 17:55:35 +02:00
Ian Johnson
588875e19f many: use more specific check for unit test mocking
Currently, if you try to use a snap with an app named "test", os.Args will also
end in ".test", making numerous places in the codebase think that it is being
run in a test environment and behave differently. In the case of the reported
bug, the code panics from `snap run` as it seems to the mountinfo code that we
are not mocking something that we should always be mocking in unit tests.

The bug could still hypothetically happen if someone made a symlink from 
somewhere with "go-build" in the path (but not as part of the app name in the
snap) and a .test suffix on the executable, but this is sufficiently specific
that it is highly unlikely we would ever actually run into that problem "in the
wild".

Fixes: https://bugs.launchpad.net/snapd/+bug/1886786

Signed-off-by: Ian Johnson <ian.johnson@canonical.com>
2020-07-08 06:55:51 -05:00
Maciej Borzecki
4228612f25 progress: tweak multibyte label unit test data (#8616)
* progress: tweak multibyte label unit test data

Since the progress bar calculates speed based on actual time, any
flakyness/delay in test execution may influence the reported value. Account for
that in the regex.

Signed-off-by: Maciej Borzecki <maciej.zenon.borzecki@canonical.com>

* progress: drop unused field in unit tests

Signed-off-by: Maciej Borzecki <maciej.zenon.borzecki@canonical.com>

* progress: one more format tweak

Signed-off-by: Maciej Borzecki <maciej.zenon.borzecki@canonical.com>
2020-05-07 21:55:34 +02:00
Maciej Borzecki
476810b31c progress: fix progress bar with multibyte duration units
Some languages may use multibyte characters for duration. In such case, the
width of the progress bar would be incorrectly calculated, making the displayed
label too short.

At runtime this would randomly cause a panic with the translation used a
multibyte characters:

panic: runtime error: slice bounds out of range [80:79] [recovered]
 panic: runtime error: slice bounds out of range [80:79]

goroutine 1 [running]:
main.main.func1()
 github.com/snapcore/snapd/cmd/snap/main.go:477 +0x95
panic(0x561d5f7fac80, 0xc000144280)
 runtime/panic.go:679 +0x1b6
github.com/snapcore/snapd/progress.(*ANSIMeter).Set(0xc000214eb0, 0x41a891a000000000)
 github.com/snapcore/snapd/progress/ansimeter.go:152 +0xa1b
main.waitMixin.wait(0xc0002e42a0, 0x561d5f3e0000, 0xc0001e4488, 0x2, 0x0, 0x0, 0x0)
 github.com/snapcore/snapd/cmd/snap/wait.go:130 +0x700
main.(*cmdInstall).installOne(0xc000359040, 0x7ffcf44c33e4, 0xb, 0x0, 0x0, 0xc00036cba0, 0x561d5f805060, 0x561d5f8693a0)
 github.com/snapcore/snapd/cmd/snap/cmd_snap_op.go:491 +0x316
main.(*cmdInstall).Execute(0xc000359040, 0xc0003787a0, 0x0, 0x2, 0xc000359040, 0x1)
 github.com/snapcore/snapd/cmd/snap/cmd_snap_op.go:596 +0x3d7
github.com/snapcore/snapd/vendor/github.com/jessevdk/go-flags.(*Parser).ParseArgs(0xc000316bd0, 0xc000032190, 0x2, 0x2, 0xc000330330, 0xc0002a5cd0, 0x561d5ee49fbd, 0x561d5f7d08a0, 0xc000330330)
 github.com/snapcore/snapd/vendor/github.com/jessevdk/go-flags/parser.go:333 +0x8e7
github.com/snapcore/snapd/vendor/github.com/jessevdk/go-flags.(*Parser).Parse(...)
 github.com/snapcore/snapd/vendor/github.com/jessevdk/go-flags/parser.go:190
main.run(0xc0002a5de0, 0xe)
 github.com/snapcore/snapd/cmd/snap/main.go:515 +0xa7
main.main()
 github.com/snapcore/snapd/cmd/snap/main.go:482 +0x371

Since we are assuming that terminal can display up to the column number of
runes, fix the length calculation to use slices of runes for all
components (keep in mind that len(string) >= len([]rune)).

Fixes: https://bugs.launchpad.net/ubuntu/+source/snapd/+bug/1876583

Signed-off-by: Maciej Borzecki <maciej.zenon.borzecki@canonical.com>
2020-05-06 13:34:21 +02:00
John R. Lenton
fbf35a65dc progress: tweak ansimeter cvvis use to no longer confuse minicom
minicom's vt220 implementation was improperly displaying half of
cvvis. It seems it can't handle CPI codes separated by ;, and fails to
ignore the unknown escape.

This simplifies it, but it still does what we want (the cvvis we were
using, taken from terminfo for xterm, was additionally doing an 'echo
on', which we don't need).

Additionally I received feedback about the spinner being nastier than
the old slash-based one, so I moved to that.

... some day I might make that customisable :-)
2018-03-06 14:00:41 +01:00
John R. Lenton
f975cc3fa9 strutil/quantity: new package that exports formatFoo (from progress)
Package progress had some useful formatting functions (originally
taken from github.com/chipaca/quantity), that are more generally
useful. Circular imports mean they can't be in strutil directly
(quantity uses i18n which uses strutil), but strutil/quantity seems
fine to me.
2018-01-28 21:57:05 +00:00
John R. Lenton
96c2ed313d the linux vt does not like unicode :-( 2018-01-09 16:38:52 +00:00
John R. Lenton
f283898ee2 move spinner to opposite edge 2018-01-09 12:36:17 +00:00
John R. Lenton
5bc568771a progress: switch ansimeter's Spin() to use a spinner
People objected to ansimeter's use of a background throbber, and asked
for a textual spinner instead. I succumb to their wishes.
2018-01-09 12:22:28 +00:00
Michael Vogt
09606aeaa9 Merge pull request #4015 from chipaca/nakedret
run-checks: use nakedret to check for naked returns on long functions
2017-10-11 09:38:19 +02:00
John R. Lenton
3cfc7d3e46 progress: be more flexible in testing ansimeter 2017-10-10 14:50:58 +01:00
John R. Lenton
190c7c9fd9 run-checks: use nakedret to check for naked returns on long functions 2017-10-10 13:15:24 +01:00
John R. Lenton
06e83b2e0e add unit tests for ansimeter; some tweaks for quantity corner cases 2017-10-06 13:54:57 +01:00
John R. Lenton
2a1bd6886c address review feedback 2017-09-27 21:36:13 -04:00
John R. Lenton
dd149fd194 tweak the Notify line break algo 2017-09-26 19:58:21 -04:00
John R. Lenton
67a6df271f many: add copyright header 2017-09-26 07:02:11 -04:00
John R. Lenton
e67a2c0ddc many: implement our own ANSI-escape-using progress indicator
This introduces a `progress.Meter` written from "scratch" (mostly based on
previous work and experiments I did on IO progress bars and fixed-width
displays). It's called `progress.ANSIMeter`.

We were using a `progress.NullProgress` in situations where we actually didn't
want progress bars but still wanted notifications, so `progress.QuietMeter` is a
`progress.Meter` that does that, and that is returned by
`progress.MakeProgressBar()` when it doesn't think we have an actual terminal.

We were replacing `os.Stdout` to look at the output of progress bars, so I wrote
`progresstest.Meter` that just records what it's told instead of printing it
anywhere, and use it in some places.

I renamed `progress.NullProgress` to `progress.NullMeter`, and added
`progress.Null` as a default `progress.NullMeter`; as it's a `struct{}`, it
doesn't make sense not to use that (we were using `&progress.NullMeter{}` in
many many tests).

`image` was not calling `Finished()` on the progress bar, so I added code for it to
do that both in the sane case and in the case the user `^C`'s out.
2017-09-25 17:36:11 -04:00
Michael Hudson-Doyle
f033646f3d switch to canonical path for gopkg.in/cheggaaa/pb.v1
this will let me remove a particularly silly bit of the debian delta.
2017-08-15 13:32:26 +12:00
John R. Lenton
fc5e6d7112 build snapd without CGO 2016-11-23 22:49:52 +00:00
Michael Vogt
9139ec872d init pbar with correct total value 2016-11-22 08:56:56 +01:00
Leo Arias
f16ada711d static tests: add spell check 2016-11-16 22:31:16 +00:00
Michael Vogt
8d75a516a5 use New64 and fix output newline 2016-09-23 08:05:53 +02:00