Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

chore: remove org context switcher in the cli#13674

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
Emyrk merged 14 commits intomainfromstevenmasley/unified_org_cli
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes fromall 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
4 changes: 3 additions & 1 deletioncli/create.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -29,6 +29,7 @@ func (r *RootCmd) create() *serpent.Command {
parameterFlags workspaceParameterFlags
autoUpdates string
copyParametersFrom string
orgContext = NewOrganizationContext()
)
client := new(codersdk.Client)
cmd := &serpent.Command{
Expand All@@ -43,7 +44,7 @@ func (r *RootCmd) create() *serpent.Command {
),
Middleware: serpent.Chain(r.InitClient(client)),
Handler: func(inv *serpent.Invocation) error {
organization, err :=CurrentOrganization(r,inv, client)
organization, err :=orgContext.Selected(inv, client)
if err != nil {
return err
}
Expand DownExpand Up@@ -269,6 +270,7 @@ func (r *RootCmd) create() *serpent.Command {
)
cmd.Options = append(cmd.Options, parameterFlags.cliParameters()...)
cmd.Options = append(cmd.Options, parameterFlags.cliParameterDefaults()...)
orgContext.AttachOptions(cmd)
return cmd
}

Expand Down
7 changes: 0 additions & 7 deletionscli/login.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -358,13 +358,6 @@ func (r *RootCmd) login() *serpent.Command {
return xerrors.Errorf("write server url: %w", err)
}

// If the current organization cannot be fetched, then reset the organization context.
// Otherwise, organization cli commands will fail.
_, err = CurrentOrganization(r, inv, client)
if err != nil {
_ = config.Organization().Delete()
}

_, _ = fmt.Fprintf(inv.Stdout, Caret+"Welcome to Coder, %s! You're authenticated.\n", pretty.Sprint(cliui.DefaultStyles.Keyword, resp.Username))
return nil
},
Expand Down
25 changes: 0 additions & 25 deletionscli/login_test.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -5,11 +5,9 @@ import (
"fmt"
"net/http"
"net/http/httptest"
"os"
"runtime"
"testing"

"github.com/google/uuid"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

Expand DownExpand Up@@ -424,29 +422,6 @@ func TestLogin(t *testing.T) {
require.NotEqual(t, client.SessionToken(), sessionFile)
})

// Login should reset the configured organization if the user is not a member
t.Run("ResetOrganization", func(t *testing.T) {
t.Parallel()
client := coderdtest.New(t, nil)
coderdtest.CreateFirstUser(t, client)
root, cfg := clitest.New(t, "login", client.URL.String(), "--token", client.SessionToken())

notRealOrg := uuid.NewString()
err := cfg.Organization().Write(notRealOrg)
require.NoError(t, err, "write bad org to config")

err = root.Run()
require.NoError(t, err)
sessionFile, err := cfg.Session().Read()
require.NoError(t, err)
require.NotEqual(t, client.SessionToken(), sessionFile)

// Organization config should be deleted since the org does not exist
selected, err := cfg.Organization().Read()
require.ErrorIs(t, err, os.ErrNotExist)
require.NotEqual(t, selected, notRealOrg)
})

