Skip to content

Commit 8136905

Browse files
Handle problems detected by used linters
The problems in the code base detected by the linters that have been integrated in GH-62 through GolangCI have been handled by refactoring the affected implementations. This helps to improve the overall code quality and prevents possible errors. 1. Removed unused function parameters detected by unparam (1). 1. `(*cmdOptions).prepare` - `cmd` is unused: cmd/snowsaw/bootstrap/bootstrap.go:51:30 (2) ```go func (o *cmdOptions) prepare(cmd *cobra.Command, args []string) { ^ ``` 2. `(*cmdOptions).run` - `cmd` is unused: cmd/snowsaw/bootstrap/bootstrap.go:100:26 (2) ```go func (o *cmdOptions) run(cmd *cobra.Command, args []string) { ^ ``` 3. `(*cmdOptions).run` - `args` is unused: cmd/snowsaw/bootstrap/bootstrap.go:100:46 (2) ```go func (o *cmdOptions) run(cmd *cobra.Command, args []string) { ^ ``` 2. Improved function names and code flows detected by golint (3). 1. func `NewJsonEncoder` should be `NewJSONEncoder`: pkg/config/encoder/json/json.go:34:6 (4) ```go func NewJsonEncoder() Encoder { ^ ``` 2. var `ExtensionsJson` should be `ExtensionsJSON`: pkg/config/encoder/constants.go:26:2 (5) ```go ExtensionsJson = "json" ^ ``` 3. if block ends with a return statement, so drop this else and outdent its block (move short variable declaration to its own line if necessary): pkg/prt/printer.go:121:9 (6) ```go } else { ^ ``` 4. exported func Load returns unexported type *builder.builder, which can be annoying to use: pkg/config/builder/builder.go:39:32 (7) ```go func Load(files ...*file.File) *builder { ^ ``` 3. Improved code style smells detected by gocritic (8). 1. assignOp: replace `format = format + "\n"` with `format += "\n"`: pkg/prt/printer.go:179:4 (9) ```go format = format + "\n" ^ ``` 2. paramTypeCombine: `func(v Verbosity, w io.Writer, prefix string, format string, args ...interface{})` could be replaced with `func(v Verbosity, w io.Writer, prefix, format string, args ...interface{})`: pkg/prt/printer.go:176:1 (10) ```go func (p *printerConfig) withNewLine(v Verbosity, w io.Writer, ^ prefix string, format string, args ...interface{}) { ``` 3. emptyStringTest: replace `len(parts[0]) == 0` with `parts[0] == ""`: pkg/snowblock/task/shell/shell.go:165:5 (11) ```go if len(parts[0]) == 0 { ^ ``` 4. elseif: can replace 'else {if cond {}}' with 'else if cond {}': cmd/snowsaw/bootstrap/bootstrap.go:57:9 (12) ```go } else { ^ ``` 4. Remove unnecessary type conversions detected by unconvert (13). 1. unnecessary conversion: pkg/prt/printer.go:132:16 (14) ```go *v = Verbosity(l) ^ ``` References: (1) https://github.com/mvdan/unparam (2) https://github.com/arcticicestudio/snowsaw/blob/9366c4a9c6d59dd0fccad12fbc413842ea751fa6/cmd/snowsaw/bootstrap/bootstrap.go#L51 (3) https://github.com/golang/lint (4) https://github.com/arcticicestudio/snowsaw/blob/5aa483e7e5e45888254aa4d0143d2afb898b4332/pkg/config/encoder/json/json.go#L34 (5) https://github.com/arcticicestudio/snowsaw/blob/008edbcb509af2cb5ced942d679fa3845a4ec1e1/pkg/config/encoder/constants.go#L26 (6) https://github.com/arcticicestudio/snowsaw/blob/79afc12ebc15620fd94e78416e0b49a68bbf2eb6/pkg/prt/printer.go#L121 (7) https://github.com/arcticicestudio/snowsaw/blob/dea6ab56b7410a8cbc8901818703d5ab1ace5c87/pkg/config/builder/builder.go#L39 (8) https://github.com/go-critic/go-critic (9) https://github.com/arcticicestudio/snowsaw/blob/79afc12ebc15620fd94e78416e0b49a68bbf2eb6/pkg/prt/printer.go#L179 (10) https://github.com/arcticicestudio/snowsaw/blob/79afc12ebc15620fd94e78416e0b49a68bbf2eb6/pkg/prt/printer.go#L176 (11) https://github.com/arcticicestudio/snowsaw/blob/a78810b7ccb5ddb8e80929d54fb7c461a1b80a1c/pkg/snowblock/task/shell/shell.go#L165 (12) https://github.com/arcticicestudio/snowsaw/blob/9366c4a9c6d59dd0fccad12fbc413842ea751fa6/cmd/snowsaw/bootstrap/bootstrap.go#L57 (13) https://github.com/mdempsky/unconvert (14) https://github.com/arcticicestudio/snowsaw/blob/79afc12ebc15620fd94e78416e0b49a68bbf2eb6/pkg/prt/printer.go#L132 Epic: GH-33 Resolves GH-80
1 parent a78810b commit 8136905

