From 9bb1ee723f7e79bf388043945c42479edfa3bdea Mon Sep 17 00:00:00 2001 From: Chris Goodwin <40746380+CGoodwin90@users.noreply.github.com> Date: Wed, 20 Mar 2024 12:35:36 +1100 Subject: [PATCH] Improvement: Includes logic checks if project doesn't exist or no notifications found (#322) (#323) * Included logic checks if project doesn't exist or no notifications found * Upgraded to latest machinery --------- Co-authored-by: Ben Jackson --- cmd/notificationsemail.go | 32 +++++++++++++++++++++----------- cmd/notificationsrocketchat.go | 34 ++++++++++++++++++++++------------ cmd/notificationsslack.go | 34 ++++++++++++++++++++++------------ cmd/notificationsteams.go | 32 +++++++++++++++++++++----------- cmd/notificationswebhook.go | 32 +++++++++++++++++++++----------- go.mod | 8 ++++---- go.sum | 13 +++++++++++++ 7 files changed, 124 insertions(+), 61 deletions(-) diff --git a/cmd/notificationsemail.go b/cmd/notificationsemail.go index ac3f96f2..3abe4261 100644 --- a/cmd/notificationsemail.go +++ b/cmd/notificationsemail.go @@ -154,26 +154,36 @@ var listProjectEmailsCmd = &cobra.Command{ if err != nil { return err } - if cmdProjectName == "" { - return fmt.Errorf("Missing arguments: project name is not defined") + if err := requiredInputCheck("Project name", cmdProjectName); err != nil { + return err } + current := lagoonCLIConfig.Current - lc := client.New( + token := lagoonCLIConfig.Lagoons[current].Token + lc := lclient.New( lagoonCLIConfig.Lagoons[current].GraphQL, - lagoonCLIConfig.Lagoons[current].Token, - lagoonCLIConfig.Lagoons[current].Version, lagoonCLIVersion, + &token, debug) - result, err := lagoon.GetProjectNotificationEmail(context.TODO(), cmdProjectName, lc) + + result, err := l.GetProjectNotificationEmail(context.TODO(), cmdProjectName, lc) if err != nil { return err } + if len(result.Name) == 0 { + outputOptions.Error = fmt.Sprintf("No project found for '%s'\n", cmdProjectName) + } else if len(result.Notifications.Email) == 0 { + outputOptions.Error = fmt.Sprintf("No email notificatons found for project: '%s'\n", cmdProjectName) + } + data := []output.Data{} - for _, notification := range result.Notifications.Email { - data = append(data, []string{ - returnNonEmptyString(fmt.Sprintf("%v", notification.Name)), - returnNonEmptyString(fmt.Sprintf("%v", notification.EmailAddress)), - }) + if result.Notifications != nil { + for _, notification := range result.Notifications.Email { + data = append(data, []string{ + returnNonEmptyString(fmt.Sprintf("%v", notification.Name)), + returnNonEmptyString(fmt.Sprintf("%v", notification.EmailAddress)), + }) + } } output.RenderOutput(output.Table{ Header: []string{ diff --git a/cmd/notificationsrocketchat.go b/cmd/notificationsrocketchat.go index c74b76fd..a6de9c0a 100644 --- a/cmd/notificationsrocketchat.go +++ b/cmd/notificationsrocketchat.go @@ -162,27 +162,37 @@ var listProjectRocketChatsCmd = &cobra.Command{ if err != nil { return err } - if cmdProjectName == "" { - return fmt.Errorf("Missing arguments: project name is not defined") + if err := requiredInputCheck("Project name", cmdProjectName); err != nil { + return err } + current := lagoonCLIConfig.Current - lc := client.New( + token := lagoonCLIConfig.Lagoons[current].Token + lc := lclient.New( lagoonCLIConfig.Lagoons[current].GraphQL, - lagoonCLIConfig.Lagoons[current].Token, - lagoonCLIConfig.Lagoons[current].Version, lagoonCLIVersion, + &token, debug) - result, err := lagoon.GetProjectNotificationRocketChat(context.TODO(), cmdProjectName, lc) + + result, err := l.GetProjectNotificationRocketChat(context.TODO(), cmdProjectName, lc) if err != nil { return err } + if len(result.Name) == 0 { + outputOptions.Error = fmt.Sprintf("No project found for '%s'\n", cmdProjectName) + } else if len(result.Notifications.RocketChat) == 0 { + outputOptions.Error = fmt.Sprintf("No rocketchat notificatons found for project: '%s'\n", cmdProjectName) + } + data := []output.Data{} - for _, notification := range result.Notifications.RocketChat { - data = append(data, []string{ - returnNonEmptyString(fmt.Sprintf("%v", notification.Name)), - returnNonEmptyString(fmt.Sprintf("%v", notification.Webhook)), - returnNonEmptyString(fmt.Sprintf("%v", notification.Channel)), - }) + if result.Notifications != nil { + for _, notification := range result.Notifications.RocketChat { + data = append(data, []string{ + returnNonEmptyString(fmt.Sprintf("%v", notification.Name)), + returnNonEmptyString(fmt.Sprintf("%v", notification.Webhook)), + returnNonEmptyString(fmt.Sprintf("%v", notification.Channel)), + }) + } } output.RenderOutput(output.Table{ Header: []string{ diff --git a/cmd/notificationsslack.go b/cmd/notificationsslack.go index 4d4074e0..8353fd70 100644 --- a/cmd/notificationsslack.go +++ b/cmd/notificationsslack.go @@ -162,27 +162,37 @@ var listProjectSlacksCmd = &cobra.Command{ if err != nil { return err } - if cmdProjectName == "" { - return fmt.Errorf("Missing arguments: project name is not defined") + if err := requiredInputCheck("Project name", cmdProjectName); err != nil { + return err } + current := lagoonCLIConfig.Current - lc := client.New( + token := lagoonCLIConfig.Lagoons[current].Token + lc := lclient.New( lagoonCLIConfig.Lagoons[current].GraphQL, - lagoonCLIConfig.Lagoons[current].Token, - lagoonCLIConfig.Lagoons[current].Version, lagoonCLIVersion, + &token, debug) - result, err := lagoon.GetProjectNotificationSlack(context.TODO(), cmdProjectName, lc) + + result, err := l.GetProjectNotificationSlack(context.TODO(), cmdProjectName, lc) if err != nil { return err } + if len(result.Name) == 0 { + outputOptions.Error = fmt.Sprintf("No project found for '%s'\n", cmdProjectName) + } else if len(result.Notifications.Slack) == 0 { + outputOptions.Error = fmt.Sprintf("No slack notificatons found for project: '%s'\n", cmdProjectName) + } + data := []output.Data{} - for _, notification := range result.Notifications.Slack { - data = append(data, []string{ - returnNonEmptyString(fmt.Sprintf("%v", notification.Name)), - returnNonEmptyString(fmt.Sprintf("%v", notification.Webhook)), - returnNonEmptyString(fmt.Sprintf("%v", notification.Channel)), - }) + if result.Notifications != nil { + for _, notification := range result.Notifications.Slack { + data = append(data, []string{ + returnNonEmptyString(fmt.Sprintf("%v", notification.Name)), + returnNonEmptyString(fmt.Sprintf("%v", notification.Webhook)), + returnNonEmptyString(fmt.Sprintf("%v", notification.Channel)), + }) + } } output.RenderOutput(output.Table{ Header: []string{ diff --git a/cmd/notificationsteams.go b/cmd/notificationsteams.go index bac5212c..1d31953e 100644 --- a/cmd/notificationsteams.go +++ b/cmd/notificationsteams.go @@ -155,26 +155,36 @@ var listProjectMicrosoftTeamsCmd = &cobra.Command{ if err != nil { return err } - if cmdProjectName == "" { - return fmt.Errorf("Missing arguments: project name is not defined") + if err := requiredInputCheck("Project name", cmdProjectName); err != nil { + return err } + current := lagoonCLIConfig.Current - lc := client.New( + token := lagoonCLIConfig.Lagoons[current].Token + lc := lclient.New( lagoonCLIConfig.Lagoons[current].GraphQL, - lagoonCLIConfig.Lagoons[current].Token, - lagoonCLIConfig.Lagoons[current].Version, lagoonCLIVersion, + &token, debug) - result, err := lagoon.GetProjectNotificationMicrosoftTeams(context.TODO(), cmdProjectName, lc) + + result, err := l.GetProjectNotificationMicrosoftTeams(context.TODO(), cmdProjectName, lc) if err != nil { return err } + if len(result.Name) == 0 { + outputOptions.Error = fmt.Sprintf("No project found for '%s'\n", cmdProjectName) + } else if len(result.Notifications.MicrosoftTeams) == 0 { + outputOptions.Error = fmt.Sprintf("No microsoft teams notificatons found for project: '%s'\n", cmdProjectName) + } + data := []output.Data{} - for _, notification := range result.Notifications.MicrosoftTeams { - data = append(data, []string{ - returnNonEmptyString(fmt.Sprintf("%v", notification.Name)), - returnNonEmptyString(fmt.Sprintf("%v", notification.Webhook)), - }) + if result.Notifications != nil { + for _, notification := range result.Notifications.MicrosoftTeams { + data = append(data, []string{ + returnNonEmptyString(fmt.Sprintf("%v", notification.Name)), + returnNonEmptyString(fmt.Sprintf("%v", notification.Webhook)), + }) + } } output.RenderOutput(output.Table{ Header: []string{ diff --git a/cmd/notificationswebhook.go b/cmd/notificationswebhook.go index 35021065..c82524b9 100644 --- a/cmd/notificationswebhook.go +++ b/cmd/notificationswebhook.go @@ -155,26 +155,36 @@ var listProjectWebhooksCmd = &cobra.Command{ if err != nil { return err } - if cmdProjectName == "" { - return fmt.Errorf("Missing arguments: project name is not defined") + if err := requiredInputCheck("Project name", cmdProjectName); err != nil { + return err } + current := lagoonCLIConfig.Current - lc := client.New( + token := lagoonCLIConfig.Lagoons[current].Token + lc := lclient.New( lagoonCLIConfig.Lagoons[current].GraphQL, - lagoonCLIConfig.Lagoons[current].Token, - lagoonCLIConfig.Lagoons[current].Version, lagoonCLIVersion, + &token, debug) - result, err := lagoon.GetProjectNotificationWebhook(context.TODO(), cmdProjectName, lc) + + result, err := l.GetProjectNotificationWebhook(context.TODO(), cmdProjectName, lc) if err != nil { return err } + if len(result.Name) == 0 { + outputOptions.Error = fmt.Sprintf("No project found for '%s'\n", cmdProjectName) + } else if len(result.Notifications.Webhook) == 0 { + outputOptions.Error = fmt.Sprintf("No webhook notificatons found for project: '%s'\n", cmdProjectName) + } + data := []output.Data{} - for _, notification := range result.Notifications.Webhook { - data = append(data, []string{ - returnNonEmptyString(fmt.Sprintf("%v", notification.Name)), - returnNonEmptyString(fmt.Sprintf("%v", notification.Webhook)), - }) + if result.Notifications != nil { + for _, notification := range result.Notifications.Webhook { + data = append(data, []string{ + returnNonEmptyString(fmt.Sprintf("%v", notification.Name)), + returnNonEmptyString(fmt.Sprintf("%v", notification.Webhook)), + }) + } } output.RenderOutput(output.Table{ Header: []string{ diff --git a/go.mod b/go.mod index f9473643..fc1a532b 100644 --- a/go.mod +++ b/go.mod @@ -19,8 +19,8 @@ require ( github.com/spf13/cobra v1.8.0 github.com/spf13/pflag v1.0.5 github.com/stretchr/testify v1.8.2 - github.com/uselagoon/machinery v0.0.16 - golang.org/x/crypto v0.17.0 + github.com/uselagoon/machinery v0.0.18 + golang.org/x/crypto v0.21.0 gopkg.in/yaml.v3 v3.0.1 sigs.k8s.io/yaml v1.4.0 ) @@ -36,8 +36,8 @@ require ( github.com/pmezard/go-difflib v1.0.0 // indirect github.com/rivo/uniseg v0.2.0 // indirect github.com/russross/blackfriday/v2 v2.1.0 // indirect - golang.org/x/sys v0.15.0 // indirect - golang.org/x/term v0.15.0 // indirect + golang.org/x/sys v0.18.0 // indirect + golang.org/x/term v0.18.0 // indirect ) // replace github.com/uselagoon/machinery => ../machinery diff --git a/go.sum b/go.sum index c569c611..14f7263d 100644 --- a/go.sum +++ b/go.sum @@ -68,11 +68,18 @@ github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= github.com/uselagoon/machinery v0.0.16 h1:LGWUaESTXPfiTyJTJCC1i+3ghidY2qsf9li7mGbH9Wo= github.com/uselagoon/machinery v0.0.16/go.mod h1:Duljjz/3d/7m0jbmF1nVRDTNaMxMr6m+5LkgjiRrQaU= +github.com/uselagoon/machinery v0.0.17-0.20240313011522-ed1d14100c69 h1:T6wm/RNQMhIdAWtzcVGttbXKasPNN/KKqUeBBOURiFI= +github.com/uselagoon/machinery v0.0.17-0.20240313011522-ed1d14100c69/go.mod h1:Duljjz/3d/7m0jbmF1nVRDTNaMxMr6m+5LkgjiRrQaU= +github.com/uselagoon/machinery v0.0.18 h1:KQLMAYa0pJOSDlnwv7uKa4XXZTqw8WiI0VL+yIFgesM= +github.com/uselagoon/machinery v0.0.18/go.mod h1:NbgtEofjK2XY0iUpk9aMYazIo+W/NI56+UF72jv8zVY= github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= golang.org/x/crypto v0.17.0 h1:r8bRNjWL3GshPW3gkd+RpvzWrZAwPS49OmTGZ/uhM4k= golang.org/x/crypto v0.17.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4= +golang.org/x/crypto v0.19.0/go.mod h1:Iy9bg/ha4yyC70EfRS8jz+B6ybOBKMaSxLj6P6oBDfU= +golang.org/x/crypto v0.21.0 h1:X31++rzVUdKhX5sWmSOFZxx8UW/ldWx55cbf08iNAMA= +golang.org/x/crypto v0.21.0/go.mod h1:0BP7YvVV9gBbVKyeTG0Gyn+gZm94bibOW5BjDEYAOMs= golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= @@ -88,9 +95,15 @@ golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBc golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc= golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4= +golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.15.0 h1:y/Oo/a/q3IXu26lQgl04j/gjuBDOBlx7X6Om1j2CPW4= golang.org/x/term v0.15.0/go.mod h1:BDl952bC7+uMoWR75FIrCDx79TPU9oHkTZ9yRbYOrX0= +golang.org/x/term v0.17.0/go.mod h1:lLRBjIVuehSbZlaOtGMbcMncT+aqLLLmKrsjNrUguwk= +golang.org/x/term v0.18.0 h1:FcHjZXDMxI8mM3nwhX9HlKop4C0YQvCVCdwYl2wOtE8= +golang.org/x/term v0.18.0/go.mod h1:ILwASektA3OnRv7amZ1xhE/KTR+u50pbXfZ03+6Nx58= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=