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
This repository was archived by the owner on Aug 30, 2024. It is now read-only.
/coder-v1-cliPublic archive

feat: improved arg usage errors#225

Merged
cmoog merged 4 commits intomasterfromcmoog/better-arg-errors
Jan 23, 2021
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
3 changes: 2 additions & 1 deletioninternal/cmd/cmd.go
View file
Open in desktop
Original file line numberDiff line numberDiff 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 DownExpand 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 deletionsinternal/cmd/envs.go
View file
Open in desktop
Original file line numberDiff line numberDiff 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 DownExpand 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 DownExpand 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 deletioninternal/cmd/login.go
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionsinternal/cmd/rebuild.go
View file
Open in desktop
Original file line numberDiff line numberDiff 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 DownExpand 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 deletioninternal/cmd/sync.go
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionsinternal/cmd/tags.go
View file
Open in desktop
Original file line numberDiff line numberDiff 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 DownExpand 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 DownExpand 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 deletionsinternal/cmd/tokens.go
View file
Open in desktop
Original file line numberDiff line numberDiff 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 DownExpand 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 deletionsinternal/cmd/urls.go
View file
Open in desktop
Original file line numberDiff line numberDiff 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 DownExpand 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 deletionsinternal/x/xcobra/cobra.go
View file
Open in desktop
Original file line numberDiff line numberDiff 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
}
}

[8]ページ先頭

©2009-2025 Movatter.jp