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
Next Next commit
Migrate to urfave/cli/v2
  • Loading branch information
cmoog committed Aug 7, 2020
commit 8212be0d7b6ccb0f8c40f091875d5079afb1c4e7
9 changes: 3 additions & 6 deletions ci/integration/secrets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func TestSecrets(t *testing.T) {
)

// this tests the "Value:" prompt fallback
c.Run(ctx, fmt.Sprintf("echo %s | coder secrets create %s --from-prompt", value, name)).Assert(t,
c.Run(ctx, fmt.Sprintf("echo %s | coder secrets create --from-prompt %s", value, name)).Assert(t,
tcli.Success(),
tcli.StderrEmpty(),
)
Expand Down Expand Up @@ -70,7 +70,7 @@ func TestSecrets(t *testing.T) {

name, value = randString(8), randString(8)

c.Run(ctx, fmt.Sprintf("coder secrets create %s --from-literal %s", name, value)).Assert(t,
c.Run(ctx, fmt.Sprintf("coder secrets create --from-literal %s %s", value, name)).Assert(t,
tcli.Success(),
tcli.StderrEmpty(),
)
Expand All @@ -84,10 +84,7 @@ func TestSecrets(t *testing.T) {
c.Run(ctx, fmt.Sprintf("echo %s > ~/secret.json", value)).Assert(t,
tcli.Success(),
)
c.Run(ctx, fmt.Sprintf("coder secrets create %s --from-literal %s --from-file ~/secret.json", name, value)).Assert(t,
tcli.Error(),
)
c.Run(ctx, fmt.Sprintf("coder secrets create %s --from-file ~/secret.json", name)).Assert(t,
c.Run(ctx, fmt.Sprintf("coder secrets create --from-file ~/secret.json %s", name)).Assert(t,
tcli.Success(),
)
//
Expand Down
10 changes: 5 additions & 5 deletions cmd/coder/configssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,30 @@ import (

"cdr.dev/coder-cli/internal/config"
"cdr.dev/coder-cli/internal/entclient"
"github.com/urfave/cli"
"github.com/urfave/cli/v2"
"golang.org/x/xerrors"
)

func makeConfigSSHCmd() cli.Command {
func makeConfigSSHCmd() *cli.Command {
var (
configpath string
remove = false
)

return cli.Command{
return &cli.Command{
Name: "config-ssh",
Usage: "Configure SSH to access Coder environments",
Description: "Inject the proper OpenSSH configuration into your local SSH config file.",
Action: configSSH(&configpath, &remove),
Flags: []cli.Flag{
cli.StringFlag{
&cli.StringFlag{
Name: "filepath",
Usage: "overide the default path of your ssh config file",
Value: filepath.Join(os.Getenv("HOME"), ".ssh", "config"),
TakesFile: true,
Destination: &configpath,
},
cli.BoolFlag{
&cli.BoolFlag{
Name: "remove",
Usage: "remove the auto-generated Coder Enterprise ssh config",
Destination: &remove,
Expand Down
11 changes: 6 additions & 5 deletions cmd/coder/envs.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@ import (
"os"

"cdr.dev/coder-cli/internal/x/xtabwriter"
"github.com/urfave/cli"
"github.com/urfave/cli/v2"
"golang.org/x/xerrors"
)

func makeEnvsCommand() cli.Command {
func makeEnvsCommand() *cli.Command {
var outputFmt string
return cli.Command{
return &cli.Command{
Name: "envs",
Usage: "Interact with Coder environments",
Description: "Perform operations on the Coder environments owned by the active user.",
Action: exitHelp,
Subcommands: []cli.Command{
Subcommands: []*cli.Command{
{
Name: "ls",
Usage: "list all environments owned by the active user",
Expand Down Expand Up @@ -48,8 +48,9 @@ func makeEnvsCommand() cli.Command {
return nil
},
Flags: []cli.Flag{
cli.StringFlag{
&cli.StringFlag{
Name: "output",
Aliases: []string{"o"},
Usage: "json | human",
Value: "human",
Destination: &outputFmt,
Expand Down
6 changes: 3 additions & 3 deletions cmd/coder/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ import (
"cdr.dev/coder-cli/internal/config"
"cdr.dev/coder-cli/internal/loginsrv"
"github.com/pkg/browser"
"github.com/urfave/cli"
"github.com/urfave/cli/v2"
"golang.org/x/xerrors"

"go.coder.com/flog"
)

func makeLoginCmd() cli.Command {
return cli.Command{
func makeLoginCmd() *cli.Command {
return &cli.Command{
Name: "login",
Usage: "Authenticate this client for future operations",
ArgsUsage: "[Coder Enterprise URL eg. http://my.coder.domain/]",
Expand Down
6 changes: 3 additions & 3 deletions cmd/coder/logout.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import (
"os"

"cdr.dev/coder-cli/internal/config"
"github.com/urfave/cli"
"github.com/urfave/cli/v2"
"golang.org/x/xerrors"

"go.coder.com/flog"
)

func makeLogoutCmd() cli.Command {
return cli.Command{
func makeLogoutCmd() *cli.Command {
return &cli.Command{
Name: "logout",
Usage: "Remove local authentication credentials if any exist",
Action: logout,
Expand Down
9 changes: 4 additions & 5 deletions cmd/coder/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"runtime"

"cdr.dev/coder-cli/internal/x/xterminal"
"github.com/urfave/cli"
"github.com/urfave/cli/v2"

"go.coder.com/flog"
)
Expand All @@ -35,14 +35,12 @@ func main() {
app.Name = "coder"
app.Usage = "coder provides a CLI for working with an existing Coder Enterprise installation"
app.Version = fmt.Sprintf("%s %s %s/%s", version, runtime.Version(), runtime.GOOS, runtime.GOARCH)
app.Author = "Coder Technologies Inc."
app.CommandNotFound = func(c *cli.Context, s string) {
flog.Fatal("command %q not found", s)
}
app.Email = "support@coder.com"
app.Action = exitHelp

app.Commands = []cli.Command{
app.Commands = []*cli.Command{
makeLoginCmd(),
makeLogoutCmd(),
makeShellCmd(),
Expand All @@ -59,6 +57,7 @@ func main() {
}
}

func exitHelp(c *cli.Context) {
func exitHelp(c *cli.Context) error {
cli.ShowCommandHelpAndExit(c, c.Command.FullName(), 1)
return nil
}
24 changes: 12 additions & 12 deletions cmd/coder/secrets.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,19 @@ import (
"cdr.dev/coder-cli/internal/entclient"
"cdr.dev/coder-cli/internal/x/xtabwriter"
"github.com/manifoldco/promptui"
"github.com/urfave/cli"
"github.com/urfave/cli/v2"
"golang.org/x/xerrors"

"go.coder.com/flog"
)

func makeSecretsCmd() cli.Command {
return cli.Command{
func makeSecretsCmd() *cli.Command {
return &cli.Command{
Name: "secrets",
Usage: "Interact with Coder Secrets",
Description: "Interact with secrets objects owned by the active user.",
Action: exitHelp,
Subcommands: []cli.Command{
Subcommands: []*cli.Command{
{
Name: "ls",
Usage: "List all secrets owned by the active user",
Expand All @@ -43,15 +43,15 @@ func makeSecretsCmd() cli.Command {
}
}

func makeCreateSecret() cli.Command {
func makeCreateSecret() *cli.Command {
var (
fromFile string
fromLiteral string
fromPrompt bool
description string
)

return cli.Command{
return &cli.Command{
Name: "create",
Usage: "Create a new secret",
Description: "Create a new secret object to store application secrets and access them securely from within your environments.",
Expand Down Expand Up @@ -114,23 +114,23 @@ func makeCreateSecret() cli.Command {
return nil
},
Flags: []cli.Flag{
cli.StringFlag{
&cli.StringFlag{
Name: "from-file",
Usage: "a file from which to read the value of the secret",
TakesFile: true,
Destination: &fromFile,
},
cli.StringFlag{
&cli.StringFlag{
Name: "from-literal",
Usage: "the value of the secret",
Destination: &fromLiteral,
},
cli.BoolFlag{
&cli.BoolFlag{
Name: "from-prompt",
Usage: "enter the secret value through a terminal prompt",
Destination: &fromPrompt,
},
cli.StringFlag{
&cli.StringFlag{
Name: "description",
Usage: "a description of the secret",
Destination: &description,
Expand Down Expand Up @@ -197,10 +197,10 @@ func removeSecrets(c *cli.Context) error {
for _, n := range names {
err := client.DeleteSecretByName(n)
if err != nil {
flog.Error("Failed to delete secret: %v", err)
flog.Error("failed to delete secret %q: %v", n, err)
errorSeen = true
} else {
flog.Info("Successfully deleted secret %q", n)
flog.Success("successfully deleted secret %q", n)
}
}
if errorSeen {
Expand Down
7 changes: 3 additions & 4 deletions cmd/coder/shell.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"cdr.dev/coder-cli/internal/activity"
"cdr.dev/coder-cli/internal/x/xterminal"
"cdr.dev/wsep"
"github.com/urfave/cli"
"github.com/urfave/cli/v2"
"golang.org/x/crypto/ssh/terminal"
"golang.org/x/time/rate"
"golang.org/x/xerrors"
Expand All @@ -19,14 +19,13 @@ import (
"go.coder.com/flog"
)

func makeShellCmd() cli.Command {
return cli.Command{
func makeShellCmd() *cli.Command {
return &cli.Command{
Name: "sh",
Usage: "Open a shell and execute commands in a Coder environment",
Description: "Execute a remote command on the environment\\nIf no command is specified, the default shell is opened.",
ArgsUsage: "[env_name] [<command [args...]>]",
SkipFlagParsing: true,
SkipArgReorder: true,
Before: func(c *cli.Context) error {
if c.Args().First() == "" {
return xerrors.New("argument [env_name] is required")
Expand Down
8 changes: 4 additions & 4 deletions cmd/coder/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ import (
"strings"

"cdr.dev/coder-cli/internal/sync"
"github.com/urfave/cli"
"github.com/urfave/cli/v2"
"golang.org/x/xerrors"

"go.coder.com/flog"
)

func makeSyncCmd() cli.Command {
func makeSyncCmd() *cli.Command {
var init bool
return cli.Command{
return &cli.Command{
Name: "sync",
Usage: "Establish a one way directory sync to a Coder environment",
ArgsUsage: "[local directory] [<env name>:<remote directory>]",
Expand All @@ -29,7 +29,7 @@ func makeSyncCmd() cli.Command {
},
Action: makeRunSync(&init),
Flags: []cli.Flag{
cli.BoolFlag{
&cli.BoolFlag{
Name: "init",
Usage: "do initial transfer and exit",
Destination: &init,
Expand Down
19 changes: 10 additions & 9 deletions cmd/coder/urls.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,19 @@ import (
"strings"

"cdr.dev/coder-cli/internal/x/xtabwriter"
"github.com/urfave/cli"
"github.com/urfave/cli/v2"
"golang.org/x/xerrors"

"go.coder.com/flog"
)

func makeURLCmd() cli.Command {
func makeURLCmd() *cli.Command {
var outputFmt string
return cli.Command{
return &cli.Command{
Name: "urls",
Usage: "Interact with environment DevURLs",
Action: exitHelp,
Subcommands: []cli.Command{
Subcommands: []*cli.Command{
makeCreateDevURL(),
{
Name: "ls",
Expand All @@ -39,8 +39,9 @@ func makeURLCmd() cli.Command {
},
Action: makeListDevURLs(&outputFmt),
Flags: []cli.Flag{
cli.StringFlag{
&cli.StringFlag{
Name: "output",
Aliases: []string{"o"},
Usage: "human | json",
Value: "human",
Destination: &outputFmt,
Expand Down Expand Up @@ -137,12 +138,12 @@ func makeListDevURLs(outputFmt *string) func(c *cli.Context) error {
}
}

func makeCreateDevURL() cli.Command {
func makeCreateDevURL() *cli.Command {
var (
access string
urlname string
)
return cli.Command{
return &cli.Command{
Name: "create",
Usage: "Create a new devurl for an environment",
ArgsUsage: "[env_name] [port] [--access <level>] [--name <name>]",
Expand All @@ -154,13 +155,13 @@ func makeCreateDevURL() cli.Command {
return nil
},
Flags: []cli.Flag{
cli.StringFlag{
&cli.StringFlag{
Name: "access",
Usage: "Set DevURL access to [private | org | authed | public]",
Value: "private",
Destination: &access,
},
cli.StringFlag{
&cli.StringFlag{
Name: "name",
Usage: "DevURL name",
Required: true,
Expand Down
Loading