From 5a65aadd8d2ec8aaa6830c21cdee0d1fe14c3341 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Sun, 27 Mar 2022 19:48:55 +0200 Subject: [PATCH 1/4] cli/command/container: unnecessary use of fmt.Sprintf (gosimple) cli/command/container/formatter_stats.go:184:10: S1039: unnecessary use of fmt.Sprintf (gosimple) return fmt.Sprintf("--") ^ cli/command/container/formatter_stats.go:191:10: S1039: unnecessary use of fmt.Sprintf (gosimple) return fmt.Sprintf("-- / --") ^ cli/command/container/formatter_stats.go:201:10: S1039: unnecessary use of fmt.Sprintf (gosimple) return fmt.Sprintf("--") ^ cli/command/container/formatter_stats.go:184:10: S1039: unnecessary use of fmt.Sprintf (gosimple) return fmt.Sprintf("--") ^ cli/command/container/formatter_stats.go:191:10: S1039: unnecessary use of fmt.Sprintf (gosimple) return fmt.Sprintf("-- / --") ^ cli/command/container/formatter_stats.go:201:10: S1039: unnecessary use of fmt.Sprintf (gosimple) return fmt.Sprintf("--") ^ Signed-off-by: Sebastiaan van Stijn --- cli/command/container/formatter_stats.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/cli/command/container/formatter_stats.go b/cli/command/container/formatter_stats.go index 80fdd586d6..4909405cf3 100644 --- a/cli/command/container/formatter_stats.go +++ b/cli/command/container/formatter_stats.go @@ -181,14 +181,14 @@ func (c *statsContext) ID() string { func (c *statsContext) CPUPerc() string { if c.s.IsInvalid { - return fmt.Sprintf("--") + return "--" } return fmt.Sprintf("%.2f%%", c.s.CPUPercentage) } func (c *statsContext) MemUsage() string { if c.s.IsInvalid { - return fmt.Sprintf("-- / --") + return "-- / --" } if c.os == winOSType { return units.BytesSize(c.s.Memory) @@ -198,28 +198,28 @@ func (c *statsContext) MemUsage() string { func (c *statsContext) MemPerc() string { if c.s.IsInvalid || c.os == winOSType { - return fmt.Sprintf("--") + return "--" } return fmt.Sprintf("%.2f%%", c.s.MemoryPercentage) } func (c *statsContext) NetIO() string { if c.s.IsInvalid { - return fmt.Sprintf("--") + return "--" } return fmt.Sprintf("%s / %s", units.HumanSizeWithPrecision(c.s.NetworkRx, 3), units.HumanSizeWithPrecision(c.s.NetworkTx, 3)) } func (c *statsContext) BlockIO() string { if c.s.IsInvalid { - return fmt.Sprintf("--") + return "--" } return fmt.Sprintf("%s / %s", units.HumanSizeWithPrecision(c.s.BlockRead, 3), units.HumanSizeWithPrecision(c.s.BlockWrite, 3)) } func (c *statsContext) PIDs() string { if c.s.IsInvalid || c.os == winOSType { - return fmt.Sprintf("--") + return "--" } return fmt.Sprintf("%d", c.s.PidsCurrent) } From d7c1fb9112c616ece53b9fcef1b136ac736bb021 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Sun, 27 Mar 2022 20:07:52 +0200 Subject: [PATCH 2/4] linting: ignore some "G101: Potential hardcoded credentials" warnings cli/config/credentials/native_store.go:10:2: G101: Potential hardcoded credentials (gosec) remoteCredentialsPrefix = "docker-credential-" ^ cli/command/service/opts.go:917:2: G101: Potential hardcoded credentials (gosec) flagCredentialSpec = "credential-spec" ^ Signed-off-by: Sebastiaan van Stijn --- cli/command/service/opts.go | 2 +- cli/config/credentials/native_store.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/cli/command/service/opts.go b/cli/command/service/opts.go index 020e9dd015..f7d7fcfa87 100644 --- a/cli/command/service/opts.go +++ b/cli/command/service/opts.go @@ -914,7 +914,7 @@ func addServiceFlags(flags *pflag.FlagSet, opts *serviceOptions, defaultFlagValu } const ( - flagCredentialSpec = "credential-spec" + flagCredentialSpec = "credential-spec" //nolint:gosec // ignore G101: Potential hardcoded credentials flagPlacementPref = "placement-pref" flagPlacementPrefAdd = "placement-pref-add" flagPlacementPrefRemove = "placement-pref-rm" diff --git a/cli/config/credentials/native_store.go b/cli/config/credentials/native_store.go index afe542cc3c..f9619b0381 100644 --- a/cli/config/credentials/native_store.go +++ b/cli/config/credentials/native_store.go @@ -7,7 +7,7 @@ import ( ) const ( - remoteCredentialsPrefix = "docker-credential-" + remoteCredentialsPrefix = "docker-credential-" //nolint:gosec // ignore G101: Potential hardcoded credentials tokenUsername = "" ) From 4ab70bf61e87271ca56511172352296a202ced41 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Sun, 27 Mar 2022 21:13:03 +0200 Subject: [PATCH 3/4] linting: fix incorrectly formatted errors (revive) cli/compose/interpolation/interpolation.go:102:4: error-strings: error strings should not be capitalized or end with punctuation or a newline (revive) "invalid interpolation format for %s: %#v. You may need to escape any $ with another $.", ^ cli/command/stack/loader/loader.go:30:30: error-strings: error strings should not be capitalized or end with punctuation or a newline (revive) return nil, errors.Errorf("Compose file contains unsupported options:\n\n%s\n", ^ cli/command/formatter/formatter.go:76:30: error-strings: error strings should not be capitalized or end with punctuation or a newline (revive) return tmpl, errors.Errorf("Template parsing error: %v\n", err) ^ cli/command/formatter/formatter.go:97:24: error-strings: error strings should not be capitalized or end with punctuation or a newline (revive) return errors.Errorf("Template parsing error: %v\n", err) ^ cli/command/image/build.go:257:25: error-strings: error strings should not be capitalized or end with punctuation or a newline (revive) return errors.Errorf("error checking context: '%s'.", err) ^ cli/command/volume/create.go:35:27: error-strings: error strings should not be capitalized or end with punctuation or a newline (revive) return errors.Errorf("Conflicting options: either specify --name or provide positional arg, not both\n") ^ cli/command/container/create.go:160:24: error-strings: error strings should not be capitalized or end with punctuation or a newline (revive) return errors.Errorf("failed to remove the CID file '%s': %s \n", cid.path, err) ^ Signed-off-by: Sebastiaan van Stijn --- cli/command/config/formatter_test.go | 6 ++---- cli/command/config/inspect_test.go | 2 +- cli/command/container/create.go | 8 ++++---- cli/command/container/create_test.go | 2 +- cli/command/container/formatter_stats_test.go | 6 ++---- cli/command/formatter/container_test.go | 6 ++---- cli/command/formatter/disk_usage_test.go | 6 ++---- cli/command/formatter/formatter.go | 4 ++-- cli/command/formatter/image_test.go | 6 ++---- cli/command/formatter/volume_test.go | 6 ++---- cli/command/image/build.go | 2 +- cli/command/inspect/inspector.go | 6 +++--- cli/command/inspect/inspector_test.go | 4 ++-- cli/command/network/formatter_test.go | 6 ++---- cli/command/node/formatter_test.go | 10 ++++------ cli/command/plugin/formatter_test.go | 6 ++---- cli/command/plugin/inspect_test.go | 2 +- cli/command/plugin/list_test.go | 2 +- cli/command/registry/formatter_search_test.go | 6 ++---- cli/command/secret/formatter_test.go | 6 ++---- cli/command/secret/inspect_test.go | 2 +- cli/command/service/formatter_test.go | 6 ++---- cli/command/stack/formatter/formatter_test.go | 6 ++---- cli/command/stack/list_test.go | 2 +- cli/command/stack/loader/loader.go | 1 + cli/command/stack/services_test.go | 2 +- cli/command/system/info.go | 2 +- cli/command/system/info_test.go | 2 +- cli/command/system/version.go | 2 +- cli/command/task/formatter_test.go | 6 ++---- cli/command/trust/formatter_test.go | 12 ++++-------- cli/command/volume/create.go | 2 +- cli/command/volume/create_test.go | 2 +- cli/command/volume/inspect_test.go | 2 +- cli/compose/interpolation/interpolation.go | 2 +- cli/compose/interpolation/interpolation_test.go | 2 +- 36 files changed, 62 insertions(+), 93 deletions(-) diff --git a/cli/command/config/formatter_test.go b/cli/command/config/formatter_test.go index 143aaa1686..c6ebcb4f25 100644 --- a/cli/command/config/formatter_test.go +++ b/cli/command/config/formatter_test.go @@ -19,13 +19,11 @@ func TestConfigContextFormatWrite(t *testing.T) { // Errors { formatter.Context{Format: "{{InvalidFunction}}"}, - `Template parsing error: template: :1: function "InvalidFunction" not defined -`, + `template parsing error: template: :1: function "InvalidFunction" not defined`, }, { formatter.Context{Format: "{{nil}}"}, - `Template parsing error: template: :1:2: executing "" at : nil is not a command -`, + `template parsing error: template: :1:2: executing "" at : nil is not a command`, }, // Table format {formatter.Context{Format: NewFormat("table", false)}, diff --git a/cli/command/config/inspect_test.go b/cli/command/config/inspect_test.go index d30c891a98..523eba2acd 100644 --- a/cli/command/config/inspect_test.go +++ b/cli/command/config/inspect_test.go @@ -36,7 +36,7 @@ func TestConfigInspectErrors(t *testing.T) { flags: map[string]string{ "format": "{{invalid format}}", }, - expectedError: "Template parsing error", + expectedError: "template parsing error", }, { args: []string{"foo", "bar"}, diff --git a/cli/command/container/create.go b/cli/command/container/create.go index 575b40db00..6abbb4a4a1 100644 --- a/cli/command/container/create.go +++ b/cli/command/container/create.go @@ -157,7 +157,7 @@ func (cid *cidFile) Close() error { return nil } if err := os.Remove(cid.path); err != nil { - return errors.Errorf("failed to remove the CID file '%s': %s \n", cid.path, err) + return errors.Wrapf(err, "failed to remove the CID file '%s'", cid.path) } return nil @@ -168,7 +168,7 @@ func (cid *cidFile) Write(id string) error { return nil } if _, err := cid.file.Write([]byte(id)); err != nil { - return errors.Errorf("Failed to write the container ID to the file: %s", err) + return errors.Wrap(err, "failed to write the container ID to the file") } cid.written = true return nil @@ -179,12 +179,12 @@ func newCIDFile(path string) (*cidFile, error) { return &cidFile{}, nil } if _, err := os.Stat(path); err == nil { - return nil, errors.Errorf("Container ID file found, make sure the other container isn't running or delete %s", path) + return nil, errors.Errorf("container ID file found, make sure the other container isn't running or delete %s", path) } f, err := os.Create(path) if err != nil { - return nil, errors.Errorf("Failed to create the container ID file: %s", err) + return nil, errors.Wrap(err, "failed to create the container ID file") } return &cidFile{path: path, file: f}, nil diff --git a/cli/command/container/create_test.go b/cli/command/container/create_test.go index dbe9e1f939..3634c194d2 100644 --- a/cli/command/container/create_test.go +++ b/cli/command/container/create_test.go @@ -38,7 +38,7 @@ func TestNewCIDFileWhenFileAlreadyExists(t *testing.T) { defer tempfile.Remove() _, err := newCIDFile(tempfile.Path()) - assert.ErrorContains(t, err, "Container ID file found") + assert.ErrorContains(t, err, "container ID file found") } func TestCIDFileCloseWithNoWrite(t *testing.T) { diff --git a/cli/command/container/formatter_stats_test.go b/cli/command/container/formatter_stats_test.go index d0be1606ca..844679654b 100644 --- a/cli/command/container/formatter_stats_test.go +++ b/cli/command/container/formatter_stats_test.go @@ -54,13 +54,11 @@ func TestContainerStatsContextWrite(t *testing.T) { }{ { formatter.Context{Format: "{{InvalidFunction}}"}, - `Template parsing error: template: :1: function "InvalidFunction" not defined -`, + `template parsing error: template: :1: function "InvalidFunction" not defined`, }, { formatter.Context{Format: "{{nil}}"}, - `Template parsing error: template: :1:2: executing "" at : nil is not a command -`, + `template parsing error: template: :1:2: executing "" at : nil is not a command`, }, { formatter.Context{Format: "table {{.MemUsage}}"}, diff --git a/cli/command/formatter/container_test.go b/cli/command/formatter/container_test.go index 051c71589d..42dc0a3cda 100644 --- a/cli/command/formatter/container_test.go +++ b/cli/command/formatter/container_test.go @@ -130,13 +130,11 @@ func TestContainerContextWrite(t *testing.T) { // Errors { Context{Format: "{{InvalidFunction}}"}, - `Template parsing error: template: :1: function "InvalidFunction" not defined -`, + `template parsing error: template: :1: function "InvalidFunction" not defined`, }, { Context{Format: "{{nil}}"}, - `Template parsing error: template: :1:2: executing "" at : nil is not a command -`, + `template parsing error: template: :1:2: executing "" at : nil is not a command`, }, // Table Format { diff --git a/cli/command/formatter/disk_usage_test.go b/cli/command/formatter/disk_usage_test.go index 40ca19ec8d..7fff4b2147 100644 --- a/cli/command/formatter/disk_usage_test.go +++ b/cli/command/formatter/disk_usage_test.go @@ -61,8 +61,7 @@ CACHE ID CACHE TYPE SIZE CREATED LAST USED USAGE SHARED Format: "{{InvalidFunction}}", }, }, - `Template parsing error: template: :1: function "InvalidFunction" not defined -`, + `template parsing error: template: :1: function "InvalidFunction" not defined`, }, { DiskUsageContext{ @@ -70,8 +69,7 @@ CACHE ID CACHE TYPE SIZE CREATED LAST USED USAGE SHARED Format: "{{nil}}", }, }, - `Template parsing error: template: :1:2: executing "" at : nil is not a command -`, + `template parsing error: template: :1:2: executing "" at : nil is not a command`, }, // Table Format { diff --git a/cli/command/formatter/formatter.go b/cli/command/formatter/formatter.go index ab0ebdfdfd..4a69e56074 100644 --- a/cli/command/formatter/formatter.go +++ b/cli/command/formatter/formatter.go @@ -73,7 +73,7 @@ func (c *Context) preFormat() { func (c *Context) parseFormat() (*template.Template, error) { tmpl, err := templates.Parse(c.finalFormat) if err != nil { - return tmpl, errors.Errorf("Template parsing error: %v\n", err) + return tmpl, errors.Wrap(err, "template parsing error") } return tmpl, err } @@ -94,7 +94,7 @@ func (c *Context) postFormat(tmpl *template.Template, subContext SubContext) { func (c *Context) contextFormat(tmpl *template.Template, subContext SubContext) error { if err := tmpl.Execute(c.buffer, subContext); err != nil { - return errors.Errorf("Template parsing error: %v\n", err) + return errors.Wrap(err, "template parsing error") } if c.Format.IsTable() && c.header != nil { c.header = subContext.FullHeader() diff --git a/cli/command/formatter/image_test.go b/cli/command/formatter/image_test.go index b4b039d2da..ab336b4a39 100644 --- a/cli/command/formatter/image_test.go +++ b/cli/command/formatter/image_test.go @@ -119,8 +119,7 @@ func TestImageContextWrite(t *testing.T) { Format: "{{InvalidFunction}}", }, }, - `Template parsing error: template: :1: function "InvalidFunction" not defined -`, + `template parsing error: template: :1: function "InvalidFunction" not defined`, }, { ImageContext{ @@ -128,8 +127,7 @@ func TestImageContextWrite(t *testing.T) { Format: "{{nil}}", }, }, - `Template parsing error: template: :1:2: executing "" at : nil is not a command -`, + `template parsing error: template: :1:2: executing "" at : nil is not a command`, }, // Table Format { diff --git a/cli/command/formatter/volume_test.go b/cli/command/formatter/volume_test.go index 75a8f10d39..b34dc74676 100644 --- a/cli/command/formatter/volume_test.go +++ b/cli/command/formatter/volume_test.go @@ -62,13 +62,11 @@ func TestVolumeContextWrite(t *testing.T) { // Errors { Context{Format: "{{InvalidFunction}}"}, - `Template parsing error: template: :1: function "InvalidFunction" not defined -`, + `template parsing error: template: :1: function "InvalidFunction" not defined`, }, { Context{Format: "{{nil}}"}, - `Template parsing error: template: :1:2: executing "" at : nil is not a command -`, + `template parsing error: template: :1:2: executing "" at : nil is not a command`, }, // Table format { diff --git a/cli/command/image/build.go b/cli/command/image/build.go index 5fc8911410..6f5ea8828a 100644 --- a/cli/command/image/build.go +++ b/cli/command/image/build.go @@ -254,7 +254,7 @@ func runBuild(dockerCli command.Cli, options buildOptions) error { } if err := build.ValidateContextDirectory(contextDir, excludes); err != nil { - return errors.Errorf("error checking context: '%s'.", err) + return errors.Wrap(err, "error checking context") } // And canonicalize dockerfile name to a platform-independent one diff --git a/cli/command/inspect/inspector.go b/cli/command/inspect/inspector.go index 307972e2b0..4a6f210807 100644 --- a/cli/command/inspect/inspector.go +++ b/cli/command/inspect/inspector.go @@ -50,7 +50,7 @@ func NewTemplateInspectorFromString(out io.Writer, tmplStr string) (Inspector, e tmpl, err := templates.Parse(tmplStr) if err != nil { - return nil, errors.Errorf("Template parsing error: %s", err) + return nil, errors.Errorf("template parsing error: %s", err) } return NewTemplateInspector(out, tmpl), nil } @@ -100,7 +100,7 @@ func (i *TemplateInspector) Inspect(typedElement interface{}, rawElement []byte) buffer := new(bytes.Buffer) if err := i.tmpl.Execute(buffer, typedElement); err != nil { if rawElement == nil { - return errors.Errorf("Template parsing error: %v", err) + return errors.Errorf("template parsing error: %v", err) } return i.tryRawInspectFallback(rawElement) } @@ -124,7 +124,7 @@ func (i *TemplateInspector) tryRawInspectFallback(rawElement []byte) error { tmplMissingKey := i.tmpl.Option("missingkey=error") if rawErr := tmplMissingKey.Execute(buffer, raw); rawErr != nil { - return errors.Errorf("Template parsing error: %v", rawErr) + return errors.Errorf("template parsing error: %v", rawErr) } i.buffer.Write(buffer.Bytes()) diff --git a/cli/command/inspect/inspector_test.go b/cli/command/inspect/inspector_test.go index 637f3b92d1..142d50342b 100644 --- a/cli/command/inspect/inspector_test.go +++ b/cli/command/inspect/inspector_test.go @@ -62,7 +62,7 @@ func TestTemplateInspectorTemplateError(t *testing.T) { t.Fatal("Expected error got nil") } - if !strings.HasPrefix(err.Error(), "Template parsing error") { + if !strings.HasPrefix(err.Error(), "template parsing error") { t.Fatalf("Expected template error, got %v", err) } } @@ -98,7 +98,7 @@ func TestTemplateInspectorRawFallbackError(t *testing.T) { t.Fatal("Expected error got nil") } - if !strings.HasPrefix(err.Error(), "Template parsing error") { + if !strings.HasPrefix(err.Error(), "template parsing error") { t.Fatalf("Expected template error, got %v", err) } } diff --git a/cli/command/network/formatter_test.go b/cli/command/network/formatter_test.go index 02ae981670..3cadbecbcf 100644 --- a/cli/command/network/formatter_test.go +++ b/cli/command/network/formatter_test.go @@ -79,13 +79,11 @@ func TestNetworkContextWrite(t *testing.T) { // Errors { formatter.Context{Format: "{{InvalidFunction}}"}, - `Template parsing error: template: :1: function "InvalidFunction" not defined -`, + `template parsing error: template: :1: function "InvalidFunction" not defined`, }, { formatter.Context{Format: "{{nil}}"}, - `Template parsing error: template: :1:2: executing "" at : nil is not a command -`, + `template parsing error: template: :1:2: executing "" at : nil is not a command`, }, // Table format { diff --git a/cli/command/node/formatter_test.go b/cli/command/node/formatter_test.go index 87b65a14f2..442359f671 100644 --- a/cli/command/node/formatter_test.go +++ b/cli/command/node/formatter_test.go @@ -62,15 +62,13 @@ func TestNodeContextWrite(t *testing.T) { // Errors { - context: formatter.Context{Format: "{{InvalidFunction}}"}, - expected: `Template parsing error: template: :1: function "InvalidFunction" not defined -`, + context: formatter.Context{Format: "{{InvalidFunction}}"}, + expected: `template parsing error: template: :1: function "InvalidFunction" not defined`, clusterInfo: swarm.ClusterInfo{TLSInfo: swarm.TLSInfo{TrustRoot: "hi"}}, }, { - context: formatter.Context{Format: "{{nil}}"}, - expected: `Template parsing error: template: :1:2: executing "" at : nil is not a command -`, + context: formatter.Context{Format: "{{nil}}"}, + expected: `template parsing error: template: :1:2: executing "" at : nil is not a command`, clusterInfo: swarm.ClusterInfo{TLSInfo: swarm.TLSInfo{TrustRoot: "hi"}}, }, // Table format diff --git a/cli/command/plugin/formatter_test.go b/cli/command/plugin/formatter_test.go index 26eeca7be9..b2e991287d 100644 --- a/cli/command/plugin/formatter_test.go +++ b/cli/command/plugin/formatter_test.go @@ -59,13 +59,11 @@ func TestPluginContextWrite(t *testing.T) { // Errors { formatter.Context{Format: "{{InvalidFunction}}"}, - `Template parsing error: template: :1: function "InvalidFunction" not defined -`, + `template parsing error: template: :1: function "InvalidFunction" not defined`, }, { formatter.Context{Format: "{{nil}}"}, - `Template parsing error: template: :1:2: executing "" at : nil is not a command -`, + `template parsing error: template: :1:2: executing "" at : nil is not a command`, }, // Table format { diff --git a/cli/command/plugin/inspect_test.go b/cli/command/plugin/inspect_test.go index 26de608830..4d3127660a 100644 --- a/cli/command/plugin/inspect_test.go +++ b/cli/command/plugin/inspect_test.go @@ -61,7 +61,7 @@ func TestInspectErrors(t *testing.T) { flags: map[string]string{ "format": "{{invalid format}}", }, - expectedError: "Template parsing error", + expectedError: "template parsing error", }, } diff --git a/cli/command/plugin/list_test.go b/cli/command/plugin/list_test.go index d930d0d454..ed30eb1c59 100644 --- a/cli/command/plugin/list_test.go +++ b/cli/command/plugin/list_test.go @@ -41,7 +41,7 @@ func TestListErrors(t *testing.T) { flags: map[string]string{ "format": "{{invalid format}}", }, - expectedError: "Template parsing error", + expectedError: "template parsing error", }, } diff --git a/cli/command/registry/formatter_search_test.go b/cli/command/registry/formatter_search_test.go index 0255321118..8c72408cf3 100644 --- a/cli/command/registry/formatter_search_test.go +++ b/cli/command/registry/formatter_search_test.go @@ -112,13 +112,11 @@ func TestSearchContextWrite(t *testing.T) { // Errors { formatter.Context{Format: "{{InvalidFunction}}"}, - `Template parsing error: template: :1: function "InvalidFunction" not defined -`, + `template parsing error: template: :1: function "InvalidFunction" not defined`, }, { formatter.Context{Format: "{{nil}}"}, - `Template parsing error: template: :1:2: executing "" at : nil is not a command -`, + `template parsing error: template: :1:2: executing "" at : nil is not a command`, }, // Table format { diff --git a/cli/command/secret/formatter_test.go b/cli/command/secret/formatter_test.go index a5e3aac874..e87d013e1b 100644 --- a/cli/command/secret/formatter_test.go +++ b/cli/command/secret/formatter_test.go @@ -19,13 +19,11 @@ func TestSecretContextFormatWrite(t *testing.T) { // Errors { formatter.Context{Format: "{{InvalidFunction}}"}, - `Template parsing error: template: :1: function "InvalidFunction" not defined -`, + `template parsing error: template: :1: function "InvalidFunction" not defined`, }, { formatter.Context{Format: "{{nil}}"}, - `Template parsing error: template: :1:2: executing "" at : nil is not a command -`, + `template parsing error: template: :1:2: executing "" at : nil is not a command`, }, // Table format {formatter.Context{Format: NewFormat("table", false)}, diff --git a/cli/command/secret/inspect_test.go b/cli/command/secret/inspect_test.go index 6919cb8943..e05311f2af 100644 --- a/cli/command/secret/inspect_test.go +++ b/cli/command/secret/inspect_test.go @@ -36,7 +36,7 @@ func TestSecretInspectErrors(t *testing.T) { flags: map[string]string{ "format": "{{invalid format}}", }, - expectedError: "Template parsing error", + expectedError: "template parsing error", }, { args: []string{"foo", "bar"}, diff --git a/cli/command/service/formatter_test.go b/cli/command/service/formatter_test.go index 74e1a3677b..c15c9e4d96 100644 --- a/cli/command/service/formatter_test.go +++ b/cli/command/service/formatter_test.go @@ -29,13 +29,11 @@ func TestServiceContextWrite(t *testing.T) { // Errors { formatter.Context{Format: "{{InvalidFunction}}"}, - `Template parsing error: template: :1: function "InvalidFunction" not defined -`, + `template parsing error: template: :1: function "InvalidFunction" not defined`, }, { formatter.Context{Format: "{{nil}}"}, - `Template parsing error: template: :1:2: executing "" at : nil is not a command -`, + `template parsing error: template: :1:2: executing "" at : nil is not a command`, }, // Table format { diff --git a/cli/command/stack/formatter/formatter_test.go b/cli/command/stack/formatter/formatter_test.go index 5c629037cd..ec9ef83ada 100644 --- a/cli/command/stack/formatter/formatter_test.go +++ b/cli/command/stack/formatter/formatter_test.go @@ -16,13 +16,11 @@ func TestStackContextWrite(t *testing.T) { // Errors { formatter.Context{Format: "{{InvalidFunction}}"}, - `Template parsing error: template: :1: function "InvalidFunction" not defined -`, + `template parsing error: template: :1: function "InvalidFunction" not defined`, }, { formatter.Context{Format: "{{nil}}"}, - `Template parsing error: template: :1:2: executing "" at : nil is not a command -`, + `template parsing error: template: :1:2: executing "" at : nil is not a command`, }, // Table format { diff --git a/cli/command/stack/list_test.go b/cli/command/stack/list_test.go index a78558d811..3d18f894d9 100644 --- a/cli/command/stack/list_test.go +++ b/cli/command/stack/list_test.go @@ -28,7 +28,7 @@ func TestListErrors(t *testing.T) { flags: map[string]string{ "format": "{{invalid format}}", }, - expectedError: "Template parsing error", + expectedError: "template parsing error", }, { serviceListFunc: func(options types.ServiceListOptions) ([]swarm.Service, error) { diff --git a/cli/command/stack/loader/loader.go b/cli/command/stack/loader/loader.go index 210803ceec..a61efc7bd7 100644 --- a/cli/command/stack/loader/loader.go +++ b/cli/command/stack/loader/loader.go @@ -27,6 +27,7 @@ func LoadComposefile(dockerCli command.Cli, opts options.Deploy) (*composetypes. config, err := loader.Load(configDetails) if err != nil { if fpe, ok := err.(*loader.ForbiddenPropertiesError); ok { + //nolint:revive // ignore capitalization error; this error is intentionally formatted multi-line return nil, errors.Errorf("Compose file contains unsupported options:\n\n%s\n", propertyWarnings(fpe.Properties)) } diff --git a/cli/command/stack/services_test.go b/cli/command/stack/services_test.go index e38c1b9534..f48ee2b11c 100644 --- a/cli/command/stack/services_test.go +++ b/cli/command/stack/services_test.go @@ -62,7 +62,7 @@ func TestStackServicesErrors(t *testing.T) { serviceListFunc: func(options types.ServiceListOptions) ([]swarm.Service, error) { return []swarm.Service{*Service()}, nil }, - expectedError: "Template parsing error", + expectedError: "template parsing error", }, } diff --git a/cli/command/system/info.go b/cli/command/system/info.go index f4882aa158..1780487111 100644 --- a/cli/command/system/info.go +++ b/cli/command/system/info.go @@ -548,7 +548,7 @@ func formatInfo(dockerCli command.Cli, info info, format string) error { tmpl, err := templates.Parse(format) if err != nil { return cli.StatusError{StatusCode: 64, - Status: "Template parsing error: " + err.Error()} + Status: "template parsing error: " + err.Error()} } err = tmpl.Execute(dockerCli.Out(), info) dockerCli.Out().Write([]byte{'\n'}) diff --git a/cli/command/system/info_test.go b/cli/command/system/info_test.go index 4bd489f955..499ea4edd1 100644 --- a/cli/command/system/info_test.go +++ b/cli/command/system/info_test.go @@ -396,7 +396,7 @@ func TestFormatInfo(t *testing.T) { { doc: "syntax", template: "{{}", - expectedError: `Status: Template parsing error: template: :1: unexpected "}" in command, Code: 64`, + expectedError: `Status: template parsing error: template: :1: unexpected "}" in command, Code: 64`, }, { doc: "syntax", diff --git a/cli/command/system/version.go b/cli/command/system/version.go index 3d1d9b9289..06b62ae763 100644 --- a/cli/command/system/version.go +++ b/cli/command/system/version.go @@ -198,7 +198,7 @@ func newVersionTemplate(templateFormat string) (*template.Template, error) { tmpl := templates.New("version").Funcs(template.FuncMap{"getDetailsOrder": getDetailsOrder}) tmpl, err := tmpl.Parse(templateFormat) - return tmpl, errors.Wrap(err, "Template parsing error") + return tmpl, errors.Wrap(err, "template parsing error") } func getDetailsOrder(v types.ComponentVersion) []string { diff --git a/cli/command/task/formatter_test.go b/cli/command/task/formatter_test.go index 2dfdb98587..4a7ed7fb2f 100644 --- a/cli/command/task/formatter_test.go +++ b/cli/command/task/formatter_test.go @@ -20,13 +20,11 @@ func TestTaskContextWrite(t *testing.T) { }{ { formatter.Context{Format: "{{InvalidFunction}}"}, - `Template parsing error: template: :1: function "InvalidFunction" not defined -`, + `template parsing error: template: :1: function "InvalidFunction" not defined`, }, { formatter.Context{Format: "{{nil}}"}, - `Template parsing error: template: :1:2: executing "" at : nil is not a command -`, + `template parsing error: template: :1:2: executing "" at : nil is not a command`, }, { formatter.Context{Format: NewTaskFormat("table", true)}, diff --git a/cli/command/trust/formatter_test.go b/cli/command/trust/formatter_test.go index d3863b034d..c8a507cf9f 100644 --- a/cli/command/trust/formatter_test.go +++ b/cli/command/trust/formatter_test.go @@ -95,15 +95,13 @@ func TestTrustTagContextWrite(t *testing.T) { formatter.Context{ Format: "{{InvalidFunction}}", }, - `Template parsing error: template: :1: function "InvalidFunction" not defined -`, + `template parsing error: template: :1: function "InvalidFunction" not defined`, }, { formatter.Context{ Format: "{{nil}}", }, - `Template parsing error: template: :1:2: executing "" at : nil is not a command -`, + `template parsing error: template: :1:2: executing "" at : nil is not a command`, }, // Table Format { @@ -191,15 +189,13 @@ func TestSignerInfoContextWrite(t *testing.T) { formatter.Context{ Format: "{{InvalidFunction}}", }, - `Template parsing error: template: :1: function "InvalidFunction" not defined -`, + `template parsing error: template: :1: function "InvalidFunction" not defined`, }, { formatter.Context{ Format: "{{nil}}", }, - `Template parsing error: template: :1:2: executing "" at : nil is not a command -`, + `template parsing error: template: :1:2: executing "" at : nil is not a command`, }, // Table Format { diff --git a/cli/command/volume/create.go b/cli/command/volume/create.go index 125da44332..180cea6928 100644 --- a/cli/command/volume/create.go +++ b/cli/command/volume/create.go @@ -32,7 +32,7 @@ func newCreateCommand(dockerCli command.Cli) *cobra.Command { RunE: func(cmd *cobra.Command, args []string) error { if len(args) == 1 { if options.name != "" { - return errors.Errorf("Conflicting options: either specify --name or provide positional arg, not both\n") + return errors.Errorf("conflicting options: either specify --name or provide positional arg, not both") } options.name = args[0] } diff --git a/cli/command/volume/create_test.go b/cli/command/volume/create_test.go index d05e8be78d..875253b5c8 100644 --- a/cli/command/volume/create_test.go +++ b/cli/command/volume/create_test.go @@ -26,7 +26,7 @@ func TestVolumeCreateErrors(t *testing.T) { flags: map[string]string{ "name": "volumeName", }, - expectedError: "Conflicting options: either specify --name or provide positional arg, not both", + expectedError: "conflicting options: either specify --name or provide positional arg, not both", }, { args: []string{"too", "many"}, diff --git a/cli/command/volume/inspect_test.go b/cli/command/volume/inspect_test.go index 929a4f08a6..40ab82c50b 100644 --- a/cli/command/volume/inspect_test.go +++ b/cli/command/volume/inspect_test.go @@ -35,7 +35,7 @@ func TestVolumeInspectErrors(t *testing.T) { flags: map[string]string{ "format": "{{invalid format}}", }, - expectedError: "Template parsing error", + expectedError: "template parsing error", }, { args: []string{"foo", "bar"}, diff --git a/cli/compose/interpolation/interpolation.go b/cli/compose/interpolation/interpolation.go index fd34ca82ea..e84756dba6 100644 --- a/cli/compose/interpolation/interpolation.go +++ b/cli/compose/interpolation/interpolation.go @@ -99,7 +99,7 @@ func newPathError(path Path, err error) error { return nil case *template.InvalidTemplateError: return errors.Errorf( - "invalid interpolation format for %s: %#v. You may need to escape any $ with another $.", + "invalid interpolation format for %s: %#v; you may need to escape any $ with another $", path, err.Template) default: return errors.Wrapf(err, "error while interpolating %s", path) diff --git a/cli/compose/interpolation/interpolation_test.go b/cli/compose/interpolation/interpolation_test.go index 55800b1b26..536da0013b 100644 --- a/cli/compose/interpolation/interpolation_test.go +++ b/cli/compose/interpolation/interpolation_test.go @@ -58,7 +58,7 @@ func TestInvalidInterpolation(t *testing.T) { }, } _, err := Interpolate(services, Options{LookupValue: defaultMapping}) - assert.Error(t, err, `invalid interpolation format for servicea.image: "${". You may need to escape any $ with another $.`) + assert.Error(t, err, `invalid interpolation format for servicea.image: "${"; you may need to escape any $ with another $`) } func TestInterpolateWithDefaults(t *testing.T) { From 3ffe6a33759a8da08ac42767e7899e2ed26a8c38 Mon Sep 17 00:00:00 2001 From: Sebastiaan van Stijn Date: Sun, 27 Mar 2022 19:24:19 +0200 Subject: [PATCH 4/4] lint: update golangci-lint to v1.45.2 Also removed deprecated linters: The linter 'interfacer' is deprecated (since v1.38.0) due to: The repository of the linter has been archived by the owner. The linter 'golint' is deprecated (since v1.41.0) due to: The repository of the linter has been archived by the owner. Replaced by revive. Signed-off-by: Sebastiaan van Stijn --- .golangci.yml | 60 +++++++++++++++++++++++++++++-------- dockerfiles/Dockerfile.lint | 2 +- 2 files changed, 48 insertions(+), 14 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index da889369f3..1d8f1ba60d 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -6,12 +6,10 @@ linters: - dogsled - gocyclo - goimports - - golint - gosec - gosimple - govet - ineffassign - - interfacer - lll - megacheck - misspell @@ -22,6 +20,7 @@ linters: - unconvert - unparam - unused + - revive - varcheck disable: @@ -59,30 +58,65 @@ issues: - parameter .* always receives exclude-rules: - # These are copied from the default exclude rules, except for "ineffective break statement" - # and GoDoc checks. - # https://github.com/golangci/golangci-lint/blob/0cc87df732aaf1d5ad9ce9ca538d38d916918b36/pkg/config/config.go#L36 - - text: "Error return value of .((os\\.)?std(out|err)\\..*|.*Close|.*Flush|os\\.Remove(All)?|.*printf?|os\\.(Un)?Setenv). is not checked" + # We prefer to use an "exclude-list" so that new "default" exclusions are not + # automatically inherited. We can decide whether or not to follow upstream + # defaults when updating golang-ci-lint versions. + # Unfortunately, this means we have to copy the whole exclusion pattern, as + # (unlike the "include" option), the "exclude" option does not take exclusion + # ID's. + # + # These exclusion patterns are copied from the default excluses at: + # https://github.com/golangci/golangci-lint/blob/v1.44.0/pkg/config/issues.go#L10-L104 + + # EXC0001 + - text: "Error return value of .((os\\.)?std(out|err)\\..*|.*Close|.*Flush|os\\.Remove(All)?|.*print(f|ln)?|os\\.(Un)?Setenv). is not checked" linters: - errcheck + # EXC0003 - text: "func name will be used as test\\.Test.* by other packages, and that stutters; consider calling this" linters: - - golint - - text: "G103: Use of unsafe calls should be audited" + - revive + # EXC0006 + - text: "Use of unsafe calls should be audited" linters: - gosec - - text: "G104: Errors unhandled" + # EXC0007 + - text: "Subprocess launch(ed with variable|ing should be audited)" linters: - gosec - - text: "G204: Subprocess launch(ed with (variable|function call)|ing should be audited)" + # EXC0008 + # TODO: evaluate these and fix where needed: G307: Deferring unsafe method "*os.File" on type "Close" (gosec) + - text: "(G104|G307)" linters: - gosec - - text: "(G301|G302): (Expect directory permissions to be 0750 or less|Expect file permissions to be 0600 or less)" + # EXC0009 + - text: "(Expect directory permissions to be 0750 or less|Expect file permissions to be 0600 or less)" linters: - gosec - - text: "G304: Potential file inclusion via variable" + # EXC0010 + - text: "Potential file inclusion via variable" linters: - gosec - - text: "(G201|G202): SQL string (formatting|concatenation)" + + # Looks like the match in "EXC0007" above doesn't catch this one + # TODO: consider upstreaming this to golangci-lint's default exclusion rules + - text: "G204: Subprocess launched with a potential tainted input or cmd arguments" linters: - gosec + # Looks like the match in "EXC0009" above doesn't catch this one + # TODO: consider upstreaming this to golangci-lint's default exclusion rules + - text: "G306: Expect WriteFile permissions to be 0600 or less" + linters: + - gosec + + # Exclude some linters from running on tests files. + - path: _test\.go + linters: + - errcheck + - gosec + + # Maximum issues count per one linter. Set to 0 to disable. Default is 50. + max-issues-per-linter: 0 + + # Maximum count of issues with the same text. Set to 0 to disable. Default is 3. + max-same-issues: 0 diff --git a/dockerfiles/Dockerfile.lint b/dockerfiles/Dockerfile.lint index 8f3c30ec01..b28d02634f 100644 --- a/dockerfiles/Dockerfile.lint +++ b/dockerfiles/Dockerfile.lint @@ -1,7 +1,7 @@ # syntax=docker/dockerfile:1 ARG GO_VERSION=1.17.8 -ARG GOLANGCI_LINT_VERSION=v1.23.8 +ARG GOLANGCI_LINT_VERSION=v1.45.2 FROM golangci/golangci-lint:${GOLANGCI_LINT_VERSION}-alpine AS golangci-lint