From 41a2745d234c2629a1a382487517ad73d508f072 Mon Sep 17 00:00:00 2001 From: Mario Lubenka Date: Sun, 6 Sep 2020 15:24:12 +0200 Subject: [PATCH] style(cli): adjustments for linter --- ansible/collection.go | 8 ++++---- ansible/inventory.go | 10 ++++++++-- commands/init.go | 2 +- commands/init/install_collection.go | 2 +- commands/init/install_modules.go | 4 ++-- commands/validate.go | 7 +++++-- jsonschema/validation.go | 4 ++-- jsonschema/validation_test.go | 3 +-- routines/exec.go | 5 ++++- routines/tasks.go | 11 ++++++----- stackhead/modules_test.go | 2 +- 11 files changed, 35 insertions(+), 23 deletions(-) diff --git a/ansible/collection.go b/ansible/collection.go index bda432b..7e1db99 100644 --- a/ansible/collection.go +++ b/ansible/collection.go @@ -22,17 +22,17 @@ func GetAnsibleVersion() (string, error) { return "", err } - return extractAnsibleVersion(stdoutBuffer.String()), nil + return extractAnsibleVersion(stdoutBuffer.String()) } // extractAnsibleVersion extracts the version from version output -func extractAnsibleVersion(versionCmdOutput string) string { +func extractAnsibleVersion(versionCmdOutput string) (string, error) { lines := strings.Split(versionCmdOutput, "\n") versionLine := lines[0] // First line outputs e.g. "ansible 2.10.0" - r, _ := regexp.Compile("\\w+ (\\d+.\\d+.\\d+)") - return r.FindStringSubmatch(versionLine)[1] + r := regexp.MustCompile(`\w+ (\d+.\d+.\d+)`) + return r.FindStringSubmatch(versionLine)[1], nil } // GetCollectionDirs returns a list of Ansible collection paths from config or environment diff --git a/ansible/inventory.go b/ansible/inventory.go index 7a07a8b..9863964 100644 --- a/ansible/inventory.go +++ b/ansible/inventory.go @@ -39,7 +39,7 @@ func copyFile(srcPath string, destPath string) error { return err } - if err = ioutil.WriteFile(destPath, input, 0644); err != nil { + if err = ioutil.WriteFile(destPath, input, 0600); err != nil { return err } return nil @@ -73,6 +73,9 @@ func CreateInventoryFile(options ...InventoryOption) (string, error) { // Copy project definition file err = copyFile(opts.ProjectDefinitionFile, filepath.Join(opts.TmpProjectDefinitionFolder, projectDefinitionFile)) + if err != nil { + return "", err + } opts.ProjectDefinitionFileName = projectDefinitionFile opts.ProjectDefinitionFileName = strings.TrimSuffix(opts.ProjectDefinitionFileName, ".yml") @@ -93,13 +96,16 @@ all: applications: - {{ .ProjectDefinitionFileName }} `) + if err != nil { + return "", err + } if err = t.Execute(tmpFile, opts); err != nil { return "", err } // Close the file - if err := tmpFile.Close(); err != nil { + if err = tmpFile.Close(); err != nil { return "", err } diff --git a/commands/init.go b/commands/init.go index 004e816..46b2125 100644 --- a/commands/init.go +++ b/commands/init.go @@ -3,7 +3,7 @@ package commands import ( "github.com/spf13/cobra" - "github.com/getstackhead/stackhead/cli/commands/init" + commandsinit "github.com/getstackhead/stackhead/cli/commands/init" "github.com/getstackhead/stackhead/cli/routines" ) diff --git a/commands/init/install_collection.go b/commands/init/install_collection.go index 3b3e25f..3527e76 100644 --- a/commands/init/install_collection.go +++ b/commands/init/install_collection.go @@ -44,7 +44,7 @@ func InstallCollection(version string) []routines.TaskOption { // Check if Ansible is installed _, err = ansible.GetAnsibleVersion() if err != nil { - err = fmt.Errorf(fmt.Sprintf("Ansible could not be found on your system. Please install it.")) + err = fmt.Errorf("I could not find Ansible on your system. Please install it") } if err == nil { diff --git a/commands/init/install_modules.go b/commands/init/install_modules.go index 03a5112..3bef9b3 100644 --- a/commands/init/install_modules.go +++ b/commands/init/install_modules.go @@ -21,7 +21,7 @@ func collectModules() []string { } webserver, err = stackhead.AutoCompleteModuleName(webserver, stackhead.ModuleWebserver) if err != nil { - // error + panic(err.Error()) } modules = append(modules, webserver) @@ -31,7 +31,7 @@ func collectModules() []string { } container, err = stackhead.AutoCompleteModuleName(container, stackhead.ModuleContainer) if err != nil { - // error + panic(err.Error()) } modules = append(modules, container) diff --git a/commands/validate.go b/commands/validate.go index 9ab4f4d..55809ad 100644 --- a/commands/validate.go +++ b/commands/validate.go @@ -30,9 +30,12 @@ var Validate = &cobra.Command{ errorMessage := jsonschema.ShouldValidate(result) if len(errorMessage) == 0 { - _, err = fmt.Fprintln(os.Stdout, "The project definition is valid") + _, err = fmt.Fprintf(os.Stdout, "The project definition is valid.\n") } else { - _, err = fmt.Fprintln(os.Stderr, errorMessage) + _, err = fmt.Fprintf(os.Stderr, errorMessage+"\n") + if err != nil { + panic(err.Error()) + } os.Exit(1) } if err != nil { diff --git a/jsonschema/validation.go b/jsonschema/validation.go index 4463548..512847a 100644 --- a/jsonschema/validation.go +++ b/jsonschema/validation.go @@ -53,10 +53,10 @@ func ValidateFile(collectionDir string, filePath string) (*gojsonschema.Result, // signature uses interface{} and unused paremter because this method is also used in tests with Convey func ShouldValidate(actual interface{}, _ ...interface{}) string { result := actual.(*gojsonschema.Result) - if result.Valid() == true { + if result.Valid() { return "" } - errorMessage := fmt.Sprintf("The project definition is not valid. see errors:\n") + errorMessage := "The project definition is not valid. see errors:\n" for _, desc := range result.Errors() { if isInternalError(desc.Type()) { diff --git a/jsonschema/validation_test.go b/jsonschema/validation_test.go index dd5e093..63d43d9 100644 --- a/jsonschema/validation_test.go +++ b/jsonschema/validation_test.go @@ -54,7 +54,6 @@ func ShouldNotValidate(actual interface{}, _ ...interface{}) string { result := jsonschema.ShouldValidate(actual) if result == "" { // file validated return "File validated (it should not)" - } else { - return "" } + return "" } diff --git a/routines/exec.go b/routines/exec.go index 60e8fc8..3504e3d 100644 --- a/routines/exec.go +++ b/routines/exec.go @@ -17,7 +17,10 @@ func Exec(name string, arg ...string) error { cmd := exec.Command(name, arg...) var errBuffer = new(bytes.Buffer) if viper.GetBool("verbose") { - fmt.Fprintln(os.Stdout, fmt.Sprintf("Executing command: %s", strings.Join(append([]string{name}, arg...), " "))) + _, err := fmt.Fprintf(os.Stdout, "Executing command: %s\n", strings.Join(append([]string{name}, arg...), " ")) + if err != nil { + return err + } cmd.Stdout = os.Stdout cmd.Stderr = os.Stdout } else { diff --git a/routines/tasks.go b/routines/tasks.go index bb5cf74..8759638 100644 --- a/routines/tasks.go +++ b/routines/tasks.go @@ -12,9 +12,9 @@ import ( const ( // ErrorColor colors the first text red and the second text default color - ErrorColor = "\033[1;31m%s\033[0m%s" + ErrorColor = "\033[1;31m%s\033[0m%s\n" // SuccessColor colors the first text green and the second text default color - SuccessColor = "\033[1;32m%s\033[0m%s" + SuccessColor = "\033[1;32m%s\033[0m%s\n" ) // TaskOptions is a configuration used to define the behaviour of tasks and processing functions @@ -60,9 +60,10 @@ func RunTask(options ...TaskOption) { // Disable spinner when verbose mode is enabled as it does not like additional stdout messages var s *spinner.Spinner if viper.GetBool("verbose") { - fmt.Fprintln(os.Stdout, fmt.Sprintf("⌛ %s", t.Text)) + _, _ = fmt.Fprintf(os.Stdout, "⌛ %s\n", t.Text) } else { s = spinner.New(spinner.CharSets[11], 150*time.Millisecond) + //s.ShowTimeElapsed = true s.Reverse() s.Suffix = " " + t.Text s.Start() @@ -82,8 +83,8 @@ func RunTask(options ...TaskOption) { result.Message = t.Text } if result.Error { - fmt.Fprintln(os.Stdout, fmt.Sprintf(ErrorColor, "✗ ", result.Message)) + fmt.Fprintf(os.Stdout, ErrorColor, "✗ ", result.Message) } else { - fmt.Fprintln(os.Stdout, fmt.Sprintf(SuccessColor, "✓ ", result.Message)) + fmt.Fprintf(os.Stdout, SuccessColor, "✓ ", result.Message) } } diff --git a/stackhead/modules_test.go b/stackhead/modules_test.go index 89e0131..ece84c4 100644 --- a/stackhead/modules_test.go +++ b/stackhead/modules_test.go @@ -142,7 +142,7 @@ func TestIsContainerModule(t *testing.T) { }) } -func GetModuleType(t *testing.T) { +func TestGetModuleType(t *testing.T) { Convey("get module type of module name", t, func() { Convey("webserver module", func() { moduleType := stackhead.GetModuleType("getstackhead.stackhead_webserver_nginx")