106 Commits

Author SHA1 Message Date
Philip Meulengracht
2dec53312a strutil: replace manually trim logic with stdlib
review feedback
2022-03-31 12:04:05 +02:00
Philip Meulengracht
fad3dd0ea3 cmd/snap,strutil: move lineWrap to WordWrapPadded
moved to strutil as I need it for snapctl model
2022-03-31 11:00:48 +02:00
Philip Meulengracht
fc051378e1 cmd/snap,strutil: move unit test for WordWrap 2022-03-28 13:33:49 +02:00
Philip Meulengracht
a40188c56e strutil: move WordWrap function to strutil
will need this function for the model command in snapctl
2022-03-25 14:18:59 +01:00
Philip Meulengracht
0d1c1f24d5 multiple: review feedback
Always write CPUAccounting, improve usage of fmt and bytes.Buffer, improve docs, fix typos
2022-03-16 19:29:58 +01:00
Philip Meulengracht
b1d3d60f7a many: review feedback
update error messages for resource validation, add missing test for changing limits inside quota.go
2022-03-14 10:11:36 +01:00
Philip Meulengracht
1ce22daaf1 multiple: first part of review feedback
renamings, better docs
2022-02-28 16:55:15 +01:00
Philip Meulengracht
6f92ff61b3 strutil: add unit tests for SliceToCommaSeparatedString 2022-02-25 12:43:26 +01:00
Philip Meulengracht
be20d29a81 wrappers: review feedback
add new function []int => string in strutils, use that instead of chained string functions
2022-02-24 14:19:42 +01:00
Miguel Pires
615da7771d many: fix failing golangci checks
* Group 3rd party imports
* Remove unused vars
* Move var only used in tests into test
* Add 'nolint' to ignore warning on unused iotas (might've been
left to improve readability).
2022-02-09 10:12:09 +00:00
Miguel Pires
13209fc52c many: fix formatting w/ latest go version
Fix formatting when running the latest version (1.17) of gofmt.
2021-12-08 13:59:56 +00:00
Miguel Pires
6795a00dc6 strutil: improve test case 2021-10-21 11:01:02 +01:00
Miguel Pires
0e16c1040c o/snapstate: deduplicate snap names in install, remove and update 2021-10-20 15:48:08 +01:00
Maciej Borzecki
41bf1a3597 many: pick up special build time flags for invoking go while generating artifacts
Some packaging frameworks (dh-golang in partcular) invokes go generate. Our code
calls `go run ..` to generate some output files. In particular, when building
with Go modules support, we must pass -mod=vendor occasionally. Allow the build
code to set an environment variables with flags that shall be passed to go run.

Signed-off-by: Maciej Borzecki <maciej.zenon.borzecki@canonical.com>
2021-09-09 13:37:31 +02:00
Ian Johnson
3c9708c927 strutil/intersection.go: apply optimization from Miguel
Thanks to Miguel for the suggestion

Signed-off-by: Ian Johnson <ian.johnson@canonical.com>
2021-09-03 11:50:36 -05:00
Ian Johnson
46647a836f strutil: add Intersection()
This treats input slices as sets and computes the intersection of those sets.

We need this for cloud-init where we have to take a 3-way intersection of slice
lists.

Signed-off-by: Ian Johnson <ian.johnson@canonical.com>
2021-09-03 10:37:26 -05:00
Miguel Pires
dd9c0856ea cmd/snap: add Size to refresh --list 2021-08-17 09:37:39 +01:00
Samuele Pedroni
bcbf8edafd osutil,strutil,testutil: fix imports order (according to gci) 2021-06-15 18:04:44 +02:00
Maciej Borzecki
8f9d5ca13e strutil: refactor match counter to keep N last matches
A little refactor of match counter to allow it to keep N last matches. Useful
when we want to collect the few last lines of a failed command output, which
likely contain the error cause.

