Skip to content

Commit

Permalink
chore: remove additional unused code and fix up some error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
shreddedbacon committed Jun 17, 2024
1 parent 8eab507 commit cd4e346
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 84 deletions.
3 changes: 3 additions & 0 deletions cmd/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,9 @@ This environment should already exist in lagoon. It is analogous with the 'Deplo
}

buildVarStrings, err := cmd.Flags().GetStringArray("buildvar")
if err != nil {
return err
}
if err := requiredInputCheck("Project name", cmdProjectName, "Environment name", cmdProjectEnvironment); err != nil {
return err
}
Expand Down
18 changes: 3 additions & 15 deletions cmd/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@ package cmd

import (
"context"
"encoding/json"
"fmt"
"strconv"
"strings"

"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/uselagoon/lagoon-cli/pkg/output"

"github.com/uselagoon/machinery/api/lagoon"
Expand All @@ -22,19 +20,6 @@ type GetFlags struct {
RemoteID string `json:"remoteid,omitempty"`
}

func parseGetFlags(flags pflag.FlagSet) GetFlags {
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 := GetFlags{}
json.Unmarshal(jsonStr, &parsedFlags)
return parsedFlags
}

var getCmd = &cobra.Command{
Use: "get",
Aliases: []string{"g"},
Expand Down Expand Up @@ -216,6 +201,9 @@ var getEnvironmentCmd = &cobra.Command{
},
RunE: func(cmd *cobra.Command, args []string) error {
debug, err := cmd.Flags().GetBool("debug")
if err != nil {
return err
}
if err := requiredInputCheck("Project name", cmdProjectName, "Environment name", cmdProjectEnvironment); err != nil {
return err
}
Expand Down
33 changes: 18 additions & 15 deletions cmd/groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package cmd

import (
"context"
"encoding/json"
"fmt"
"slices"
"strings"
Expand All @@ -13,23 +12,9 @@ import (

"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/uselagoon/lagoon-cli/pkg/api"
"github.com/uselagoon/lagoon-cli/pkg/output"
)

func parseGroup(flags pflag.FlagSet) api.Group {
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 := api.Group{}
json.Unmarshal(jsonStr, &parsedFlags)
return parsedFlags
}

var addGroupCmd = &cobra.Command{
Use: "group",
Aliases: []string{"g"},
Expand Down Expand Up @@ -191,6 +176,9 @@ var addProjectToGroupCmd = &cobra.Command{
},
RunE: func(cmd *cobra.Command, args []string) error {
debug, err := cmd.Flags().GetBool("debug")
if err != nil {
return err
}
groupName, err := cmd.Flags().GetString("name")
if err != nil {
return err
Expand Down Expand Up @@ -220,6 +208,9 @@ var addProjectToGroupCmd = &cobra.Command{
debug)

project, err := lagoon.GetMinimalProjectByName(context.TODO(), cmdProjectName, lc)
if err != nil {
return err
}
if len(project.Name) == 0 {
outputOptions.Error = fmt.Sprintf("Project '%s' not found", cmdProjectName)
output.RenderError(outputOptions.Error, outputOptions)
Expand Down Expand Up @@ -247,6 +238,9 @@ var deleteUserFromGroupCmd = &cobra.Command{
},
RunE: func(cmd *cobra.Command, args []string) error {
debug, err := cmd.Flags().GetBool("debug")
if err != nil {
return err
}
groupName, err := cmd.Flags().GetString("name")
if err != nil {
return err
Expand Down Expand Up @@ -301,6 +295,9 @@ var deleteProjectFromGroupCmd = &cobra.Command{
},
RunE: func(cmd *cobra.Command, args []string) error {
debug, err := cmd.Flags().GetBool("debug")
if err != nil {
return err
}
groupName, err := cmd.Flags().GetString("name")
if err != nil {
return err
Expand Down Expand Up @@ -330,6 +327,9 @@ var deleteProjectFromGroupCmd = &cobra.Command{
debug)

project, err := lagoon.GetMinimalProjectByName(context.TODO(), cmdProjectName, lc)
if err != nil {
return err
}
if len(project.Name) == 0 {
outputOptions.Error = fmt.Sprintf("Project '%s' not found", cmdProjectName)
output.RenderError(outputOptions.Error, outputOptions)
Expand All @@ -356,6 +356,9 @@ var deleteGroupCmd = &cobra.Command{
Short: "Delete a group from lagoon",
RunE: func(cmd *cobra.Command, args []string) error {
debug, err := cmd.Flags().GetBool("debug")
if err != nil {
return err
}
groupName, err := cmd.Flags().GetString("name")
if err != nil {
return err
Expand Down
28 changes: 24 additions & 4 deletions cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,10 +248,10 @@ var listGroupProjectsCmd = &cobra.Command{
for _, project := range group.Projects {
data = append(data, []string{
returnNonEmptyString(fmt.Sprintf("%d", project.ID)),
returnNonEmptyString(fmt.Sprintf("%s", project.Name)),
returnNonEmptyString(project.Name),
})
if listAllProjects {
data[idx] = append(data[idx], returnNonEmptyString(fmt.Sprintf("%s", group.Name)))
data[idx] = append(data[idx], returnNonEmptyString(group.Name))
}
idx++
}
Expand All @@ -262,6 +262,8 @@ var listGroupProjectsCmd = &cobra.Command{
} else {
outputOptions.Error = "There are no projects in any groups\n"
}
output.RenderOutput(output.Table{Data: []output.Data{[]string{}}}, outputOptions)
return nil
}

dataMain := output.Table{
Expand Down Expand Up @@ -307,6 +309,8 @@ var listEnvironmentsCmd = &cobra.Command{

if len(*environments) == 0 {
outputOptions.Error = fmt.Sprintf("No environments found for project '%s'\n", cmdProjectName)
output.RenderOutput(output.Table{Data: []output.Data{[]string{}}}, outputOptions)
return nil
}

data := []output.Data{}
Expand Down Expand Up @@ -403,6 +407,8 @@ var listVariablesCmd = &cobra.Command{
} else {
outputOptions.Error = fmt.Sprintf("There are no variables for project '%s'\n", cmdProjectName)
}
output.RenderOutput(output.Table{Data: []output.Data{[]string{}}}, outputOptions)
return nil
}
output.RenderOutput(output.Table{
Header: header,
Expand Down Expand Up @@ -462,6 +468,8 @@ var listDeploymentsCmd = &cobra.Command{

if len(data) == 0 {
outputOptions.Error = fmt.Sprintf("There are no deployments for environment '%s' in project '%s'\n", cmdProjectEnvironment, cmdProjectName)
output.RenderOutput(output.Table{Data: []output.Data{[]string{}}}, outputOptions)
return nil
}
dataMain := output.Table{
Header: []string{"ID", "RemoteID", "Name", "Status", "Created", "Started", "Completed"},
Expand Down Expand Up @@ -523,6 +531,8 @@ var listTasksCmd = &cobra.Command{

if len(data) == 0 {
outputOptions.Error = fmt.Sprintf("There are no tasks for environment '%s' in project '%s'\n", cmdProjectEnvironment, cmdProjectName)
output.RenderOutput(output.Table{Data: []output.Data{[]string{}}}, outputOptions)
return nil
}
dataMain := output.Table{
Header: []string{"ID", "RemoteID", "Name", "Status", "Created", "Started", "Completed", "Service"},
Expand Down Expand Up @@ -740,13 +750,15 @@ var listInvokableTasks = &cobra.Command{
data := []output.Data{}
for _, task := range tasks.AdvancedTasks {
data = append(data, []string{
returnNonEmptyString(fmt.Sprintf("%s", task.Name)),
returnNonEmptyString(fmt.Sprintf("%s", task.Description)),
returnNonEmptyString(task.Name),
returnNonEmptyString(task.Description),
})
}

if len(data) == 0 {
outputOptions.Error = "There are no user defined tasks for this environment\n"
output.RenderOutput(output.Table{Data: []output.Data{[]string{}}}, outputOptions)
return nil
}
dataMain := output.Table{
Header: []string{"Task Name", "Description"},
Expand Down Expand Up @@ -797,6 +809,8 @@ var listProjectGroupsCmd = &cobra.Command{

if len(projectGroups.Groups) == 0 {
outputOptions.Error = fmt.Sprintf("There are no groups for project '%s'\n", cmdProjectName)
output.RenderOutput(output.Table{Data: []output.Data{[]string{}}}, outputOptions)
return nil
}

data := []output.Data{}
Expand Down Expand Up @@ -863,6 +877,8 @@ var listOrganizationProjectsCmd = &cobra.Command{

if len(*orgProjects) == 0 {
outputOptions.Error = fmt.Sprintf("No associated projects found for organization '%s'\n", organizationName)
output.RenderOutput(output.Table{Data: []output.Data{[]string{}}}, outputOptions)
return nil
}

data := []output.Data{}
Expand Down Expand Up @@ -924,6 +940,8 @@ var listOrganizationGroupsCmd = &cobra.Command{
}
if len(*orgGroups) == 0 {
outputOptions.Error = fmt.Sprintf("No associated groups found for organization '%s'\n", organizationName)
output.RenderOutput(output.Table{Data: []output.Data{[]string{}}}, outputOptions)
return nil
}

data := []output.Data{}
Expand Down Expand Up @@ -982,6 +1000,8 @@ var listOrganizationDeployTargetsCmd = &cobra.Command{
}
if len(*deployTargets) == 0 {
outputOptions.Error = fmt.Sprintf("No associated deploy targets found for organization '%s'\n", organizationName)
output.RenderOutput(output.Table{Data: []output.Data{[]string{}}}, outputOptions)
return nil
}

data := []output.Data{}
Expand Down
24 changes: 0 additions & 24 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ import (
"github.com/uselagoon/lagoon-cli/pkg/app"
"github.com/uselagoon/lagoon-cli/pkg/graphql"
"github.com/uselagoon/lagoon-cli/pkg/lagoon/environments"
"github.com/uselagoon/lagoon-cli/pkg/lagoon/projects"
"github.com/uselagoon/lagoon-cli/pkg/lagoon/users"
"github.com/uselagoon/lagoon-cli/pkg/output"
"github.com/uselagoon/lagoon-cli/pkg/updatecheck"
)
Expand Down Expand Up @@ -286,8 +284,6 @@ func Prompt(prompt string) string {

// global the clients
var eClient environments.Client
var uClient users.Client
var pClient projects.Client

// FormatType .
type FormatType string
Expand Down Expand Up @@ -319,16 +315,6 @@ func validateToken(lagoon string) {
output.RenderError(err.Error(), outputOptions)
os.Exit(1)
}
uClient, err = users.New(&lagoonCLIConfig, debugEnable)
if err != nil {
output.RenderError(err.Error(), outputOptions)
os.Exit(1)
}
pClient, err = projects.New(&lagoonCLIConfig, debugEnable)
if err != nil {
output.RenderError(err.Error(), outputOptions)
os.Exit(1)
}
outputOptions.Debug = debugEnable
}

Expand All @@ -353,16 +339,6 @@ func validateTokenE(lagoon string) error {
output.RenderError(err.Error(), outputOptions)
return err
}
uClient, err = users.New(&lagoonCLIConfig, debugEnable)
if err != nil {
output.RenderError(err.Error(), outputOptions)
return err
}
pClient, err = projects.New(&lagoonCLIConfig, debugEnable)
if err != nil {
output.RenderError(err.Error(), outputOptions)
return err
}
outputOptions.Debug = debugEnable
// fallback if token is expired or there was no token to begin with
return nil
Expand Down
7 changes: 0 additions & 7 deletions cmd/shared.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,6 @@ var lagoonUI string
var lagoonKibana string
var lagoonSSHKey string

// user vars
var userFirstName string
var userLastName string
var userEmail string
var pubKeyFile string
var sshKeyName string

// group vars
var groupName string

Expand Down
7 changes: 6 additions & 1 deletion cmd/tasks.go
Original file line number Diff line number Diff line change
Expand Up @@ -354,9 +354,14 @@ Path:
Command: taskCommand,
Service: taskService,
}
fmt.Println(task.Name)
project, err := lagoon.GetMinimalProjectByName(context.TODO(), cmdProjectName, lc)
if err != nil {
return err
}
environment, err := lagoon.GetEnvironmentByName(context.TODO(), cmdProjectEnvironment, project.ID, lc)
if err != nil {
return err
}
taskResult, err := lagoon.AddTask(context.TODO(), environment.ID, task, lc)
if err != nil {
return err
Expand Down
18 changes: 0 additions & 18 deletions cmd/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package cmd

import (
"context"
"encoding/json"
"fmt"
"os"
"strconv"
Expand All @@ -13,26 +12,9 @@ import (
"github.com/uselagoon/machinery/api/schema"

"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/uselagoon/lagoon-cli/pkg/api"
"github.com/uselagoon/lagoon-cli/pkg/output"
)

func parseUser(flags pflag.FlagSet) api.User {
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 := api.User{}
json.Unmarshal(jsonStr, &parsedFlags)
// lowercase user email address
parsedFlags.Email = strings.ToLower(parsedFlags.Email)
return parsedFlags
}

func parseSSHKeyFile(sshPubKey string, keyName string, keyValue string, userEmail string) (schema.AddSSHKeyInput, error) {
// if we haven't got a keyvalue
if keyValue == "" {
Expand Down

0 comments on commit cd4e346

Please sign in to comment.