Skip to content

Commit 3672601

Browse files
authored
Merge pull request #12 from stuartleeks/sl/lint
Add linting. Convert to RunE on commands
2 parents ea9671e + 6303903 commit 3672601

File tree

9 files changed

+72
-72
lines changed

9 files changed

+72
-72
lines changed

Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
build:
22
go build ./cmd/devcontainer
33

4+
lint: build
5+
golangci-lint run
6+
47
devcontainer:
58
docker build -f ./.devcontainer/Dockerfile ./.devcontainer -t devcontainer-cli
69

cmd/devcontainer/completion.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,26 +23,28 @@ func createCompleteCommand(rootCmd *cobra.Command) *cobra.Command {
2323
# ~/.bashrc or ~/.profile
2424
. <(devcontainer completion)
2525
`,
26-
Run: func(cmd *cobra.Command, args []string) {
26+
RunE: func(cmd *cobra.Command, args []string) error {
2727
if len(args) != 1 {
28-
cmd.Usage()
28+
_ = cmd.Usage()
2929
os.Exit(1)
3030
}
3131
shell := args[0]
32+
var err error
3233
switch strings.ToLower(shell) {
3334
case "bash":
34-
rootCmd.GenBashCompletion(os.Stdout)
35+
err = rootCmd.GenBashCompletion(os.Stdout)
3536
case "fish":
36-
rootCmd.GenFishCompletion(os.Stdout, true)
37+
err = rootCmd.GenFishCompletion(os.Stdout, true)
3738
case "powershell":
38-
rootCmd.GenPowerShellCompletion(os.Stdout)
39+
err = rootCmd.GenPowerShellCompletion(os.Stdout)
3940
case "zsh":
40-
rootCmd.GenPowerShellCompletion(os.Stdout)
41+
err = rootCmd.GenZshCompletion(os.Stdout)
4142
default:
4243
fmt.Printf("Unsupported SHELL value: '%s'\n", shell)
43-
cmd.Usage()
44-
os.Exit(1)
44+
return cmd.Usage()
4545
}
46+
47+
return err
4648
},
4749
}
4850
return cmd

cmd/devcontainer/config.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package main
33
import (
44
"encoding/json"
55
"fmt"
6-
"os"
76

87
"github.com/spf13/cobra"
98
"github.com/stuartleeks/devcontainer-cli/internal/pkg/config"
@@ -22,14 +21,14 @@ func createConfigShowCommand() *cobra.Command {
2221
Use: "show",
2322
Short: "show the current config",
2423
Long: "load the current config and print it out",
25-
Run: func(cmd *cobra.Command, args []string) {
24+
RunE: func(cmd *cobra.Command, args []string) error {
2625
c := config.GetAll()
2726
jsonConfig, err := json.MarshalIndent(c, "", " ")
2827
if err != nil {
29-
fmt.Printf("Error converting to JSON: %s\n", err)
30-
os.Exit(1)
28+
return fmt.Errorf("Error converting to JSON: %s\n", err)
3129
}
3230
fmt.Println(string(jsonConfig))
31+
return nil
3332
},
3433
}
3534
return cmd
@@ -39,12 +38,12 @@ func createConfigWriteCommand() *cobra.Command {
3938
Use: "write",
4039
Short: "write config",
4140
Long: "Write out the config file to ~/.devcontainer-cli/devcontainer-cli.json",
42-
Run: func(cmd *cobra.Command, args []string) {
41+
RunE: func(cmd *cobra.Command, args []string) error {
4342
if err := config.SaveConfig(); err != nil {
44-
fmt.Printf("Error saving config: %s\n", err)
45-
} else {
46-
fmt.Println("Config saved")
43+
return fmt.Errorf("Error saving config: %s\n", err)
4744
}
45+
fmt.Println("Config saved")
46+
return nil
4847
},
4948
}
5049
return cmd

cmd/devcontainer/devcontainer.go

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,14 @@ func createListCommand() *cobra.Command {
1818
Use: "list",
1919
Short: "List devcontainers",
2020
Long: "Lists running devcontainers",
21-
Run: func(cmd *cobra.Command, args []string) {
21+
RunE: func(cmd *cobra.Command, args []string) error {
2222
if listIncludeContainerNames && listVerbose {
2323
fmt.Println("Can't use both verbose and include-container-names")
2424
os.Exit(1)
2525
}
2626
devcontainers, err := devcontainers.ListDevcontainers()
2727
if err != nil {
28-
fmt.Printf("Error: %v", err)
29-
os.Exit(1)
28+
return err
3029
}
3130
if listVerbose {
3231
sort.Slice(devcontainers, func(i, j int) bool { return devcontainers[i].DevcontainerName < devcontainers[j].DevcontainerName })
@@ -42,7 +41,7 @@ func createListCommand() *cobra.Command {
4241
for _, devcontainer := range devcontainers {
4342
fmt.Fprintf(w, "%s\t%s\n", devcontainer.DevcontainerName, devcontainer.ContainerName)
4443
}
45-
return
44+
return nil
4645
}
4746
names := []string{}
4847
for _, devcontainer := range devcontainers {
@@ -55,6 +54,7 @@ func createListCommand() *cobra.Command {
5554
for _, name := range names {
5655
fmt.Println(name)
5756
}
57+
return nil
5858
},
5959
}
6060
cmdList.Flags().BoolVar(&listIncludeContainerNames, "include-container-names", false, "Also include container names in the list")
@@ -67,18 +67,16 @@ func createExecCommand() *cobra.Command {
6767
Use: "exec DEVCONTAINER_NAME COMMAND [args...]",
6868
Short: "Execute a command in a devcontainer",
6969
Long: "Execute a command in a devcontainer, similar to `docker exec`.",
70-
Run: func(cmd *cobra.Command, args []string) {
70+
RunE: func(cmd *cobra.Command, args []string) error {
7171

7272
if len(args) < 2 {
73-
cmd.Usage()
74-
os.Exit(1)
73+
return cmd.Usage()
7574
}
7675

7776
devcontainerName := args[0]
7877
devcontainers, err := devcontainers.ListDevcontainers()
7978
if err != nil {
80-
fmt.Printf("Error: %v", err)
81-
os.Exit(1)
79+
return err
8280
}
8381

8482
containerID := ""
@@ -89,11 +87,7 @@ func createExecCommand() *cobra.Command {
8987
}
9088
}
9189
if containerID == "" {
92-
cmd.Usage()
93-
if err != nil {
94-
fmt.Printf("Error: %v", err)
95-
}
96-
os.Exit(1)
90+
return cmd.Usage()
9791
}
9892

9993
dockerArgs := []string{"exec", "-it", containerID}
@@ -105,12 +99,13 @@ func createExecCommand() *cobra.Command {
10599

106100
err = dockerCmd.Start()
107101
if err != nil {
108-
fmt.Printf("Exec: start error: %s\n", err)
102+
return fmt.Errorf("Exec: start error: %s\n", err)
109103
}
110104
err = dockerCmd.Wait()
111105
if err != nil {
112-
fmt.Printf("Exec: wait error: %s\n", err)
106+
return fmt.Errorf("Exec: wait error: %s\n", err)
113107
}
108+
return nil
114109
},
115110
Args: cobra.ArbitraryArgs,
116111
DisableFlagParsing: true,

cmd/devcontainer/main.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package main
22

33
import (
4+
"fmt"
5+
"os"
6+
47
"github.com/spf13/cobra"
58
"github.com/stuartleeks/devcontainer-cli/internal/pkg/update"
69
)
@@ -30,5 +33,9 @@ func main() {
3033
rootCmd.AddCommand(createUpdateCommand())
3134
rootCmd.AddCommand(createVersionCommand())
3235

33-
rootCmd.Execute()
36+
err := rootCmd.Execute()
37+
if err != nil {
38+
fmt.Printf("Error running command:\n\t%s\n", err)
39+
os.Exit(1)
40+
}
3441
}

cmd/devcontainer/template.go

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,17 @@ func createTemplateListCommand() *cobra.Command {
2929
Use: "list",
3030
Short: "list templates",
3131
Long: "List devcontainer templates",
32-
Run: func(cmd *cobra.Command, args []string) {
32+
RunE: func(cmd *cobra.Command, args []string) error {
3333

3434
templates, err := devcontainers.GetTemplates()
3535
if err != nil {
36-
fmt.Println(err)
37-
os.Exit(1)
36+
return err
3837
}
3938

4039
for _, template := range templates {
4140
fmt.Println(template.Name)
4241
}
42+
return nil
4343
},
4444
}
4545
return cmd
@@ -50,37 +50,34 @@ func createTemplateAddCommand() *cobra.Command {
5050
Use: "add TEMPLATE_NAME",
5151
Short: "add devcontainer from template",
5252
Long: "Add a devcontainer definition to the current folder using the specified template",
53-
Run: func(cmd *cobra.Command, args []string) {
53+
RunE: func(cmd *cobra.Command, args []string) error {
5454

5555
if len(args) != 1 {
56-
cmd.Usage()
57-
os.Exit(1)
56+
return cmd.Usage()
5857
}
5958
name := args[0]
6059

6160
template, err := devcontainers.GetTemplateByName(name)
6261
if err != nil {
63-
fmt.Println(err)
64-
os.Exit(1)
62+
return err
6563
}
6664
if template == nil {
6765
fmt.Printf("Template '%s' not found\n", name)
6866
}
6967

7068
info, err := os.Stat("./.devcontainer")
7169
if info != nil && err == nil {
72-
fmt.Println("Current folder already contains a .devcontainer folder - exiting")
73-
os.Exit(1)
70+
return fmt.Errorf("Current folder already contains a .devcontainer folder - exiting")
7471
}
7572

7673
currentDirectory, err := os.Getwd()
7774
if err != nil {
78-
fmt.Printf("Error reading current directory: %s\n", err)
75+
return fmt.Errorf("Error reading current directory: %s\n", err)
7976
}
8077
if err = ioutil2.CopyFolder(template.Path, currentDirectory+"/.devcontainer"); err != nil {
81-
fmt.Printf("Error copying folder: %s\n", err)
82-
os.Exit(1)
78+
return fmt.Errorf("Error copying folder: %s\n", err)
8379
}
80+
return err
8481
},
8582
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
8683
// only completing the first arg (template name)
@@ -108,43 +105,39 @@ func createTemplateAddLinkCommand() *cobra.Command {
108105
Use: "add-link TEMPLATE_NAME",
109106
Short: "add-link devcontainer from template",
110107
Long: "Symlink a devcontainer definition to the current folder using the specified template",
111-
Run: func(cmd *cobra.Command, args []string) {
108+
RunE: func(cmd *cobra.Command, args []string) error {
112109

113110
if len(args) != 1 {
114-
cmd.Usage()
115-
os.Exit(1)
111+
return cmd.Usage()
116112
}
117113
name := args[0]
118114

119115
template, err := devcontainers.GetTemplateByName(name)
120116
if err != nil {
121-
fmt.Println(err)
122-
os.Exit(1)
117+
return err
123118
}
124119
if template == nil {
125-
fmt.Printf("Template '%s' not found\n", name)
120+
return fmt.Errorf("Template '%s' not found\n", name)
126121
}
127122

128123
info, err := os.Stat("./.devcontainer")
129124
if info != nil && err == nil {
130-
fmt.Println("Current folder already contains a .devcontainer folder - exiting")
131-
os.Exit(1)
125+
return fmt.Errorf("Current folder already contains a .devcontainer folder - exiting")
132126
}
133127

134128
currentDirectory, err := os.Getwd()
135129
if err != nil {
136-
fmt.Printf("Error reading current directory: %s\n", err)
130+
return fmt.Errorf("Error reading current directory: %s\n", err)
137131
}
138132
if err = ioutil2.LinkFolder(template.Path, currentDirectory+"/.devcontainer"); err != nil {
139-
fmt.Printf("Error linking folder: %s\n", err)
140-
os.Exit(1)
133+
return fmt.Errorf("Error linking folder: %s\n", err)
141134
}
142135

143136
content := []byte("*\n")
144137
if err := ioutil.WriteFile(currentDirectory+"/.devcontainer/.gitignore", content, 0644); err != nil { // -rw-r--r--
145-
fmt.Printf("Error writing .gitignore: %s\n", err)
146-
os.Exit(1)
138+
return fmt.Errorf("Error writing .gitignore: %s\n", err)
147139
}
140+
return err
148141
},
149142
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
150143
// only completing the first arg (template name)

cmd/devcontainer/update.go

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package main
33
import (
44
"bufio"
55
"fmt"
6-
"log"
76
"os"
87

98
"github.com/rhysd/go-github-selfupdate/selfupdate"
@@ -23,43 +22,42 @@ func createUpdateCommand() *cobra.Command {
2322
PersistentPreRun: func(cmd *cobra.Command, args []string) {
2423
// do nothing - suppress root PersistentPreRun which does periodic update check
2524
},
26-
Run: func(cmd *cobra.Command, args []string) {
25+
RunE: func(cmd *cobra.Command, args []string) error {
2726
latest, err := update.CheckForUpdate(version)
2827
if err != nil {
29-
log.Println("Error occurred while checking for updates:", err)
30-
return
28+
return fmt.Errorf("Error occurred while checking for updates: %v", err)
3129
}
3230

3331
if latest == nil {
3432
fmt.Println("No updates available")
35-
return
33+
return nil
3634
}
3735

3836
fmt.Printf("\n\n UPDATE AVAILABLE: %s \n \n Release notes: %s\n", latest.Version, latest.ReleaseNotes)
3937

4038
if checkOnly {
41-
return
39+
return nil
4240
}
4341

4442
fmt.Print("Do you want to update? (y/n): ")
4543
if !yes {
4644
input, err := bufio.NewReader(os.Stdin).ReadString('\n')
4745
if err != nil || (input != "y\n" && input != "y\r\n") {
4846
// error or something other than `y`
49-
return
47+
return err
5048
}
5149
}
5250
fmt.Println("Applying...")
5351

5452
exe, err := os.Executable()
5553
if err != nil {
56-
log.Panicln("Could not locate executable path")
54+
return fmt.Errorf("Could not locate executable path: %v", err)
5755
}
5856
if err := selfupdate.UpdateTo(latest.AssetURL, exe); err != nil {
59-
log.Panicln("Error occurred while updating binary:", err)
57+
return fmt.Errorf("Error occurred while updating binary: %v", err)
6058
}
61-
log.Println("Successfully updated to version", latest.Version)
62-
59+
fmt.Printf("Successfully updated to version %s\n", latest.Version)
60+
return nil
6361
},
6462
}
6563
cmd.Flags().BoolVar(&checkOnly, "check-only", false, "Check for an update without applying")

cmd/devcontainer/version.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,13 @@ func createVersionCommand() *cobra.Command {
1414
Use: "version",
1515
Short: "show version",
1616
Long: "Show version",
17-
Run: func(cmd *cobra.Command, args []string) {
17+
RunE: func(cmd *cobra.Command, args []string) error {
1818
if verbose {
1919
fmt.Printf("devcontainer version %s\nBuilt %s (commit %s)\n%s\n\n", version, date, commit, goversion)
20-
return
20+
return nil
2121
}
2222
fmt.Println(version)
23+
return nil
2324
},
2425
}
2526
cmd.Flags().BoolVarP(&verbose, "verbose", "v", false, "Verbose output")

0 commit comments

Comments
 (0)