Skip to content
This repository has been archived by the owner on Sep 18, 2021. It is now read-only.

Commit

Permalink
style(cli): adjustments for linter
Browse files Browse the repository at this point in the history
  • Loading branch information
saitho committed Sep 9, 2020
1 parent 0d59347 commit 41a2745
Show file tree
Hide file tree
Showing 11 changed files with 35 additions and 23 deletions.
8 changes: 4 additions & 4 deletions ansible/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 8 additions & 2 deletions ansible/inventory.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")
Expand All @@ -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
}

Expand Down
2 changes: 1 addition & 1 deletion commands/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down
2 changes: 1 addition & 1 deletion commands/init/install_collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions commands/init/install_modules.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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)
Expand Down
7 changes: 5 additions & 2 deletions commands/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions jsonschema/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down
3 changes: 1 addition & 2 deletions jsonschema/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 ""
}
5 changes: 4 additions & 1 deletion routines/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
11 changes: 6 additions & 5 deletions routines/tasks.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand All @@ -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)
}
}
2 changes: 1 addition & 1 deletion stackhead/modules_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit 41a2745

Please sign in to comment.