Signed-off-by: Maciej Borzecki <maciej.zenon.borzecki@canonical.com>
2021-02-24 12:27:21 +01:00
Ian Johnson
2e2b3aaf7f osutil: add KernelCommandLineKeyValue; boot: refactor ModeAnd...FromKernelCommandLine (#9659)
* boot/cmdline.go: add TODO about using strutil.KernelCommandLineSplit

Signed-off-by: Ian Johnson <ian.johnson@canonical.com>

* strutil/cmdline.go: add GetKernelCommandLineKeyValue

This complements KernelCommandLineSplit, but goes further, checking for a
specific key-value pair in the kernel command line parameters, and returning the
value if found. This will be useful across the codebase for places where we want
to check one specific kernel command line parameter key-value pair.

Signed-off-by: Ian Johnson <ian.johnson@canonical.com>

* logger/logger.go: use GetKernelCommandLineKeyValue directly

This is a bit more straight forward to read IMHO and potentially reduces some
looping over the parameters as we break as soon as we find snapd.debug in the
positive case where it is set.

Signed-off-by: Ian Johnson <ian.johnson@canonical.com>

* strutil,osutil: move kernel commandline helpers to osutil

* boot: tweak TODO now that we use osutil.KernelCommandLineSplit

* many: refactor ModeAnd...FromKernelCommandLine and KernelCommandLineKeyValue

Refactor GetKernelCommandLineKeyValue to KernelCommandLineKeyValue which
returns a map of the specified keys that were found on the kernel command line
that have values. It also no longer takes the command line string as an
argument and instead parses the command line itself.

The above necessitates moving the mocking function for where to find
/proc/cmdline to osutil as well and adjusting many tests for this.

Finally, with all of this in place we can refactor
boot.ModeAndRecoverySystemFromKernelCommandLine to use the osutil helpers
and not duplicate parsing logic in the boot package. This does result in
a slight change in behavior where now duplicated kernel command line
parameters are not a fatal condition, but instead the last definition is
used.

Also adjust some tests to mock an empty proc/cmdline to avoid using the
host's version when running tests.

Signed-off-by: Ian Johnson <ian.johnson@canonical.com>

* osutil, many: rename to KernelCommandLineKeyValues

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

* boot: disallow non empty system label without a mode

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

* osutil: tweak handling of cmdline keys

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

* osutil: mv cmdline to kcmdline

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

* osutil: comment tweak

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

* logger: further tweaks

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

Co-authored-by: Michael Vogt <mvo@ubuntu.com>
Co-authored-by: Maciej Borzecki <maciej.zenon.borzecki@canonical.com>
2020-11-19 15:35:31 +01:00
Ian Johnson
f17ae685c5 strutil/shlex,osutil/udev/netlink: minimally import go-check
This allows us to use -gocheck options for the whole codebase, for example to
run all tests in the codebase with the name matching "kernel" one can now do:

go test ./... -gocheck.f kernel

and that will only fail if the tests fail, whereas previously it would fail
because in these packages gocheck was not imported and as such -gocheck.f was
not recognized as an option and the help text for `go test` was printed off
instead of running any tests.

Eventually we may want to adapt the tests in these packages to use go-check too
so that these can also be filtered/run successfully with go test and gocheck
options.

Signed-off-by: Ian Johnson <ian.johnson@canonical.com>
2020-11-16 15:29:02 -06:00
Santiago Bautista
3c95be0fc5 fix some typos 2020-09-27 23:38:17 +02:00
Samuele Pedroni
ee9a1b5842 strutil: add SortedListsUniqueMerge 2020-09-10 21:53:02 +02:00
Maciej Borzecki
02960ad453 strutil: drop repeated test case in kernel command line split unit tests
Signed-off-by: Maciej Borzecki <maciej.zenon.borzecki@canonical.com>
2020-07-13 12:40:46 +02:00
Maciej Borzecki
f2d898331f strutil: improve kernel command line splitting with realistic test cases
Signed-off-by: Maciej Borzecki <maciej.zenon.borzecki@canonical.com>
2020-07-12 07:59:01 +02:00