Skip to content

Commit b3a17c3

Browse files
committed
chore: address go linting
1 parent 82a91e3 commit b3a17c3

13 files changed

Lines changed: 67 additions & 51 deletions

File tree

cmd/config.go

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func parseLagoonConfig(flags pflag.FlagSet) LagoonConfigFlags {
3939
})
4040
jsonStr, _ := json.Marshal(configMap)
4141
parsedFlags := LagoonConfigFlags{}
42-
json.Unmarshal(jsonStr, &parsedFlags)
42+
_ = json.Unmarshal(jsonStr, &parsedFlags)
4343
return parsedFlags
4444
}
4545

@@ -55,12 +55,10 @@ var configDefaultCmd = &cobra.Command{
5555
Use: "default",
5656
Aliases: []string{"d"},
5757
Short: "Set the default Lagoon to use",
58-
Run: func(cmd *cobra.Command, args []string) {
58+
RunE: func(cmd *cobra.Command, args []string) error {
5959
lagoonConfig := parseLagoonConfig(*cmd.Flags())
6060
if lagoonConfig.Lagoon == "" {
61-
fmt.Println("Not enough arguments")
62-
cmd.Help()
63-
os.Exit(1)
61+
return fmt.Errorf("not enough arguments")
6462
}
6563
lagoonCLIConfig.Default = strings.TrimSpace(string(lagoonConfig.Lagoon))
6664
contextExists := false
@@ -70,8 +68,7 @@ var configDefaultCmd = &cobra.Command{
7068
}
7169
}
7270
if !contextExists {
73-
fmt.Println(fmt.Printf("Chosen context '%s' doesn't exist in config file", lagoonCLIConfig.Current))
74-
os.Exit(1)
71+
return fmt.Errorf("chosen context '%s' doesn't exist in config file", lagoonCLIConfig.Current)
7572
}
7673
err := writeLagoonConfig(&lagoonCLIConfig, filepath.Join(configFilePath, configName+configExtension))
7774
handleError(err)
@@ -84,6 +81,7 @@ var configDefaultCmd = &cobra.Command{
8481
}
8582
r := output.RenderResult(resultData, outputOptions)
8683
fmt.Fprintf(cmd.OutOrStdout(), "%s", r)
84+
return nil
8785
},
8886
}
8987

cmd/helpers_test.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,11 @@ func Test_flagStringNullValueOrNil(t *testing.T) {
157157
for k, v := range tt.args.flags {
158158
flags.StringP(k, "", "", "")
159159
if v != nil {
160-
flags.Set(k, v.(string))
160+
err := flags.Set(k, v.(string))
161+
if (err != nil) != tt.wantErr {
162+
t.Errorf("flagStringNullValueOrNil() error setting flags = %v, wantErr %v", err, tt.wantErr)
163+
return
164+
}
161165
}
162166
}
163167
got, err := flagStringNullValueOrNil(flags, tt.args.flag)

cmd/list.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1081,7 +1081,7 @@ var listOrganizationDeployTargetsCmd = &cobra.Command{
10811081
},
10821082
}
10831083

