Skip to content

Commit

Permalink
chore: code formatting, tidy up errors, remove unused functions (#338)
Browse files Browse the repository at this point in the history
  • Loading branch information
shreddedbacon authored Apr 10, 2024
1 parent 3fd31de commit 86458ac
Show file tree
Hide file tree
Showing 33 changed files with 186 additions and 283 deletions.
26 changes: 2 additions & 24 deletions cmd/config.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package cmd

import (
"bufio"
"context"
"encoding/json"
"fmt"
"io"
"os"
"path/filepath"
"sort"
Expand Down Expand Up @@ -146,7 +144,7 @@ var configAddCmd = &cobra.Command{
RunE: func(cmd *cobra.Command, args []string) error {
lagoonConfig := parseLagoonConfig(*cmd.Flags())
if lagoonConfig.Lagoon == "" {
return fmt.Errorf("Missing arguments: Lagoon name is not defined")
return fmt.Errorf("missing arguments: Lagoon name is not defined")
}

if lagoonConfig.Hostname != "" && lagoonConfig.Port != "" && lagoonConfig.GraphQL != "" {
Expand Down Expand Up @@ -193,7 +191,7 @@ var configAddCmd = &cobra.Command{
},
}, outputOptions)
} else {
return fmt.Errorf("Must have Hostname, Port, and GraphQL endpoint")
return fmt.Errorf("must have Hostname, Port, and GraphQL endpoint")
}
return nil
},
Expand Down Expand Up @@ -359,26 +357,6 @@ func readLagoonConfig(lc *lagoon.Config, file string) error {

}

func analyze(file string) error {
handle, err := os.Open(file)

if err != nil {
return err
}
defer handle.Close()
return doSomething(handle)
}

func doSomething(handle io.Reader) error {
scanner := bufio.NewScanner(handle)
for scanner.Scan() {
// Do something with line
d := scanner.Text()
fmt.Println(d)
}
return nil
}

// functions to handle read/write of configuration file

// writeLagoonConfig writes the lagoon config to specified file.
Expand Down
13 changes: 4 additions & 9 deletions cmd/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ use 'lagoon deploy latest' instead`,
return err
}
if cmdProjectName == "" || branch == "" {
return fmt.Errorf("Missing arguments: Project name or branch name is not defined")
return fmt.Errorf("missing arguments: Project name or branch name is not defined")
}

buildVarStrings, err := cmd.Flags().GetStringArray("buildvar")
Expand Down Expand Up @@ -109,7 +109,7 @@ var deployPromoteCmd = &cobra.Command{
return err
}
if cmdProjectName == "" || sourceEnvironment == "" || destinationEnvironment == "" {
return fmt.Errorf("Missing arguments: Project name, source environment, or destination environment is not defined")
return fmt.Errorf("missing arguments: Project name, source environment, or destination environment is not defined")
}

buildVarStrings, err := cmd.Flags().GetStringArray("buildvar")
Expand Down Expand Up @@ -176,7 +176,7 @@ This environment should already exist in lagoon. It is analogous with the 'Deplo
}

if cmdProjectName == "" || cmdProjectEnvironment == "" {
return fmt.Errorf("Missing arguments: Project name or environment name is not defined")
return fmt.Errorf("missing arguments: Project name or environment name is not defined")
}
if yesNo(fmt.Sprintf("You are attempting to deploy the latest environment '%s' for project '%s', are you sure?", cmdProjectEnvironment, cmdProjectName)) {
current := lagoonCLIConfig.Current
Expand Down Expand Up @@ -246,7 +246,7 @@ This pullrequest may not already exist as an environment in lagoon.`,
}
if cmdProjectName == "" || prTitle == "" || prNumber == 0 || baseBranchName == "" ||
baseBranchRef == "" || headBranchName == "" || headBranchRef == "" {
return fmt.Errorf("Missing arguments: Project name, title, number, baseBranchName, baseBranchRef, headBranchName, or headBranchRef is not defined")
return fmt.Errorf("missing arguments: Project name, title, number, baseBranchName, baseBranchRef, headBranchName, or headBranchRef is not defined")
}
buildVarStrings, err := cmd.Flags().GetStringArray("buildvar")
if err != nil {
Expand Down Expand Up @@ -292,11 +292,6 @@ This pullrequest may not already exist as an environment in lagoon.`,
},
}

var (
promoteSourceEnv string
promoteDestEnv string
)

func init() {
deployCmd.AddCommand(deployBranchCmd)
deployCmd.AddCommand(deployPromoteCmd)
Expand Down
6 changes: 3 additions & 3 deletions cmd/deploytarget.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,13 @@ var addDeployTargetCmd = &cobra.Command{
}

if name == "" {
return fmt.Errorf("Missing arguments: name is not defined")
return fmt.Errorf("missing arguments: name is not defined")
}
if token == "" {
return fmt.Errorf("Missing arguments: token is not defined")
return fmt.Errorf("missing arguments: token is not defined")
}
if consoleURL == "" {
return fmt.Errorf("Missing arguments: console-url is not defined")
return fmt.Errorf("missing arguments: console-url is not defined")
}

addDeployTarget := &schema.AddDeployTargetInput{
Expand Down
12 changes: 6 additions & 6 deletions cmd/deploytargetconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,16 @@ var addDeployTargetConfigCmd = &cobra.Command{
}

if cmdProjectName == "" {
return fmt.Errorf("Missing arguments: project is a required flag")
return fmt.Errorf("missing arguments: project is a required flag")
}
if deploytarget == 0 {
return fmt.Errorf("Missing arguments: deploytarget id is a required flag")
return fmt.Errorf("missing arguments: deploytarget id is a required flag")
}
if pullrequests == "" {
return fmt.Errorf("Missing arguments: pullrequests is a required flag")
return fmt.Errorf("missing arguments: pullrequests is a required flag")
}
if branches == "" {
return fmt.Errorf("Missing arguments: branches is a required flag")
return fmt.Errorf("missing arguments: branches is a required flag")
}
current := lagoonCLIConfig.Current
token := lagoonCLIConfig.Lagoons[current].Token
Expand Down Expand Up @@ -150,7 +150,7 @@ var updateDeployTargetConfigCmd = &cobra.Command{
return err
}
if id == 0 {
return fmt.Errorf("Missing arguments: deploytarget config id is not defined")
return fmt.Errorf("missing arguments: deploytarget config id is not defined")
}
current := lagoonCLIConfig.Current
lc := client.New(
Expand Down Expand Up @@ -226,7 +226,7 @@ var deleteDeployTargetConfigCmd = &cobra.Command{
return err
}
if id == 0 {
return fmt.Errorf("Missing arguments: deploytarget config id is not defined")
return fmt.Errorf("missing arguments: deploytarget config id is not defined")
}
current := lagoonCLIConfig.Current
lc := client.New(
Expand Down
4 changes: 2 additions & 2 deletions cmd/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ var listBackupsCmd = &cobra.Command{
return err
}
if cmdProjectEnvironment == "" || cmdProjectName == "" {
return fmt.Errorf("Missing arguments: Project name or environment name is not defined")
return fmt.Errorf("missing arguments: Project name or environment name is not defined")
}
current := lagoonCLIConfig.Current
lc := client.New(
Expand Down Expand Up @@ -246,7 +246,7 @@ This returns a direct URL to the backup, this is a signed download link with a l
return err
}
if cmdProjectEnvironment == "" || cmdProjectName == "" {
return fmt.Errorf("Missing arguments: Project name or environment name is not defined")
return fmt.Errorf("missing arguments: Project name or environment name is not defined")
}
current := lagoonCLIConfig.Current
lc := client.New(
Expand Down
102 changes: 50 additions & 52 deletions cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"strconv"

"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/uselagoon/lagoon-cli/internal/lagoon"
"github.com/uselagoon/lagoon-cli/internal/lagoon/client"
"github.com/uselagoon/lagoon-cli/internal/schema"
Expand All @@ -26,19 +25,6 @@ type ListFlags struct {
Reveal bool `json:"reveal,omitempty"`
}

func parseListFlags(flags pflag.FlagSet) ListFlags {
configMap := make(map[string]interface{})
flags.VisitAll(func(f *pflag.Flag) {
if flags.Changed(f.Name) {
configMap[f.Name] = f.Value
}
})
jsonStr, _ := json.Marshal(configMap)
parsedFlags := ListFlags{}
json.Unmarshal(jsonStr, &parsedFlags)
return parsedFlags
}

var listCmd = &cobra.Command{
Use: "list",
Short: "List projects, environments, deployments, variables or notifications",
Expand Down Expand Up @@ -408,10 +394,10 @@ Without a group name, this query may time out in large Lagoon installs.`,
}
for _, member := range groupMembers.Members {
data = append(data, []string{
returnNonEmptyString(fmt.Sprintf("%s", groupMembers.ID)),
returnNonEmptyString(fmt.Sprintf("%s", groupMembers.Name)),
returnNonEmptyString(fmt.Sprintf("%s", member.User.Email)),
returnNonEmptyString(fmt.Sprintf("%s", member.Role)),
returnNonEmptyString(groupMembers.ID.String()),
returnNonEmptyString(groupMembers.Name),
returnNonEmptyString(member.User.Email),
returnNonEmptyString(string(member.Role)),
})
}
} else {
Expand All @@ -423,10 +409,10 @@ Without a group name, this query may time out in large Lagoon installs.`,
for _, group := range *groupMembers {
for _, member := range group.Members {
data = append(data, []string{
returnNonEmptyString(fmt.Sprintf("%s", group.ID)),
returnNonEmptyString(fmt.Sprintf("%s", group.Name)),
returnNonEmptyString(fmt.Sprintf("%s", member.User.Email)),
returnNonEmptyString(fmt.Sprintf("%s", member.Role)),
returnNonEmptyString(group.ID.String()),
returnNonEmptyString(group.Name),
returnNonEmptyString(member.User.Email),
returnNonEmptyString(string(member.Role)),
})
}
}
Expand Down Expand Up @@ -475,11 +461,11 @@ This query can take a long time to run if there are a lot of users.`,
data := []output.Data{}
for _, user := range *allUsers {
data = append(data, []string{
returnNonEmptyString(fmt.Sprintf("%s", user.ID)),
returnNonEmptyString(fmt.Sprintf("%s", user.Email)),
returnNonEmptyString(fmt.Sprintf("%s", user.FirstName)),
returnNonEmptyString(fmt.Sprintf("%s", user.LastName)),
returnNonEmptyString(fmt.Sprintf("%s", user.Comment)),
returnNonEmptyString(user.ID.String()),
returnNonEmptyString(user.Email),
returnNonEmptyString(user.FirstName),
returnNonEmptyString(user.LastName),
returnNonEmptyString(user.Comment),
})
}
dataMain := output.Table{
Expand Down Expand Up @@ -509,7 +495,7 @@ var listUsersGroupsCmd = &cobra.Command{
return err
}
if emailAddress == "" {
return fmt.Errorf("Missing arguments: email address is not defined")
return fmt.Errorf("missing arguments: email address is not defined")
}
current := lagoonCLIConfig.Current
token := lagoonCLIConfig.Lagoons[current].Token
Expand All @@ -526,10 +512,10 @@ var listUsersGroupsCmd = &cobra.Command{
data := []output.Data{}
for _, grouprole := range allUsers.GroupRoles {
data = append(data, []string{
returnNonEmptyString(fmt.Sprintf("%s", allUsers.ID)),
returnNonEmptyString(fmt.Sprintf("%s", allUsers.Email)),
returnNonEmptyString(fmt.Sprintf("%s", grouprole.Name)),
returnNonEmptyString(fmt.Sprintf("%s", grouprole.Role)),
returnNonEmptyString(allUsers.ID.String()),
returnNonEmptyString(allUsers.Email),
returnNonEmptyString(grouprole.Name),
returnNonEmptyString(grouprole.Role),
})
}
dataMain := output.Table{
Expand Down Expand Up @@ -681,8 +667,13 @@ var listOrganizationProjectsCmd = &cobra.Command{
debug)

org, err := l.GetOrganizationByName(context.TODO(), organizationName, lc)
if err != nil {
return err
}
orgProjects, err := l.ListProjectsByOrganizationID(context.TODO(), org.ID, lc)
handleError(err)
if err != nil {
return err
}

if len(*orgProjects) == 0 {
outputOptions.Error = fmt.Sprintf("No associated projects found for organization '%s'\n", organizationName)
Expand All @@ -692,7 +683,7 @@ var listOrganizationProjectsCmd = &cobra.Command{
for _, project := range *orgProjects {
data = append(data, []string{
returnNonEmptyString(fmt.Sprintf("%d", project.ID)),
returnNonEmptyString(fmt.Sprintf("%s", project.Name)),
returnNonEmptyString(project.Name),
returnNonEmptyString(fmt.Sprintf("%d", project.GroupCount)),
})
}
Expand Down Expand Up @@ -735,19 +726,23 @@ var listOrganizationGroupsCmd = &cobra.Command{
debug)

org, err := l.GetOrganizationByName(context.TODO(), organizationName, lc)
if err != nil {
return err
}
orgGroups, err := l.ListGroupsByOrganizationID(context.TODO(), org.ID, lc)
handleError(err)

if err != nil {
return err
}
if len(*orgGroups) == 0 {
outputOptions.Error = fmt.Sprintf("No associated groups found for organization '%s'\n", organizationName)
}

data := []output.Data{}
for _, group := range *orgGroups {
data = append(data, []string{
returnNonEmptyString(fmt.Sprintf("%s", group.ID.String())),
returnNonEmptyString(fmt.Sprintf("%s", group.Name)),
returnNonEmptyString(fmt.Sprintf("%s", group.Type)),
returnNonEmptyString(group.ID.String()),
returnNonEmptyString(group.Name),
returnNonEmptyString(group.Type),
returnNonEmptyString(fmt.Sprintf("%d", group.MemberCount)),
})
}
Expand Down Expand Up @@ -805,12 +800,12 @@ var listOrganizationDeployTargetsCmd = &cobra.Command{
for _, dt := range *deployTargets {
data = append(data, []string{
returnNonEmptyString(fmt.Sprintf("%d", dt.ID)),
returnNonEmptyString(fmt.Sprintf("%s", dt.Name)),
returnNonEmptyString(fmt.Sprintf("%s", dt.RouterPattern)),
returnNonEmptyString(fmt.Sprintf("%s", dt.CloudRegion)),
returnNonEmptyString(fmt.Sprintf("%s", dt.CloudProvider)),
returnNonEmptyString(fmt.Sprintf("%s", dt.SSHHost)),
returnNonEmptyString(fmt.Sprintf("%s", dt.SSHPort)),
returnNonEmptyString(dt.Name),
returnNonEmptyString(dt.RouterPattern),
returnNonEmptyString(dt.CloudRegion),
returnNonEmptyString(dt.CloudProvider),
returnNonEmptyString(dt.SSHHost),
returnNonEmptyString(dt.SSHPort),
})
}
dataMain := output.Table{
Expand Down Expand Up @@ -858,11 +853,11 @@ var ListOrganizationUsersCmd = &cobra.Command{
data := []output.Data{}
for _, user := range *users {
data = append(data, []string{
returnNonEmptyString(fmt.Sprintf("%s", user.ID)),
returnNonEmptyString(fmt.Sprintf("%s", user.Email)),
returnNonEmptyString(fmt.Sprintf("%s", user.FirstName)),
returnNonEmptyString(fmt.Sprintf("%s", user.LastName)),
returnNonEmptyString(fmt.Sprintf("%s", user.Comment)),
returnNonEmptyString(user.ID),
returnNonEmptyString(user.Email),
returnNonEmptyString(user.FirstName),
returnNonEmptyString(user.LastName),
returnNonEmptyString(user.Comment),
returnNonEmptyString(fmt.Sprintf("%v", user.Owner)),
})
}
Expand Down Expand Up @@ -898,13 +893,16 @@ var listOrganizationsCmd = &cobra.Command{
debug)

organizations, err := l.AllOrganizations(context.TODO(), lc)
if err != nil {
return err
}

data := []output.Data{}
for _, organization := range *organizations {
data = append(data, []string{
returnNonEmptyString(fmt.Sprintf("%d", organization.ID)),
returnNonEmptyString(fmt.Sprintf("%s", organization.Name)),
returnNonEmptyString(fmt.Sprintf("%s", organization.Description)),
returnNonEmptyString(organization.Name),
returnNonEmptyString(organization.Description),
returnNonEmptyString(fmt.Sprintf("%d", organization.QuotaProject)),
returnNonEmptyString(fmt.Sprintf("%d", organization.QuotaGroup)),
returnNonEmptyString(fmt.Sprintf("%d", organization.QuotaNotification)),
Expand Down
Loading

0 comments on commit 86458ac

Please sign in to comment.