diff --git a/cmd/list.go b/cmd/list.go index 1345d8cf..96ab4eed 100644 --- a/cmd/list.go +++ b/cmd/list.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "strconv" + "strings" "github.com/spf13/cobra" "github.com/uselagoon/lagoon-cli/pkg/output" @@ -1243,6 +1244,76 @@ var listOrganizationsCmd = &cobra.Command{ }, } +var listEnvironmentServicesCmd = &cobra.Command{ + Use: "environment-services", + Aliases: []string{"e"}, + Short: "Get information about an environments services", + PreRunE: func(_ *cobra.Command, _ []string) error { + return validateTokenE(lagoonCLIConfig.Current) + }, + 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 + } + current := lagoonCLIConfig.Current + token := lagoonCLIConfig.Lagoons[current].Token + lc := lclient.New( + lagoonCLIConfig.Lagoons[current].GraphQL, + lagoonCLIVersion, + lagoonCLIConfig.Lagoons[current].Version, + &token, + debug) + + project, err := lagoon.GetProjectByName(context.TODO(), cmdProjectName, lc) + if err != nil { + return err + } + environment, err := lagoon.GetEnvironmentByName(context.TODO(), cmdProjectEnvironment, project.ID, lc) + if err != nil { + return err + } + + if project.Name == "" || environment.Name == "" { + if project.Name == "" { + return handleNilResults("Project '%s' not found\n", cmd, cmdProjectName) + } else { + return handleNilResults("Environment '%s' not found in project '%s'\n", cmd, cmdProjectEnvironment, cmdProjectName) + } + } + + data := []output.Data{} + envHeader := []string{"EnvironmentID", "EnvironmentName", "ServiceID", "ServiceName", "ServiceType", "Containers", "Updated", "Created"} + for _, es := range environment.Services { + containers := []string{} + for _, c := range es.Containers { + containers = append(containers, c.Name) + } + envData := []string{ + returnNonEmptyString(fmt.Sprintf("%d", environment.ID)), + returnNonEmptyString(fmt.Sprintf("%v", environment.Name)), + returnNonEmptyString(fmt.Sprintf("%d", es.ID)), + returnNonEmptyString(fmt.Sprintf("%v", es.Name)), + returnNonEmptyString(fmt.Sprintf("%v", es.Type)), + returnNonEmptyString(fmt.Sprintf("%v", strings.Join(containers, ","))), + returnNonEmptyString(fmt.Sprintf("%v", es.Updated)), + returnNonEmptyString(fmt.Sprintf("%v", es.Created)), + } + data = append(data, envData) + } + dataMain := output.Table{ + Header: envHeader, + Data: data, + } + r := output.RenderOutput(dataMain, outputOptions) + fmt.Fprintf(cmd.OutOrStdout(), "%s", r) + return nil + }, +} + func init() { listCmd.AddCommand(listDeployTargetsCmd) listCmd.AddCommand(listDeploymentsCmd) @@ -1267,6 +1338,7 @@ func init() { listCmd.AddCommand(listOrganizationGroupsCmd) listCmd.AddCommand(listOrganizationDeployTargetsCmd) listCmd.AddCommand(listOrganizationsCmd) + listCmd.AddCommand(listEnvironmentServicesCmd) listAllUsersCmd.Flags().StringP("email", "E", "", "The email address of a user") listUsersGroupsCmd.Flags().StringP("email", "E", "", "The email address of a user") listCmd.Flags().BoolVarP(&listAllProjects, "all-projects", "", false, "All projects (if supported)") diff --git a/docs/commands/lagoon_list.md b/docs/commands/lagoon_list.md index cbf8cbd6..e8f94cbd 100644 --- a/docs/commands/lagoon_list.md +++ b/docs/commands/lagoon_list.md @@ -38,6 +38,7 @@ List projects, environments, deployments, variables or notifications * [lagoon list deployments](lagoon_list_deployments.md) - List deployments for an environment (alias: d) * [lagoon list deploytarget-configs](lagoon_list_deploytarget-configs.md) - List deploytarget configs for a project * [lagoon list deploytargets](lagoon_list_deploytargets.md) - List all DeployTargets in Lagoon +* [lagoon list environment-services](lagoon_list_environment-services.md) - Get information about an environments services * [lagoon list environments](lagoon_list_environments.md) - List environments for a project (alias: e) * [lagoon list group-projects](lagoon_list_group-projects.md) - List projects in a group (alias: gp) * [lagoon list group-users](lagoon_list_group-users.md) - List all users in groups diff --git a/docs/commands/lagoon_list_environment-services.md b/docs/commands/lagoon_list_environment-services.md new file mode 100644 index 00000000..a17c16a5 --- /dev/null +++ b/docs/commands/lagoon_list_environment-services.md @@ -0,0 +1,39 @@ +## lagoon list environment-services + +Get information about an environments services + +``` +lagoon list environment-services [flags] +``` + +### Options + +``` + -h, --help help for environment-services +``` + +### Options inherited from parent commands + +``` + --config-file string Path to the config file to use (must be *.yml or *.yaml) + --debug Enable debugging output (if supported) + -e, --environment string Specify an environment to use + --force Force yes on prompts (if supported) + -l, --lagoon string The Lagoon instance to interact with + --no-header No header on table (if supported) + --output-csv Output as CSV (if supported) + --output-json Output as JSON (if supported) + --pretty Make JSON pretty (if supported) + -p, --project string Specify a project to use + --skip-update-check Skip checking for updates + -i, --ssh-key string Specify path to a specific SSH key to use for lagoon authentication + --ssh-publickey string Specify path to a specific SSH public key to use for lagoon authentication using ssh-agent. + This will override any public key identities defined in configuration + --strict-host-key-checking string Similar to SSH StrictHostKeyChecking (accept-new, no, ignore) (default "accept-new") + -v, --verbose Enable verbose output to stderr (if supported) +``` + +### SEE ALSO + +* [lagoon list](lagoon_list.md) - List projects, environments, deployments, variables or notifications +