File tree

8 files changed

+25
-27
lines changed

8 files changed

+25
-27
lines changed

cmd/snowsaw/bootstrap/bootstrap.go

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,24 +41,22 @@ func NewBootstrapCmd() *cobra.Command {
4141
To process individual snowblocks a list of space-separated paths can be passed as arguments.
4242
`,
4343
Run: func(cmd *cobra.Command, args []string) {
44-
o.prepare(cmd, args)
45-
o.run(cmd, args)
44+
o.prepare(args)
45+
o.run()
4646
},
4747
}
4848
return bootstrapCmd
4949
}
5050

51-
func (o *cmdOptions) prepare(cmd *cobra.Command, args []string) {
51+
func (o *cmdOptions) prepare(args []string) {
5252
// Use explicit snowblocks if specified, otherwise find all snowblocks within the base directories.
5353
if len(args) > 0 {
5454
prt.Debugf("Using individual snowblocks instead of configured base directories(s): %s",
5555
color.CyanString("%v", args))
5656
config.AppConfig.Snowblocks.Paths = args
57-
} else {
58-
if err := o.readSnowblockDirectories(); err != nil {
59-
prt.Errorf("Failed to read snowblocks from base directories: %v", err)
60-
os.Exit(1)
61-
}
57+
} else if err := o.readSnowblockDirectories(); err != nil {
58+
prt.Errorf("Failed to read snowblocks from base directories: %v", err)
59+
os.Exit(1)
6260
}
6361
o.SnowblockPaths = config.AppConfig.Snowblocks.Paths
6462
}
@@ -97,7 +95,7 @@ func (o *cmdOptions) readSnowblockDirectories() error {
9795
return nil
9896
}
9997

100-
func (o *cmdOptions) run(cmd *cobra.Command, args []string) {
98+
func (o *cmdOptions) run() {
10199
for _, path := range o.SnowblockPaths {
102100
sb := snowblock.NewSnowblock(path)
103101
err := sb.Validate(config.SnowblockTaskRunnerRegistry.GetAll())

magefile.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,7 @@ func runGolangCILint() {
425425
prt.Errorf("Linters finished with non-zero exit code")
426426
os.Exit(1)
427427
}
428+
prt.Successf("Linters finished successfully with zero exit code")
428429
}
429430

430431
func runGoTest(testFlags ...string) {

pkg/config/builder/builder.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,16 @@ import (
2828
"github.com/arcticicestudio/snowsaw/pkg/util/filesystem"
2929
)
3030

31-
// builder contains the current configuration building state.
32-
type builder struct {
31+
// Builder contains the current configuration building state.
32+
type Builder struct {
3333
Files []*file.File
3434
}
3535

3636
// Load tries to load all given configuration files.
3737
// It checks if the path is valid and exists, tries to assign a matching encoder based on the file extension and returns
3838
// a pointer to a builder to chain the merge function.
39-
func Load(files ...*file.File) *builder {
40-
b := &builder{Files: []*file.File{}}
39+
func Load(files ...*file.File) *Builder {
40+
b := &Builder{Files: []*file.File{}}
4141

4242
for _, f := range files {
4343
// Convert to an absolute path and check if the file exists, otherwise ignore and check next.
@@ -70,7 +70,7 @@ func Load(files ...*file.File) *builder {
7070

7171
// Into accepts a configuration struct pointer and populates it with the current config state.
7272
// The order of the files are maintained when merging of the configuration states is enabled.
73-
func (b *builder) Into(c *config.Config, merge bool) error {
73+
func (b *Builder) Into(c *config.Config, merge bool) error {
7474
for _, f := range b.Files {
7575
content, err := ioutil.ReadFile(f.Path)
7676
if err != nil {

pkg/config/encoder/constants.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ import (
1919
var (
2020
// ExtensionMapping maps supported file extensions to their compatible encoders.
2121
ExtensionMapping = map[string]Encoder{
22-
ExtensionsJson: json.NewJsonEncoder(),
22+
ExtensionsJSON: json.NewJSONEncoder(),
2323
ExtensionsYaml: yaml.NewYamlEncoder(),
2424
}
25-
// ExtensionsJson is the supported extension for files containing JSON data.
26-
ExtensionsJson = "json"
25+
// ExtensionsJSON is the supported extension for files containing JSON data.
26+
ExtensionsJSON = "json"
2727
// ExtensionsYaml is the supported extension for files containing YAML data.
2828
ExtensionsYaml = "yml"
2929
)

pkg/config/encoder/json/json.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func (j Encoder) Decode(d []byte, v interface{}) error {
3030
return json.Unmarshal(d, v)
3131
}
3232

33-
// NewJsonEncoder returns a new JSON Encoder.
34-
func NewJsonEncoder() Encoder {
33+
// NewJSONEncoder returns a new JSON Encoder.
34+
func NewJSONEncoder() Encoder {
3535
return Encoder{}
3636
}

pkg/prt/printer.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,8 @@ func (v Verbosity) MarshalText() ([]byte, error) {
118118
func (v Verbosity) String() string {
119119
if b, err := v.MarshalText(); err == nil {
120120
return string(b)
121-
} else {
122-
return "unknown"
123121
}
122+
return "unknown"
124123
}
125124

126125
// UnmarshalText implements encoding.TextUnmarshaler to unmarshal a textual representation of itself.
@@ -130,7 +129,7 @@ func (v *Verbosity) UnmarshalText(text []byte) error {
130129
return err
131130
}
132131

133-
*v = Verbosity(l)
132+
*v = l
134133
return nil
135134
}
136135

@@ -174,10 +173,10 @@ func (p *printerConfig) isPrinterEnabled(v Verbosity) bool { return p.verbosity
174173
func (p *printerConfig) setVerbosityLevel(v Verbosity) { p.verbosity = v }
175174

176175
// withNewLine writes to the specified writer and appends a new line to the given format if not already.
177-
func (p *printerConfig) withNewLine(v Verbosity, w io.Writer, prefix string, format string, args ...interface{}) {
176+
func (p *printerConfig) withNewLine(v Verbosity, w io.Writer, prefix, format string, args ...interface{}) {
178177
if p.isPrinterEnabled(v) {
179178
if !strings.HasSuffix(format, "\n") {
180-
format = format + "\n"
179+
format += "\n"
181180
}
182181
fmt.Fprintf(w, prefix+format, args...)
183182
}

pkg/snowblock/snowblock.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ func (s *Snowblock) Validate(taskRunner map[string]api.TaskRunner) error {
9999
s.Path = expandedAbsPath
100100

101101
// Try to read and encode the task objects when the directory contains a configuration file.
102-
configFilePath := filepath.Join(s.Path, fmt.Sprintf("%s.%s", api.ConfigurationFileName, encoder.ExtensionsJson))
102+
configFilePath := filepath.Join(s.Path, fmt.Sprintf("%s.%s", api.ConfigurationFileName, encoder.ExtensionsJSON))
103103
if configLoadErr := loadConfigFile(configFilePath, &s.TaskObjects); configLoadErr != nil {
104104
prt.Debugf("Ignoring snowblock directory %s: %v",
105105
color.CyanString(filepath.Base(s.Path)), configLoadErr)
@@ -124,7 +124,7 @@ func (s *Snowblock) Validate(taskRunner map[string]api.TaskRunner) error {
124124
}
125125

126126
func loadConfigFile(absPath string, tasks *[]api.Task) error {
127-
f := file.NewFile(absPath).WithEncoder(json.NewJsonEncoder())
127+
f := file.NewFile(absPath).WithEncoder(json.NewJSONEncoder())
128128
// Check if the file exists...
129129
if exists, _ := filesystem.FileExists(f.Path); !exists {
130130
return fmt.Errorf("no such snowblock configuration file: %s", color.RedString(f.Path))

pkg/snowblock/task/shell/shell.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ func (s *Shell) execute() error {
162162

163163
func (s *Shell) parseCommand(cmd string) error {
164164
parts := strings.Split(strings.TrimSpace(cmd), " ")
165-
if len(parts[0]) == 0 {
165+
if parts[0] == "" {
166166
return fmt.Errorf("shell command must not be empty or whitespace-only")
167167
}
168168

0 commit comments

Comments
 (0)