Skip to content
This repository was archived by the owner on Aug 30, 2024. It is now read-only.

Migrate to cobra #86

Merged
merged 10 commits into from
Aug 10, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Add completions for env names to be enabled later
  • Loading branch information
cmoog committed Aug 10, 2020
commit be6bc28546b996a73322dea2b43259dc8be8963b
1 change: 1 addition & 0 deletions ci/integration/setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"golang.org/x/xerrors"
)

// binpath is populated during package initialization with a path to the coder binary
var binpath string

// initialize integration tests by building the coder-cli binary
Expand Down
30 changes: 19 additions & 11 deletions cmd/coder/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,38 @@ import (

"cdr.dev/coder-cli/internal/config"
"cdr.dev/coder-cli/internal/entclient"
"golang.org/x/xerrors"

"go.coder.com/flog"
)

// requireAuth exits the process with a nonzero exit code if the user is not authenticated to make requests
func requireAuth() *entclient.Client {
client, err := newClient()
if err != nil {
flog.Fatal("%v", err)
}
return client
}

func newClient() (*entclient.Client, error) {
sessionToken, err := config.Session.Read()
requireSuccess(err, "read session: %v (did you run coder login?)", err)
if err != nil {
return nil, xerrors.Errorf("read session: %v (did you run coder login?)", err)
}

rawURL, err := config.URL.Read()
requireSuccess(err, "read url: %v (did you run coder login?)", err)
if err != nil {
return nil, xerrors.Errorf("read url: %v (did you run coder login?)", err)
}

u, err := url.Parse(rawURL)
requireSuccess(err, "url misformatted: %v (try runing coder login)", err)
if err != nil {
return nil, xerrors.Errorf("url misformatted: %v (try runing coder login)", err)
}

return &entclient.Client{
BaseURL: u,
Token: sessionToken,
}
}

// requireSuccess prints the given message and format args as a fatal error if err != nil
func requireSuccess(err error, msg string, args ...interface{}) {
if err != nil {
flog.Fatal(msg, args...)
}
}, nil
}
21 changes: 13 additions & 8 deletions cmd/coder/secrets.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,18 @@ func makeSecretsCmd() *cobra.Command {
},
makeCreateSecret(),
&cobra.Command{
Use: "rm [...secret_name]",
Short: "Remove one or more secrets by name",
Args: cobra.MinimumNArgs(1),
RunE: removeSecrets,
Use: "rm [...secret_name]",
Short: "Remove one or more secrets by name",
Args: cobra.MinimumNArgs(1),
RunE: removeSecrets,
Example: "coder secrets rm mysql-password mysql-user",
},
&cobra.Command{
Use: "view [secret_name]",
Short: "View a secret by name",
Args: cobra.ExactArgs(1),
RunE: viewSecret,
Use: "view [secret_name]",
Short: "View a secret by name",
Args: cobra.ExactArgs(1),
RunE: viewSecret,
Example: "coder secrets view mysql-password",
},
)
return cmd
Expand All @@ -55,6 +57,9 @@ func makeCreateSecret() *cobra.Command {
Use: "create [secret_name]",
Short: "Create a new secret",
Long: "Create a new secret object to store application secrets and access them securely from within your environments.",
Example: `coder secrets create mysql-password --from-literal 123password
coder secrets create mysql-password --from-prompt
coder secrets create aws-credentials --from-file ./credentials.json`,
Args: func(cmd *cobra.Command, args []string) error {
if len(args) < 1 {
return xerrors.Errorf("[secret_name] is a required argument")
Expand Down
23 changes: 22 additions & 1 deletion cmd/coder/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,39 @@ import (
"go.coder.com/flog"
)

func getEnvsForCompletion() []string {
// TODO(@cmoog): Enable this if speed issue can be resolved. Otherwise, all commands will take > 1 second.
return nil

var envNames []string
client, err := newClient()
if err != nil {
return envNames
}
envs, err := getEnvs(client)
if err != nil {
return envNames
}
for _, e := range envs {
envNames = append(envNames, e.Name)
}
return envNames
}

func makeShellCmd() *cobra.Command {
return &cobra.Command{
Use: "sh [environment_name] [<command [args...]>]",
Short: "Open a shell and execute commands in a Coder environment",
Long: "Execute a remote command on the environment\\nIf no command is specified, the default shell is opened.",
Args: cobra.MinimumNArgs(1),
DisableFlagParsing: true,
ValidArgs: getEnvsForCompletion(),
RunE: shell,
Example: "coder sh backend-env",
}
}

func shell(cmd *cobra.Command, cmdArgs []string) error {
func shell(_ *cobra.Command, cmdArgs []string) error {
var (
envName = cmdArgs[0]
ctx = context.Background()
Expand Down
9 changes: 5 additions & 4 deletions cmd/coder/urls.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@ func makeURLCmd() *cobra.Command {
Short: "Interact with environment DevURLs",
}
lsCmd := &cobra.Command{
Use: "ls [env_name]",
Short: "List all DevURLs for an environment",
Args: cobra.ExactArgs(1),
RunE: makeListDevURLs(&outputFmt),
Use: "ls [environment_name]",
Short: "List all DevURLs for an environment",
Args: cobra.ExactArgs(1),
ValidArgs: getEnvsForCompletion(),
RunE: makeListDevURLs(&outputFmt),
}
lsCmd.Flags().StringVarP(&outputFmt, "output", "o", "human", "human|json")

Expand Down
6 changes: 4 additions & 2 deletions cmd/coder/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@ func makeUsersCmd() *cobra.Command {
lsCmd := &cobra.Command{
Use: "ls",
Short: "list all user accounts",
RunE: listUsers(&outputFmt),
Example: `coder users ls -o json
coder users ls -o json | jq .[] | jq -r .email`,
RunE: listUsers(&outputFmt),
}
lsCmd.Flags().StringVarP(&outputFmt, "output", "0", "human", "human | json")
lsCmd.Flags().StringVarP(&outputFmt, "output", "o", "human", "human | json")

cmd.AddCommand(lsCmd)
return cmd
Expand Down