Skip to content

Commit

Permalink
refactor: make get deployment more useful by using build name
Browse files Browse the repository at this point in the history
  • Loading branch information
shreddedbacon committed Oct 12, 2023
1 parent 31c24bc commit e9fd16b
Show file tree
Hide file tree
Showing 22 changed files with 111 additions and 71 deletions.
93 changes: 66 additions & 27 deletions cmd/get.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package cmd

import (
"context"
"encoding/json"
"fmt"
"os"

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

// GetFlags .
Expand Down Expand Up @@ -71,41 +73,77 @@ var getProjectCmd = &cobra.Command{
},
}

var getDeploymentCmd = &cobra.Command{
var getDeploymentByNameCmd = &cobra.Command{
Use: "deployment",
Aliases: []string{"d"},
Short: "Get a build log by remote id",
Run: func(cmd *cobra.Command, args []string) {
getProjectFlags := parseGetFlags(*cmd.Flags())
if getProjectFlags.RemoteID == "" {
fmt.Println("Missing arguments: Remote ID is not defined")
cmd.Help()
os.Exit(1)
Short: "Get a deployment by name",
Long: `Get a deployment by name
This returns information about a deployment, the logs of this build can also be retrieved`,
RunE: func(cmd *cobra.Command, args []string) error {
debug, err := cmd.Flags().GetBool("debug")
if err != nil {
return err
}
returnedJSON, err := eClient.GetDeploymentLog(getProjectFlags.RemoteID)

buildName, err := cmd.Flags().GetString("name")
if err != nil {
output.RenderError(err.Error(), outputOptions)
os.Exit(1)
return err
}
if string(returnedJSON) == "null" {
output.RenderInfo(fmt.Sprintf("No deployment for remoteId '%s'", getProjectFlags.RemoteID), outputOptions)
os.Exit(0)
showLogs, err := cmd.Flags().GetBool("logs")
if err != nil {
return err
}
var deployment api.Deployment
err = json.Unmarshal([]byte(returnedJSON), &deployment)
current := lagoonCLIConfig.Current
token := lagoonCLIConfig.Lagoons[current].Token
lc := lclient.New(
lagoonCLIConfig.Lagoons[current].GraphQL,
lagoonCLIVersion,
&token,
debug)
deployment, err := l.GetDeploymentByName(context.TODO(), cmdProjectName, cmdProjectEnvironment, buildName, showLogs, lc)
if err != nil {
output.RenderError(err.Error(), outputOptions)
os.Exit(1)
return err
}
if deployment.BuildLog != "" {
fmt.Println(deployment.BuildLog)
} else {
fmt.Println("Log data is not available")
if showLogs {
dataMain := output.Table{
Header: []string{
"Logs",
},
Data: []output.Data{
{
returnNonEmptyString(deployment.BuildLog),
},
},
}
output.RenderOutput(dataMain, outputOptions)
return nil
}

dataMain := output.Table{
Header: []string{
"ID",
"RemoteID",
"Name",
"Status",
"Created",
"Started",
"Completed",
},
Data: []output.Data{
{
returnNonEmptyString(fmt.Sprintf("%v", deployment.ID)),
returnNonEmptyString(fmt.Sprintf("%v", deployment.RemoteID)),
returnNonEmptyString(fmt.Sprintf("%v", deployment.Name)),
returnNonEmptyString(fmt.Sprintf("%v", deployment.Status)),
returnNonEmptyString(fmt.Sprintf("%v", deployment.Created)),
returnNonEmptyString(fmt.Sprintf("%v", deployment.Started)),
returnNonEmptyString(fmt.Sprintf("%v", deployment.Completed)),
},
},
}
output.RenderOutput(dataMain, outputOptions)
return nil
},
}

var getEnvironmentCmd = &cobra.Command{
Use: "environment",
Aliases: []string{"e"},
Expand Down Expand Up @@ -168,15 +206,16 @@ var getToken = &cobra.Command{

func init() {
getCmd.AddCommand(getAllUserKeysCmd)
getCmd.AddCommand(getDeploymentCmd)
getCmd.AddCommand(getEnvironmentCmd)
getCmd.AddCommand(getProjectCmd)
getCmd.AddCommand(getProjectKeyCmd)
getCmd.AddCommand(getUserKeysCmd)
getCmd.AddCommand(getTaskByID)
getCmd.AddCommand(getToken)
getCmd.AddCommand(getDeploymentByNameCmd)
getTaskByID.Flags().IntP("id", "I", 0, "ID of the task")
getTaskByID.Flags().BoolP("logs", "L", false, "Show the task logs if available")
getProjectKeyCmd.Flags().BoolVarP(&revealValue, "reveal", "", false, "Reveal the variable values")
getDeploymentCmd.Flags().StringVarP(&remoteID, "remoteid", "R", "", "The remote ID of the deployment")
getDeploymentByNameCmd.Flags().StringP("name", "N", "", "The name of the deployment (eg, lagoon-build-abcdef)")
getDeploymentByNameCmd.Flags().BoolP("logs", "L", false, "Show the build logs if available")
}
2 changes: 1 addition & 1 deletion docs/commands/lagoon.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ lagoon [flags]
* [lagoon get](lagoon_get.md) - Get info on a resource
* [lagoon import](lagoon_import.md) - Import a config from a yaml file
* [lagoon kibana](lagoon_kibana.md) - Launch the kibana interface
* [lagoon list](lagoon_list.md) - List projects, deployments, variables or notifications
* [lagoon list](lagoon_list.md) - List projects, environments, deployments, variables or notifications
* [lagoon login](lagoon_login.md) - Log into a Lagoon instance
* [lagoon retrieve](lagoon_retrieve.md) - Trigger a retrieval operation on backups
* [lagoon run](lagoon_run.md) - Run a task against an environment
Expand Down
2 changes: 1 addition & 1 deletion docs/commands/lagoon_get.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Get info on a resource
* [lagoon](lagoon.md) - Command line integration for Lagoon
* [lagoon get all-user-sshkeys](lagoon_get_all-user-sshkeys.md) - Get all user SSH keys
* [lagoon get backup](lagoon_get_backup.md) - Get a backup download link
* [lagoon get deployment](lagoon_get_deployment.md) - Get a build log by remote id
* [lagoon get deployment](lagoon_get_deployment.md) - Get a deployment by name
* [lagoon get environment](lagoon_get_environment.md) - Get details about an environment
* [lagoon get project](lagoon_get_project.md) - Get details about a project
* [lagoon get project-key](lagoon_get_project-key.md) - Get a projects public key
Expand Down
10 changes: 6 additions & 4 deletions docs/commands/lagoon_get_deployment.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
## lagoon get deployment

Get a build log by remote id
Get a deployment by name

### Synopsis

Get a build log by remote id
Get a deployment by name
This returns information about a deployment, the logs of this build can also be retrieved

```
lagoon get deployment [flags]
Expand All @@ -13,8 +14,9 @@ lagoon get deployment [flags]
### Options

```
-h, --help help for deployment
-R, --remoteid string The remote ID of the deployment
-h, --help help for deployment
-L, --logs Show the build logs if available
-N, --name string The name of the deployment (eg, lagoon-build-abcdef)
```

### Options inherited from parent commands
Expand Down
4 changes: 2 additions & 2 deletions docs/commands/lagoon_list.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
## lagoon list

List projects, deployments, variables or notifications
List projects, environments, deployments, variables or notifications

### Synopsis

List projects, deployments, variables or notifications
List projects, environments, deployments, variables or notifications

### Options

Expand Down
2 changes: 1 addition & 1 deletion docs/commands/lagoon_list_backups.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,5 @@ lagoon list backups [flags]

### SEE ALSO

* [lagoon list](lagoon_list.md) - List projects, deployments, variables or notifications
* [lagoon list](lagoon_list.md) - List projects, environments, deployments, variables or notifications

2 changes: 1 addition & 1 deletion docs/commands/lagoon_list_deployments.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,5 @@ lagoon list deployments [flags]

### SEE ALSO

* [lagoon list](lagoon_list.md) - List projects, deployments, variables or notifications
* [lagoon list](lagoon_list.md) - List projects, environments, deployments, variables or notifications

2 changes: 1 addition & 1 deletion docs/commands/lagoon_list_deploytarget-configs.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,5 @@ lagoon list deploytarget-configs [flags]

### SEE ALSO

* [lagoon list](lagoon_list.md) - List projects, deployments, variables or notifications
* [lagoon list](lagoon_list.md) - List projects, environments, deployments, variables or notifications

2 changes: 1 addition & 1 deletion docs/commands/lagoon_list_deploytargets.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,5 @@ lagoon list deploytargets [flags]

### SEE ALSO

* [lagoon list](lagoon_list.md) - List projects, deployments, variables or notifications
* [lagoon list](lagoon_list.md) - List projects, environments, deployments, variables or notifications

2 changes: 1 addition & 1 deletion docs/commands/lagoon_list_environments.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,5 @@ lagoon list environments [flags]

### SEE ALSO

* [lagoon list](lagoon_list.md) - List projects, deployments, variables or notifications
* [lagoon list](lagoon_list.md) - List projects, environments, deployments, variables or notifications

2 changes: 1 addition & 1 deletion docs/commands/lagoon_list_group-projects.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,5 @@ lagoon list group-projects [flags]

### SEE ALSO

* [lagoon list](lagoon_list.md) - List projects, deployments, variables or notifications
* [lagoon list](lagoon_list.md) - List projects, environments, deployments, variables or notifications

2 changes: 1 addition & 1 deletion docs/commands/lagoon_list_groups.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,5 @@ lagoon list groups [flags]

### SEE ALSO

* [lagoon list](lagoon_list.md) - List projects, deployments, variables or notifications
* [lagoon list](lagoon_list.md) - List projects, environments, deployments, variables or notifications

2 changes: 1 addition & 1 deletion docs/commands/lagoon_list_invokable-tasks.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,5 @@ lagoon list invokable-tasks [flags]

### SEE ALSO

* [lagoon list](lagoon_list.md) - List projects, deployments, variables or notifications
* [lagoon list](lagoon_list.md) - List projects, environments, deployments, variables or notifications

2 changes: 1 addition & 1 deletion docs/commands/lagoon_list_notification.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ List all notifications or notifications on projects

### SEE ALSO

* [lagoon list](lagoon_list.md) - List projects, deployments, variables or notifications
* [lagoon list](lagoon_list.md) - List projects, environments, deployments, variables or notifications
* [lagoon list notification email](lagoon_list_notification_email.md) - List all email notification details (alias: e)
* [lagoon list notification microsoftteams](lagoon_list_notification_microsoftteams.md) - List all Microsoft Teams notification details (alias: m)
* [lagoon list notification project-email](lagoon_list_notification_project-email.md) - List email details about a project (alias: pe)
Expand Down
2 changes: 1 addition & 1 deletion docs/commands/lagoon_list_projects-by-metadata.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,5 @@ lagoon list projects-by-metadata [flags]

### SEE ALSO

* [lagoon list](lagoon_list.md) - List projects, deployments, variables or notifications
* [lagoon list](lagoon_list.md) - List projects, environments, deployments, variables or notifications

2 changes: 1 addition & 1 deletion docs/commands/lagoon_list_projects.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,5 @@ lagoon list projects [flags]

### SEE ALSO

* [lagoon list](lagoon_list.md) - List projects, deployments, variables or notifications
* [lagoon list](lagoon_list.md) - List projects, environments, deployments, variables or notifications

2 changes: 1 addition & 1 deletion docs/commands/lagoon_list_tasks.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,5 @@ lagoon list tasks [flags]

### SEE ALSO

* [lagoon list](lagoon_list.md) - List projects, deployments, variables or notifications
* [lagoon list](lagoon_list.md) - List projects, environments, deployments, variables or notifications

2 changes: 1 addition & 1 deletion docs/commands/lagoon_list_users.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,5 @@ lagoon list users [flags]

### SEE ALSO

* [lagoon list](lagoon_list.md) - List projects, deployments, variables or notifications
* [lagoon list](lagoon_list.md) - List projects, environments, deployments, variables or notifications

2 changes: 1 addition & 1 deletion docs/commands/lagoon_list_variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,5 @@ lagoon list variables [flags]

### SEE ALSO

* [lagoon list](lagoon_list.md) - List projects, deployments, variables or notifications
* [lagoon list](lagoon_list.md) - List projects, environments, deployments, variables or notifications

24 changes: 11 additions & 13 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,41 +4,39 @@ go 1.16

require (
github.com/Masterminds/semver v1.4.2
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf // indirect
github.com/dgrijalva/jwt-go v3.2.0+incompatible
github.com/golang/mock v1.6.0
github.com/google/go-github v0.0.0-20180716180158-c0b63e2f9bb1
github.com/google/go-querystring v1.0.0 // indirect
github.com/google/uuid v1.3.0
github.com/guregu/null v4.0.0+incompatible
github.com/hashicorp/go-version v1.6.0
github.com/integralist/go-findroot v0.0.0-20160518114804-ac90681525dc
github.com/logrusorgru/aurora v0.0.0-20191017060258-dc85c304c434
github.com/machinebox/graphql v0.2.3-0.20181106130121-3a9253180225
github.com/manifoldco/promptui v0.3.2
// workaround for https://github.com/manifoldco/promptui/issues/98
github.com/nicksnyder/go-i18n v1.10.1 // indirect
github.com/olekukonko/tablewriter v0.0.4
github.com/pkg/browser v0.0.0-20180916011732-0a3d74bf9ce4
github.com/spf13/cobra v0.0.5
github.com/spf13/pflag v1.0.3
github.com/stretchr/testify v1.2.2
github.com/uselagoon/machinery v0.0.12
golang.org/x/crypto v0.0.0-20221005025214-4161e89ecf1b
gopkg.in/yaml.v2 v2.2.8
sigs.k8s.io/yaml v1.2.0
)

require (
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf // indirect
github.com/go-bindata/go-bindata v3.1.2+incompatible // indirect
github.com/google/go-querystring v1.0.0 // indirect
github.com/guregu/null v4.0.0+incompatible
// workaround for https://github.com/manifoldco/promptui/issues/98
github.com/nicksnyder/go-i18n v1.10.1 // indirect
github.com/uselagoon/machinery v0.0.11
golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3 // indirect
golang.org/x/sys v0.0.0-20220412211240-33da011f77ad // indirect
gopkg.in/alecthomas/kingpin.v3-unstable v3.0.0-20191105091915-95d230a53780 // indirect
gopkg.in/yaml.v2 v2.2.8
sigs.k8s.io/yaml v1.2.0
)

// use this version for fixes to formatting of end header
replace github.com/olekukonko/tablewriter => github.com/shreddedbacon/tablewriter v0.0.2-0.20200114082015-d810c4a558bf

// replace github.com/machinebox/graphql => ../../shreddedbacon/graphql

// replace github.com/olekukonko/tablewriter => ../../shreddedbacon/tablewriter
// replace github.com/olekukonko/tablewriter => ../../shreddedbacon/tablewriter

// replace github.com/uselagoon/machinery => ../machinery
7 changes: 3 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM=
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/go-bindata/go-bindata v3.1.2+incompatible h1:5vjJMVhowQdPzjE1LdxyFF7YFTXg5IgGVW4gBr5IbvE=
github.com/go-bindata/go-bindata v3.1.2+incompatible/go.mod h1:xK8Dsgwmeed+BBsSy2XTopBn/8uK2HWuGSnA11C3Joo=
github.com/golang-jwt/jwt v3.2.2+incompatible/go.mod h1:8pz2t5EyA70fFQQSrl6XZXzqecmYZeUEB8OUGHkxJ+I=
github.com/golang/lint v0.0.0-20181026193005-c67002cb31c3 h1:I4BOK3PBMjhWfQM2zPJKK7lOBGsrsvOB7kBELP33hiE=
github.com/golang/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:tluoj9z5200jBnyusfRPU2LqT6J+DAorxEvtC7LHB+E=
github.com/golang/mock v1.6.0 h1:ErTB+efbowRARo13NNdxyJji2egdxLGQhRaY+DUumQc=
Expand Down Expand Up @@ -105,8 +104,8 @@ github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXf
github.com/tsenart/deadcode v0.0.0-20160724212837-210d2dc333e9 h1:vY5WqiEon0ZSTGM3ayVVi+twaHKHDFUVloaQ/wug9/c=
github.com/tsenart/deadcode v0.0.0-20160724212837-210d2dc333e9/go.mod h1:q+QjxYvZ+fpjMXqs+XEriussHjSYqeXVnAdSV1tkMYk=
github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0=
github.com/uselagoon/machinery v0.0.11 h1:s6EhyU/pj1+C4FdS0EqmR6C0dLsoeCd9n+5xHL1YDag=
github.com/uselagoon/machinery v0.0.11/go.mod h1:IXLxlkahEAEgpCmu9Xa/Wmjo6ja4Aoq7tf8G7VrileE=
github.com/uselagoon/machinery v0.0.12 h1:TJnA+FrL1uEhRTjJ6dExiL4G7SOQ+hUfGuWDmbW2HBA=
github.com/uselagoon/machinery v0.0.12/go.mod h1:h/qeMWQR4Qqu33x+8AulNDeolEwvb/G+aIsn/jyUtwk=
github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q=
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
golang.org/x/crypto v0.0.0-20181203042331-505ab145d0a9/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
Expand Down
12 changes: 7 additions & 5 deletions internal/lagoon/client/lgraphql/lgraphql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit e9fd16b

Please sign in to comment.