t.Run("KeepOrganizationContext", func(t *testing.T) {
t.Parallel()
client := coderdtest.New(t, nil)
Expand Down
218 changes: 33 additions & 185 deletionscli/organization.go
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,19 @@
package cli

import (
"errors"
"fmt"
"os"
"slices"
"strings"

"golang.org/x/xerrors"

"github.com/coder/coder/v2/cli/cliui"
"github.com/coder/coder/v2/cli/config"
"github.com/coder/coder/v2/codersdk"
"github.com/coder/pretty"
"github.com/coder/serpent"
)

func (r *RootCmd) organizations() *serpent.Command {
orgContext := NewOrganizationContext()

cmd := &serpent.Command{
Use: "organizations [subcommand]",
Short: "Organization related commands",
Expand All@@ -26,188 +23,18 @@ func (r *RootCmd) organizations() *serpent.Command {
return inv.Command.HelpHandler(inv)
},
Children: []*serpent.Command{
r.currentOrganization(),
r.switchOrganization(),
r.showOrganization(orgContext),
r.createOrganization(),
r.organizationMembers(),
r.organizationRoles(),
r.organizationMembers(orgContext),
r.organizationRoles(orgContext),
},
}

cmd.Options = serpent.OptionSet{}
orgContext.AttachOptions(cmd)
return cmd
}

func (r *RootCmd) switchOrganization() *serpent.Command {
client := new(codersdk.Client)

cmd := &serpent.Command{
Use: "set <organization name | ID>",
Short: "set the organization used by the CLI. Pass an empty string to reset to the default organization.",
Long: "set the organization used by the CLI. Pass an empty string to reset to the default organization.\n" + FormatExamples(
Example{
Description: "Remove the current organization and defer to the default.",
Command: "coder organizations set ''",
},
Example{
Description: "Switch to a custom organization.",
Command: "coder organizations set my-org",
},
),
Middleware: serpent.Chain(
r.InitClient(client),
serpent.RequireRangeArgs(0, 1),
),
Options: serpent.OptionSet{},
Handler: func(inv *serpent.Invocation) error {
conf := r.createConfig()
orgs, err := client.OrganizationsByUser(inv.Context(), codersdk.Me)
if err != nil {
return xerrors.Errorf("failed to get organizations: %w", err)
}
// Keep the list of orgs sorted
slices.SortFunc(orgs, func(a, b codersdk.Organization) int {
return strings.Compare(a.Name, b.Name)
})

var switchToOrg string
if len(inv.Args) == 0 {
// Pull switchToOrg from a prompt selector, rather than command line
// args.
switchToOrg, err = promptUserSelectOrg(inv, conf, orgs)
if err != nil {
return err
}
} else {
switchToOrg = inv.Args[0]
}

// If the user passes an empty string, we want to remove the organization
// from the config file. This will defer to default behavior.
if switchToOrg == "" {
err := conf.Organization().Delete()
if err != nil && !errors.Is(err, os.ErrNotExist) {
return xerrors.Errorf("failed to unset organization: %w", err)
}
_, _ = fmt.Fprintf(inv.Stdout, "Organization unset\n")
} else {
// Find the selected org in our list.
index := slices.IndexFunc(orgs, func(org codersdk.Organization) bool {
return org.Name == switchToOrg || org.ID.String() == switchToOrg
})
if index < 0 {
// Using this error for better error message formatting
err := &codersdk.Error{
Response: codersdk.Response{
Message: fmt.Sprintf("Organization %q not found. Is the name correct, and are you a member of it?", switchToOrg),
Detail: "Ensure the organization argument is correct and you are a member of it.",
},
Helper: fmt.Sprintf("Valid organizations you can switch to: %s", strings.Join(orgNames(orgs), ", ")),
}
return err
}

// Always write the uuid to the config file. Names can change.
err := conf.Organization().Write(orgs[index].ID.String())
if err != nil {
return xerrors.Errorf("failed to write organization to config file: %w", err)
}
}

// Verify it worked.
current, err := CurrentOrganization(r, inv, client)
if err != nil {
// An SDK error could be a permission error. So offer the advice to unset the org
// and reset the context.
var sdkError *codersdk.Error
if errors.As(err, &sdkError) {
if sdkError.Helper == "" && sdkError.StatusCode() != 500 {
sdkError.Helper = `If this error persists, try unsetting your org with 'coder organizations set ""'`
}
return sdkError
}
return xerrors.Errorf("failed to get current organization: %w", err)
}

_, _ = fmt.Fprintf(inv.Stdout, "Current organization context set to %s (%s)\n", current.Name, current.ID.String())
return nil
},
}

return cmd
}

// promptUserSelectOrg will prompt the user to select an organization from a list
// of their organizations.
func promptUserSelectOrg(inv *serpent.Invocation, conf config.Root, orgs []codersdk.Organization) (string, error) {
// Default choice
var defaultOrg string
// Comes from config file
if conf.Organization().Exists() {
defaultOrg, _ = conf.Organization().Read()
}

// No config? Comes from default org in the list
if defaultOrg == "" {
defIndex := slices.IndexFunc(orgs, func(org codersdk.Organization) bool {
return org.IsDefault
})
if defIndex >= 0 {
defaultOrg = orgs[defIndex].Name
}
}

// Defer to first org
if defaultOrg == "" && len(orgs) > 0 {
defaultOrg = orgs[0].Name
}

// Ensure the `defaultOrg` value is an org name, not a uuid.
// If it is a uuid, change it to the org name.
index := slices.IndexFunc(orgs, func(org codersdk.Organization) bool {
return org.ID.String() == defaultOrg || org.Name == defaultOrg
})
if index >= 0 {
defaultOrg = orgs[index].Name
}

// deselectOption is the option to delete the organization config file and defer
// to default behavior.
const deselectOption = "[Default]"
if defaultOrg == "" {
defaultOrg = deselectOption
}

// Pull value from a prompt
_, _ = fmt.Fprintln(inv.Stdout, pretty.Sprint(cliui.DefaultStyles.Wrap, "Select an organization below to set the current CLI context to:"))
value, err := cliui.Select(inv, cliui.SelectOptions{
Options: append([]string{deselectOption}, orgNames(orgs)...),
Default: defaultOrg,
Size: 10,
HideSearch: false,
})
if err != nil {
return "", err
}
// Deselect is an alias for ""
if value == deselectOption {
value = ""
}

return value, nil
}

// orgNames is a helper function to turn a list of organizations into a list of
// their names as strings.
func orgNames(orgs []codersdk.Organization) []string {
names := make([]string, 0, len(orgs))
for _, org := range orgs {
names = append(names, org.Name)
}
return names
}

func (r *RootCmd) currentOrganization() *serpent.Command {
func (r *RootCmd) showOrganization(orgContext *OrganizationContext) *serpent.Command {
var (
stringFormat func(orgs []codersdk.Organization) (string, error)
client = new(codersdk.Client)
Expand All@@ -226,8 +53,29 @@ func (r *RootCmd) currentOrganization() *serpent.Command {
onlyID = false
)
cmd := &serpent.Command{
Use: "show [current|me|uuid]",
Short: "Show the organization, if no argument is given, the organization currently in use will be shown.",
Use: "show [\"selected\"|\"me\"|uuid|org_name]",
Short: "Show the organization. " +
"Using \"selected\" will show the selected organization from the \"--org\" flag. " +
"Using \"me\" will show all organizations you are a member of.",
Long: FormatExamples(
Example{
Description: "coder org show selected",
Command: "Shows the organizations selected with '--org=<org_name>'. " +
"This organization is the organization used by the cli.",
},
Example{
Description: "coder org show me",
Command: "List of all organizations you are a member of.",
},
Example{
Description: "coder org show developers",
Command: "Show organization with name 'developers'",
},
Example{
Description: "coder org show 90ee1875-3db5-43b3-828e-af3687522e43",
Command: "Show organization with the given ID.",
},
),
Middleware: serpent.Chain(
r.InitClient(client),
serpent.RequireRangeArgs(0, 1),
Expand All@@ -242,22 +90,22 @@ func (r *RootCmd) currentOrganization() *serpent.Command {
},
},
Handler: func(inv *serpent.Invocation) error {
orgArg := "current"
orgArg := "selected"
if len(inv.Args) >= 1 {
orgArg = inv.Args[0]
}

var orgs []codersdk.Organization
var err error
switch strings.ToLower(orgArg) {
case "current":
case "selected":
stringFormat = func(orgs []codersdk.Organization) (string, error) {
if len(orgs) != 1 {
return "", xerrors.Errorf("expected 1 organization, got %d", len(orgs))
}
return fmt.Sprintf("Current CLI Organization: %s (%s)\n", orgs[0].Name, orgs[0].ID.String()), nil
}
org, err :=CurrentOrganization(r,inv, client)
org, err :=orgContext.Selected(inv, client)
if err != nil {
return err
}
Expand Down
Loading
Loading

[8]ページ先頭

©2009-2025 Movatter.jp