Skip to content

Commit

Permalink
refactor: make get deployment more useful by using build name (#294)
Browse files Browse the repository at this point in the history
  • Loading branch information
shreddedbacon committed Dec 5, 2023
1 parent 987c4e8 commit d4a8e2e
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 38 deletions.
91 changes: 64 additions & 27 deletions cmd/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (

"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"
)
Expand Down Expand Up @@ -123,41 +123,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 @@ -275,17 +311,18 @@ var getOrganizationCmd = &cobra.Command{

func init() {
getCmd.AddCommand(getAllUserKeysCmd)
getCmd.AddCommand(getDeploymentCmd)
getCmd.AddCommand(getEnvironmentCmd)
getCmd.AddCommand(getOrganizationCmd)
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")
getOrganizationCmd.Flags().StringP("name", "O", "", "Name of the organization")
}
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 organization](lagoon_get_organization.md) - Get details about an organization
* [lagoon get project](lagoon_get_project.md) - Get details about a project
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
12 changes: 6 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,32 @@ 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.8.2
github.com/uselagoon/machinery v0.0.13
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/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.13
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
Expand Down

0 comments on commit d4a8e2e

Please sign in to comment.