Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix tenant flags for tenant variables list and tenant view #299

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 29 additions & 6 deletions pkg/cmd/tenant/variables/list/list.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package list

import (
"errors"
"fmt"

"github.com/MakeNowJust/heredoc/v2"
"github.com/OctopusDeploy/cli/pkg/apiclient"
"github.com/OctopusDeploy/cli/pkg/constants"
"github.com/OctopusDeploy/cli/pkg/factory"
"github.com/OctopusDeploy/cli/pkg/output"
"github.com/OctopusDeploy/cli/pkg/question/selectors"
"github.com/OctopusDeploy/cli/pkg/util/flag"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/actiontemplates"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/client"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/core"
Expand All @@ -18,8 +21,19 @@ import (
const (
LibraryVariableSetType = "Library"
ProjectType = "Project"
FlagTenant = "tenant"
)

type ListFlags struct {
Tenant *flag.Flag[string]
}

func NewListFlags() *ListFlags {
return &ListFlags{
Tenant: flag.New[string](FlagTenant, false),
}
}

type VariableValue struct {
Type string
OwnerName string
Expand Down Expand Up @@ -68,6 +82,8 @@ func NewVariableValueProjectAsJson(v *VariableValue) *VariableValueProjectAsJson
}

func NewCmdList(f factory.Factory) *cobra.Command {
listFlags := NewListFlags()

cmd := &cobra.Command{
Use: "list",
Short: "List tenant variables",
Expand All @@ -78,28 +94,35 @@ func NewCmdList(f factory.Factory) *cobra.Command {
`, constants.ExecutableName),
Aliases: []string{"ls"},
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
return fmt.Errorf("must supply tenant identifier")
if len(args) > 0 && listFlags.Tenant.Value == "" {
listFlags.Tenant.Value = args[0]
}
return listRun(cmd, f, args[0])
return listRun(cmd, f, listFlags)
},
}

flags := cmd.Flags()
flags.StringVarP(&listFlags.Tenant.Value, listFlags.Tenant.Name, "t", "", "Name or ID of the tenant to list variables for")
return cmd
}

func listRun(cmd *cobra.Command, f factory.Factory, id string) error {
func listRun(cmd *cobra.Command, f factory.Factory, flags *ListFlags) error {
client, err := f.GetSpacedClient(apiclient.NewRequester(cmd))
if err != nil {
return err
}

tenant, err := client.Tenants.GetByIdentifier(id)
tenant := flags.Tenant.Value
if tenant == "" {
return errors.New("tenant must be specified")
}

selectedTenant, err := client.Tenants.GetByIdentifier(tenant)
if err != nil {
return err
}

vars, err := client.Tenants.GetVariables(tenant)
vars, err := client.Tenants.GetVariables(selectedTenant)
if err != nil {
return err
}
Expand Down
87 changes: 39 additions & 48 deletions pkg/cmd/tenant/view/view.go
Original file line number Diff line number Diff line change
@@ -1,47 +1,40 @@
package view

import (
"errors"
"fmt"

"github.com/OctopusDeploy/cli/pkg/apiclient"
"io"

"github.com/MakeNowJust/heredoc/v2"
"github.com/OctopusDeploy/cli/pkg/constants"
"github.com/OctopusDeploy/cli/pkg/factory"
"github.com/OctopusDeploy/cli/pkg/output"
"github.com/OctopusDeploy/cli/pkg/usage"
"github.com/OctopusDeploy/cli/pkg/util/flag"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/client"
"github.com/pkg/browser"
"github.com/spf13/cobra"
)

const (
FlagWeb = "web"
FlagTenant = "tenant"
FlagWeb = "web"
)

type ViewFlags struct {
Web *flag.Flag[bool]
Tenant *flag.Flag[string]
Web *flag.Flag[bool]
}

func NewViewFlags() *ViewFlags {
return &ViewFlags{
Web: flag.New[bool](FlagWeb, false),
Tenant: flag.New[string](FlagTenant, false),
Web: flag.New[bool](FlagWeb, false),
}
}

type ViewOptions struct {
Client *client.Client
Host string
out io.Writer
idOrName string
flags *ViewFlags
}

Comment on lines -33 to -40
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@domenicsim1 I removed this struct because it seemed simplier without it. Should this be retained for unit test purposes?

func NewCmdView(f factory.Factory) *cobra.Command {
viewFlags := NewViewFlags()
cmd := &cobra.Command{
Args: usage.ExactArgs(1),
Use: "view {<name> | <id>}",
Short: "View a tenant",
Long: "View a tenant in Octopus Deploy",
Expand All @@ -50,67 +43,65 @@ func NewCmdView(f factory.Factory) *cobra.Command {
$ %[1]s tenant view 'Tenant'
`, constants.ExecutableName),
RunE: func(cmd *cobra.Command, args []string) error {
client, err := f.GetSpacedClient(apiclient.NewRequester(cmd))
if err != nil {
return err
}

if len(args) == 0 {
return fmt.Errorf("tenant identifier is required")
}

opts := &ViewOptions{
client,
f.GetCurrentHost(),
cmd.OutOrStdout(),
args[0],
viewFlags,
if len(args) > 0 && viewFlags.Tenant.Value == "" {
viewFlags.Tenant.Value = args[0]
}

return viewRun(opts)
return viewRun(cmd, f, viewFlags)
},
}

flags := cmd.Flags()
flags.StringVarP(&viewFlags.Tenant.Value, viewFlags.Tenant.Name, "t", "", "Name or ID of the tenant to list variables for")
flags.BoolVarP(&viewFlags.Web.Value, viewFlags.Web.Name, "w", false, "Open in web browser")

return cmd
}

func viewRun(opts *ViewOptions) error {
tenant, err := opts.Client.Tenants.GetByIdentifier(opts.idOrName)
func viewRun(cmd *cobra.Command, f factory.Factory, flags *ViewFlags) error {
client, err := f.GetSpacedClient(apiclient.NewRequester(cmd))
if err != nil {
return err
}

tenant := flags.Tenant.Value
if tenant == "" {
return errors.New("tenant must be specified")
}

selectedTenant, err := client.Tenants.GetByIdentifier(tenant)
if err != nil {
return err
}

fmt.Fprintf(opts.out, "%s %s\n", output.Bold(tenant.Name), output.Dimf("(%s)", tenant.ID))
out := cmd.OutOrStdout()
fmt.Fprintf(out, "%s %s\n", output.Bold(selectedTenant.Name), output.Dimf("(%s)", selectedTenant.ID))

if len(tenant.TenantTags) > 0 {
fmt.Fprintf(opts.out, "Tags: ")
if len(selectedTenant.TenantTags) > 0 {
fmt.Fprintf(out, "Tags: ")
}
for i, tag := range tenant.TenantTags {
for i, tag := range selectedTenant.TenantTags {
suffix := ", "
if i == len(tenant.TenantTags)-1 {
if i == len(selectedTenant.TenantTags)-1 {
suffix = ""
}
fmt.Fprintf(opts.out, "%s%s", tag, suffix)
fmt.Fprintf(out, "%s%s", tag, suffix)
}
if len(tenant.TenantTags) > 0 {
fmt.Fprintf(opts.out, "\n")
if len(selectedTenant.TenantTags) > 0 {
fmt.Fprintf(out, "\n")
}

if tenant.Description == "" {
fmt.Fprintln(opts.out, output.Dim(constants.NoDescription))
if selectedTenant.Description == "" {
fmt.Fprintln(out, output.Dim(constants.NoDescription))
} else {
fmt.Fprintln(opts.out, output.Dim(tenant.Description))
fmt.Fprintln(out, output.Dim(selectedTenant.Description))
}

link := fmt.Sprintf("%s/app#/%s/tenants/%s/overview", opts.Host, tenant.SpaceID, tenant.ID)
link := fmt.Sprintf("%s/app#/%s/tenants/%s/overview", f.GetCurrentHost(), selectedTenant.SpaceID, selectedTenant.ID)

// footer
fmt.Fprintf(opts.out, "View this tenant in Octopus Deploy: %s\n", output.Blue(link))
fmt.Fprintf(out, "View this tenant in Octopus Deploy: %s\n", output.Blue(link))

if opts.flags.Web.Value {
if flags.Web.Value {
browser.OpenURL(link)
}

Expand Down