1084-
var ListOrganizationUsersCmd = &cobra.Command{
1084+
var listOrganizationUsersCmd = &cobra.Command{
10851085
Use: "organization-users",
10861086
Aliases: []string{"org-u"},
10871087
Short: "List users in an organization",
@@ -1264,7 +1264,7 @@ func init() {
12641264
listCmd.AddCommand(listAllUsersCmd)
12651265
listCmd.AddCommand(listUsersGroupsCmd)
12661266
listCmd.AddCommand(listOrganizationProjectsCmd)
1267-
listCmd.AddCommand(ListOrganizationUsersCmd)
1267+
listCmd.AddCommand(listOrganizationUsersCmd)
12681268
listCmd.AddCommand(listOrganizationAdminsCmd)
12691269
listCmd.AddCommand(listOrganizationGroupsCmd)
12701270
listCmd.AddCommand(listOrganizationDeployTargetsCmd)
@@ -1277,7 +1277,7 @@ func init() {
12771277
listGroupProjectsCmd.Flags().BoolP("all-projects", "", false, "All projects")
12781278
listVariablesCmd.Flags().BoolP("reveal", "", false, "Reveal the variable values")
12791279
listOrganizationProjectsCmd.Flags().StringP("organization-name", "O", "", "Name of the organization to list associated projects for")
1280-
ListOrganizationUsersCmd.Flags().StringP("organization-name", "O", "", "Name of the organization to list associated users for")
1280+
listOrganizationUsersCmd.Flags().StringP("organization-name", "O", "", "Name of the organization to list associated users for")
12811281
listOrganizationAdminsCmd.Flags().StringP("organization-name", "O", "", "Name of the organization to list associated users for")
12821282
listOrganizationGroupsCmd.Flags().StringP("organization-name", "O", "", "Name of the organization to list associated groups for")
12831283
listOrganizationDeployTargetsCmd.Flags().StringP("organization-name", "O", "", "Name of the organization to list associated deploy targets for")

cmd/login.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,12 @@ func retrieveTokenViaSsh() (string, error) {
157157
HostKeyCallback: hkcb,
158158
HostKeyAlgorithms: hkalgo,
159159
}
160-
defer closeSSHAgent()
160+
defer func() {
161+
err = closeSSHAgent()
162+
if err != nil {
163+
fmt.Fprintf(os.Stderr, "error closing ssh agent:%v\n", err)
164+
}
165+
}()
161166

162167
conn, err := ssh.Dial("tcp", sshHost, config)
163168
if err != nil {

cmd/logs.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,12 @@ var logsCmd = &cobra.Command{
163163
if err != nil {
164164
return fmt.Errorf("couldn't get SSH client config: %v", err)
165165
}
166-
defer closeSSHAgent()
166+
defer func() {
167+
err = closeSSHAgent()
168+
if err != nil {
169+
fmt.Fprintf(os.Stderr, "error closing ssh agent:%v\n", err)
170+
}
171+
}()
167172
// start SSH log streaming session
168173
err = lagoonssh.LogStream(sshConfig, sshHost, sshPort, argv)
169174
if err != nil {

cmd/root.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ func init() {
155155
rootCmd.Flags().BoolVarP(&versionFlag, "version", "", false, "Version information")
156156
rootCmd.Flags().BoolVarP(&docsFlag, "docs", "", false, "Generate docs")
157157

158-
rootCmd.Flags().MarkHidden("docs")
158+
_ = rootCmd.Flags().MarkHidden("docs")
159159

160160
rootCmd.SetUsageTemplate(`Usage:{{if .Runnable}}
161161
{{.UseLine}}{{end}}{{if .HasAvailableSubCommands}}

cmd/users.go

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -27,25 +27,25 @@ func parseSSHKeyFile(sshPubKey string, keyName string, keyValue string, userEmai
2727
var err error
2828

2929
// will fail if value is not right
30-
if strings.EqualFold(string(splitKey[0]), "ssh-rsa") {
30+
switch splitKey[0] {
31+
case "ssh-rsa":
3132
keyType = schema.SSHRsa
32-
} else if strings.EqualFold(string(splitKey[0]), "ssh-ed25519") {
33+
case "ssh-ed25519":
3334
keyType = schema.SSHEd25519
34-
} else if strings.EqualFold(string(splitKey[0]), "ecdsa-sha2-nistp256") {
35+
case "ecdsa-sha2-nistp256":
3536
keyType = schema.SSHECDSA256
36-
} else if strings.EqualFold(string(splitKey[0]), "ecdsa-sha2-nistp384") {
37+
case "ecdsa-sha2-nistp384":
3738
keyType = schema.SSHECDSA384
38-
} else if strings.EqualFold(string(splitKey[0]), "ecdsa-sha2-nistp521") {
39+
case "ecdsa-sha2-nistp521":
3940
keyType = schema.SSHECDSA521
40-
} else {
41-
// return error stating key type not supported
41+
default:
4242
keyType = schema.SSHRsa
43-
err = fmt.Errorf(fmt.Sprintf("SSH key type %s not supported", splitKey[0]))
43+
err = fmt.Errorf("SSH key type %s not supported", splitKey[0])
4444
}
4545

4646
// if the sshkey has a comment/name in it, we can use that
4747
if keyName == "" && len(splitKey) == 3 {
48-
//strip new line
48+
// strip new line
4949
keyName = stripNewLines(splitKey[2])
5050
} else if keyName == "" && len(splitKey) == 2 {
5151
keyName = userEmail
@@ -373,7 +373,7 @@ var updateUserCmd = &cobra.Command{
373373
}
374374

375375
var getUserKeysCmd = &cobra.Command{
376-
//@TODO: once individual user interaction comes in, this will need to be adjusted
376+
// @TODO: once individual user interaction comes in, this will need to be adjusted
377377
Use: "user-sshkeys",
378378
Aliases: []string{"us"},
379379
Short: "Get a user's SSH keys",
@@ -434,7 +434,7 @@ var getUserKeysCmd = &cobra.Command{
434434
}
435435

436436
var getAllUserKeysCmd = &cobra.Command{
437-
//@TODO: once individual user interaction comes in, this will need to be adjusted
437+
// @TODO: once individual user interaction comes in, this will need to be adjusted
438438
Use: "all-user-sshkeys",
439439
Aliases: []string{"aus"},
440440
Short: "Get all user SSH keys",
@@ -479,10 +479,10 @@ var getAllUserKeysCmd = &cobra.Command{
479479
var data []output.Data
480480
for _, userData := range userGroups {
481481
keyID := strconv.Itoa(int(userData.SSHKey.ID))
482-
userEmail := returnNonEmptyString(strings.Replace(userData.UserEmail, " ", "_", -1))
483-
keyName := returnNonEmptyString(strings.Replace(userData.SSHKey.Name, " ", "_", -1))
484-
keyValue := returnNonEmptyString(strings.Replace(userData.SSHKey.KeyValue, " ", "_", -1))
485-
keyType := returnNonEmptyString(strings.Replace(string(userData.SSHKey.KeyType), " ", "_", -1))
482+
userEmail := returnNonEmptyString(strings.ReplaceAll(userData.UserEmail, " ", "_"))
483+
keyName := returnNonEmptyString(strings.ReplaceAll(userData.SSHKey.Name, " ", "_"))
484+
keyValue := returnNonEmptyString(strings.ReplaceAll(userData.SSHKey.KeyValue, " ", "_"))
485+
keyType := returnNonEmptyString(strings.ReplaceAll(string(userData.SSHKey.KeyType), " ", "_"))
486486
data = append(data, []string{
487487
keyID,
488488
userEmail,

pkg/app/app.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func (project *LagoonProject) ReadConfig() error {
4343
}
4444
sourceCompose, _ := os.ReadFile(dockerComposeFilepath)
4545
var dockerCompose LagoonDockerCompose
46-
yaml.Unmarshal(sourceCompose, &dockerCompose)
46+
_ = yaml.Unmarshal(sourceCompose, &dockerCompose)
4747
// Reset the name based on the docker-compose.yml file.
4848
project.Name = dockerCompose.LagoonProject
4949

@@ -83,8 +83,8 @@ func getProjectFromPath(path string) (LagoonProject, error) {
8383
}
8484
app.Name = filepath.Base(appDir)
8585
app.Dir = appDir
86-
app.ReadConfig()
87-
return app, nil
86+
err = app.ReadConfig()
87+
return app, err
8888
}
8989

9090
func findLocalProjectRoot(path string) (string, error) {

pkg/lagoon/ssh/main.go

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,12 @@ func InteractiveSSH(lagoon map[string]string, sshService string, sshContainer st
6666
if err != nil {
6767
return err
6868
}
69-
defer term.Restore(fileDescriptor, originalState)
69+
defer func() {
70+
err = term.Restore(fileDescriptor, originalState)
71+
if err != nil {
72+
fmt.Fprintf(os.Stderr, "error restoring ssh terminal:%v\n", err)
73+
}
74+
}()
7075
termWidth, termHeight, err := term.GetSize(fileDescriptor)
7176
if err != nil {
7277
return err
@@ -85,23 +90,22 @@ func InteractiveSSH(lagoon map[string]string, sshService string, sshContainer st
8590
}
8691
err = session.Start(connString)
8792
if err != nil {
88-
return fmt.Errorf("Failed to start shell: " + err.Error())
93+
return fmt.Errorf("failed to start shell: %s", err.Error())
8994
}
90-
session.Wait()
91-
return nil
95+
return session.Wait()
9296
}
9397

9498
// RunSSHCommand .
9599
func RunSSHCommand(lagoon map[string]string, sshService string, sshContainer string, command string, config *ssh.ClientConfig) error {
96100
client, err := ssh.Dial("tcp", lagoon["hostname"]+":"+lagoon["port"], config)
97101
if err != nil {
98-
return fmt.Errorf("Failed to dial: " + err.Error() + "\nCheck that the project or environment you are trying to connect to exists")
102+
return fmt.Errorf("failed to dial: %s\nCheck that the project or environment you are trying to connect to exists", err.Error())
99103
}
100104

101105
// start the session
102106
session, err := client.NewSession()
103107
if err != nil {
104-
return fmt.Errorf("Failed to create session: " + err.Error())
108+
return fmt.Errorf("failed to create session: %s", err.Error())
105109
}
106110
defer session.Close()
107111
session.Stdout = os.Stdout
@@ -118,7 +122,12 @@ func RunSSHCommand(lagoon map[string]string, sshService string, sshContainer str
118122
if err != nil {
119123
return err
120124
}
121-
defer term.Restore(fileDescriptor, originalState)
125+
defer func() {
126+
err = term.Restore(fileDescriptor, originalState)
127+
if err != nil {
128+
fmt.Fprintf(os.Stderr, "error restoring ssh terminal:%v\n", err)
129+
}
130+
}()
122131
termWidth, termHeight, err := term.GetSize(fileDescriptor)
123132
if err != nil {
124133
return err
@@ -188,7 +197,7 @@ func InteractiveKnownHosts(userPath, host string, ignorehost, accept bool) (ssh.
188197
f, ferr := os.OpenFile(filePath, os.O_APPEND|os.O_WRONLY, 0600)
189198
if ferr == nil {
190199
defer f.Close()
191-
response := "n"
200+
var response string
192201
if accept {
193202
response = "y"
194203
} else {

pkg/output/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ func RenderOutput(data Table, opts Options) string {
118118
for _, dataValues := range data.Data {
119119
jsonData := make(map[string]interface{})
120120
for indexID, dataValue := range dataValues {
121-
dataHeader := strings.Replace(strings.ToLower(data.Header[indexID]), " ", "-", -1)
121+
dataHeader := strings.ReplaceAll(strings.ToLower(data.Header[indexID]), " ", "-")
122122
jsonData[dataHeader] = dataValue
123123
}
124124
rawData = append(rawData, jsonData)

0 commit comments

Comments
 (0)