mirror of https://github.com/helm/helm
parent
53ea80b2d7
commit
4cc3498608
@ -0,0 +1,88 @@
|
|||||||
|
/*
|
||||||
|
Copyright 2018 The Kubernetes Authors All rights reserved.
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package require
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/pkg/errors"
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
)
|
||||||
|
|
||||||
|
// NoArgs returns an error if any args are included.
|
||||||
|
func NoArgs(cmd *cobra.Command, args []string) error {
|
||||||
|
if len(args) > 0 {
|
||||||
|
return errors.Errorf(
|
||||||
|
"%q accepts no arguments\n\nUsage: %s",
|
||||||
|
cmd.CommandPath(),
|
||||||
|
cmd.UseLine(),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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 errors.Errorf(
|
||||||
|
"%q requires %d %s\n\nUsage: %s",
|
||||||
|
cmd.CommandPath(),
|
||||||
|
n,
|
||||||
|
pluralize("argument", n),
|
||||||
|
cmd.UseLine(),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// MaximumNArgs returns an error if there are more than N args.
|
||||||
|
func MaximumNArgs(n int) cobra.PositionalArgs {
|
||||||
|
return func(cmd *cobra.Command, args []string) error {
|
||||||
|
if len(args) > n {
|
||||||
|
return errors.Errorf(
|
||||||
|
"%q accepts at most %d %s\n\nUsage: %s",
|
||||||
|
cmd.CommandPath(),
|
||||||
|
n,
|
||||||
|
pluralize("argument", n),
|
||||||
|
cmd.UseLine(),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// MinimumNArgs returns an error if there is not at least N args.
|
||||||
|
func MinimumNArgs(n int) cobra.PositionalArgs {
|
||||||
|
return func(cmd *cobra.Command, args []string) error {
|
||||||
|
if len(args) < n {
|
||||||
|
return errors.Errorf(
|
||||||
|
"%q requires at least %d %s\n\nUsage: %s",
|
||||||
|
cmd.CommandPath(),
|
||||||
|
n,
|
||||||
|
pluralize("argument", n),
|
||||||
|
cmd.UseLine(),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func pluralize(word string, n int) string {
|
||||||
|
if n == 1 {
|
||||||
|
return word
|
||||||
|
}
|
||||||
|
return word + "s"
|
||||||
|
}
|
@ -0,0 +1,91 @@
|
|||||||
|
/*
|
||||||
|
Copyright 2018 The Kubernetes Authors All rights reserved.
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package require
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestArgs(t *testing.T) {
|
||||||
|
runTestCases(t, []testCase{{
|
||||||
|
validateFunc: NoArgs,
|
||||||
|
}, {
|
||||||
|
args: []string{"one"},
|
||||||
|
validateFunc: NoArgs,
|
||||||
|
wantError: `"root" accepts no arguments`,
|
||||||
|
}, {
|
||||||
|
args: []string{"one"},
|
||||||
|
validateFunc: ExactArgs(1),
|
||||||
|
}, {
|
||||||
|
validateFunc: ExactArgs(1),
|
||||||
|
wantError: `"root" requires 1 argument`,
|
||||||
|
}, {
|
||||||
|
validateFunc: ExactArgs(2),
|
||||||
|
wantError: `"root" requires 2 arguments`,
|
||||||
|
}, {
|
||||||
|
args: []string{"one"},
|
||||||
|
validateFunc: MaximumNArgs(1),
|
||||||
|
}, {
|
||||||
|
args: []string{"one", "two"},
|
||||||
|
validateFunc: MaximumNArgs(1),
|
||||||
|
wantError: `"root" accepts at most 1 argument`,
|
||||||
|
}, {
|
||||||
|
validateFunc: MinimumNArgs(1),
|
||||||
|
wantError: `"root" requires at least 1 argument`,
|
||||||
|
}, {
|
||||||
|
args: []string{"one", "two"},
|
||||||
|
validateFunc: MinimumNArgs(1),
|
||||||
|
}})
|
||||||
|
}
|
||||||
|
|
||||||
|
type testCase struct {
|
||||||
|
args []string
|
||||||
|
validateFunc cobra.PositionalArgs
|
||||||
|
wantError string
|
||||||
|
}
|
||||||
|
|
||||||
|
func runTestCases(t *testing.T, testCases []testCase) {
|
||||||
|
for i, tc := range testCases {
|
||||||
|
t.Run(fmt.Sprint(i), func(t *testing.T) {
|
||||||
|
cmd := &cobra.Command{
|
||||||
|
Use: "root",
|
||||||
|
Run: func(*cobra.Command, []string) {},
|
||||||
|
Args: tc.validateFunc,
|
||||||
|
}
|
||||||
|
cmd.SetArgs(tc.args)
|
||||||
|
cmd.SetOutput(ioutil.Discard)
|
||||||
|
|
||||||
|
err := cmd.Execute()
|
||||||
|
if tc.wantError == "" {
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("unexpected error, got '%v'", err)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if !strings.Contains(err.Error(), tc.wantError) {
|
||||||
|
t.Fatalf("unexpected error \n\nWANT:\n%q\n\nGOT:\n%q\n", tc.wantError, err)
|
||||||
|
}
|
||||||
|
if !strings.Contains(err.Error(), "Usage:") {
|
||||||
|
t.Fatalf("unexpected error: want Usage string\n\nGOT:\n%q\n", err)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
@ -1 +1,3 @@
|
|||||||
Error: command 'delete' requires a release name
|
Error: "helm delete" requires at least 1 argument
|
||||||
|
|
||||||
|
Usage: helm delete RELEASE_NAME [...] [flags]
|
||||||
|
@ -1 +1,3 @@
|
|||||||
Error: release name is required
|
Error: "helm get hooks" requires 1 argument
|
||||||
|
|
||||||
|
Usage: helm get hooks RELEASE_NAME [flags]
|
||||||
|
@ -1 +1,3 @@
|
|||||||
Error: release name is required
|
Error: "helm get manifest" requires 1 argument
|
||||||
|
|
||||||
|
Usage: helm get manifest RELEASE_NAME [flags]
|
||||||
|
@ -1 +1,3 @@
|
|||||||
Error: release name is required
|
Error: "helm get" requires 1 argument
|
||||||
|
|
||||||
|
Usage: helm get RELEASE_NAME [flags]
|
||||||
|
@ -1 +1,3 @@
|
|||||||
Error: release name is required
|
Error: "helm get values" requires 1 argument
|
||||||
|
|
||||||
|
Usage: helm get values RELEASE_NAME [flags]
|
||||||
|
@ -1 +1,3 @@
|
|||||||
Error: this command needs 1 argument: chart name
|
Error: "helm install" requires 1 argument
|
||||||
|
|
||||||
|
Usage: helm install [CHART] [flags]
|
||||||
|
@ -1 +1,3 @@
|
|||||||
Error: this command needs 2 arguments: release name, revision number
|
Error: "helm rollback" requires 2 arguments
|
||||||
|
|
||||||
|
Usage: helm rollback [RELEASE] [REVISION] [flags]
|
||||||
|
@ -1 +1,3 @@
|
|||||||
Error: this command needs 2 arguments: release name, chart path
|
Error: "helm upgrade" requires 2 arguments
|
||||||
|
|
||||||
|
Usage: helm upgrade [RELEASE] [CHART] [flags]
|
||||||
|
Loading…
Reference in new issue