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

feat: improved arg usage errors #225

Merged
merged 4 commits into from
Jan 23, 2021
Merged
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
3 changes: 2 additions & 1 deletion internal/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package cmd
import (
"os"

"cdr.dev/coder-cli/internal/x/xcobra"
"github.com/spf13/cobra"
"github.com/spf13/cobra/doc"
)
Expand Down Expand Up @@ -45,8 +46,8 @@ func genDocsCmd(rootCmd *cobra.Command) *cobra.Command {
return &cobra.Command{
Use: "gen-docs [dir_path]",
Short: "Generate a markdown documentation tree for the root command.",
Args: xcobra.ExactArgs(1),
Example: "coder gen-docs ./docs",
Args: cobra.ExactArgs(1),
Hidden: true,
RunE: func(_ *cobra.Command, args []string) error {
return doc.GenMarkdownTree(rootCmd, args[0])
Expand Down
5 changes: 3 additions & 2 deletions internal/cmd/envs.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os"

"cdr.dev/coder-cli/coder-sdk"
"cdr.dev/coder-cli/internal/x/xcobra"
"cdr.dev/coder-cli/pkg/clog"
"cdr.dev/coder-cli/pkg/tablewriter"

Expand Down Expand Up @@ -159,7 +160,7 @@ func createEnvCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "create [environment_name]",
Short: "create a new environment.",
Args: cobra.ExactArgs(1),
Args: xcobra.ExactArgs(1),
Long: "Create a new Coder environment.",
Example: `# create a new environment using default resource amounts
coder envs create my-new-env --image ubuntu
Expand Down Expand Up @@ -267,7 +268,7 @@ func editEnvCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "edit",
Short: "edit an existing environment and initiate a rebuild.",
Args: cobra.ExactArgs(1),
Args: xcobra.ExactArgs(1),
Long: "Edit an existing environment and initate a rebuild.",
Example: `coder envs edit back-end-env --cpu 4

Expand Down
3 changes: 2 additions & 1 deletion internal/cmd/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"cdr.dev/coder-cli/internal/config"
"cdr.dev/coder-cli/internal/loginsrv"
"cdr.dev/coder-cli/internal/version"
"cdr.dev/coder-cli/internal/x/xcobra"
"cdr.dev/coder-cli/pkg/clog"
"github.com/pkg/browser"
"github.com/spf13/cobra"
Expand All @@ -23,7 +24,7 @@ func loginCmd() *cobra.Command {
return &cobra.Command{
Use: "login [Coder Enterprise URL eg. https://my.coder.domain/]",
Short: "Authenticate this client for future operations",
Args: cobra.ExactArgs(1),
Args: xcobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
// Pull the URL from the args and do some sanity check.
rawURL := args[0]
Expand Down
5 changes: 3 additions & 2 deletions internal/cmd/rebuild.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"time"

"cdr.dev/coder-cli/coder-sdk"
"cdr.dev/coder-cli/internal/x/xcobra"
"cdr.dev/coder-cli/pkg/clog"
"github.com/briandowns/spinner"
"github.com/fatih/color"
Expand All @@ -24,7 +25,7 @@ func rebuildEnvCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "rebuild [environment_name]",
Short: "rebuild a Coder environment",
Args: cobra.ExactArgs(1),
Args: xcobra.ExactArgs(1),
Example: `coder envs rebuild front-end-env --follow
coder envs rebuild backend-env --force`,
RunE: func(cmd *cobra.Command, args []string) error {
Expand Down Expand Up @@ -144,7 +145,7 @@ func watchBuildLogCommand() *cobra.Command {
Use: "watch-build [environment_name]",
Example: "coder envs watch-build front-end-env",
Short: "trail the build log of a Coder environment",
Args: cobra.ExactArgs(1),
Args: xcobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
client, err := newClient(ctx)
Expand Down
3 changes: 2 additions & 1 deletion internal/cmd/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"cdr.dev/coder-cli/coder-sdk"
"cdr.dev/coder-cli/internal/sync"
"cdr.dev/coder-cli/internal/x/xcobra"
"cdr.dev/coder-cli/pkg/clog"
"github.com/spf13/cobra"
"golang.org/x/xerrors"
Expand All @@ -20,7 +21,7 @@ func syncCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "sync [local directory] [<env name>:<remote directory>]",
Short: "Establish a one way directory sync to a Coder environment",
Args: cobra.ExactArgs(2),
Args: xcobra.ExactArgs(2),
RunE: makeRunSync(&init),
}
cmd.Flags().BoolVar(&init, "init", false, "do initial transfer and exit")
Expand Down
5 changes: 3 additions & 2 deletions internal/cmd/tags.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"os"

"cdr.dev/coder-cli/coder-sdk"
"cdr.dev/coder-cli/internal/x/xcobra"
"cdr.dev/coder-cli/pkg/clog"
"cdr.dev/coder-cli/pkg/tablewriter"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -37,7 +38,7 @@ func tagsCreateCmd() *cobra.Command {
Short: "add an image tag",
Long: "allow users to create environments with this image tag",
Example: `coder tags create latest --image ubuntu --org default`,
Args: cobra.ExactArgs(1),
Args: xcobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
client, err := newClient(ctx)
Expand Down Expand Up @@ -139,7 +140,7 @@ func tagsRmCmd() *cobra.Command {
Use: "rm [tag]",
Short: "remove an image tag",
Example: `coder tags rm latest --image ubuntu --org default`,
Args: cobra.ExactArgs(1),
Args: xcobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
client, err := newClient(ctx)
Expand Down
7 changes: 4 additions & 3 deletions internal/cmd/tokens.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"

"cdr.dev/coder-cli/coder-sdk"
"cdr.dev/coder-cli/internal/x/xcobra"
"cdr.dev/coder-cli/pkg/tablewriter"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -55,7 +56,7 @@ func createTokensCmd() *cobra.Command {
return &cobra.Command{
Use: "create [token_name]",
Short: "create generates a new API token and prints it to stdout",
Args: cobra.ExactArgs(1),
Args: xcobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
client, err := newClient(ctx)
Expand All @@ -78,7 +79,7 @@ func rmTokenCmd() *cobra.Command {
return &cobra.Command{
Use: "rm [token_id]",
Short: "remove an API token by its unique ID",
Args: cobra.ExactArgs(1),
Args: xcobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
client, err := newClient(ctx)
Expand All @@ -97,7 +98,7 @@ func regenTokenCmd() *cobra.Command {
return &cobra.Command{
Use: "regen [token_id]",
Short: "regenerate an API token by its unique ID and print the new token to stdout",
Args: cobra.ExactArgs(1),
Args: xcobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
ctx := cmd.Context()
client, err := newClient(ctx)
Expand Down
5 changes: 3 additions & 2 deletions internal/cmd/urls.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"golang.org/x/xerrors"

"cdr.dev/coder-cli/coder-sdk"
"cdr.dev/coder-cli/internal/x/xcobra"
"cdr.dev/coder-cli/pkg/clog"
"cdr.dev/coder-cli/pkg/tablewriter"
)
Expand All @@ -26,7 +27,7 @@ func urlCmd() *cobra.Command {
lsCmd := &cobra.Command{
Use: "ls [environment_name]",
Short: "List all DevURLs for an environment",
Args: cobra.ExactArgs(1),
Args: xcobra.ExactArgs(1),
ValidArgsFunction: getEnvsForCompletion(coder.Me),
RunE: listDevURLsCmd(&outputFmt),
}
Expand Down Expand Up @@ -126,7 +127,7 @@ func createDevURLCmd() *cobra.Command {
Use: "create [env_name] [port]",
Short: "Create a new devurl for an environment",
Aliases: []string{"edit"},
Args: cobra.ExactArgs(2),
Args: xcobra.ExactArgs(2),
// Run creates or updates a devURL
RunE: func(cmd *cobra.Command, args []string) error {
var (
Expand Down
24 changes: 24 additions & 0 deletions internal/x/xcobra/cobra.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Package xcobra wraps the cobra package to provide richer functionality.
package xcobra

import (
"fmt"

"cdr.dev/coder-cli/pkg/clog"
"github.com/spf13/cobra"
)

// ExactArgs returns an error if there are not exactly n args.
func ExactArgs(n int) cobra.PositionalArgs {
return func(cmd *cobra.Command, args []string) error {
if len(args) != n {
return clog.Error(
fmt.Sprintf("accepts %d arg(s), received %d", n, len(args)),
clog.Bold("usage: ")+cmd.UseLine(),
clog.BlankLine,
clog.Tipf("use \"--help\" for more info"),
)
}
return nil
}
}
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy