Replace flag text with const (#1391)

This commit is contained in:
Ta-Ching Chen
2019-11-09 22:13:35 +08:00
committed by GitHub
parent 6f5f6900b6
commit d3d8ff6c1b
73 changed files with 867 additions and 926 deletions
+11 -12
View File
@@ -30,10 +30,10 @@ func Commands() *cobra.Command {
RunE: wrapper.Wrapper(Create),
}
wrapper.SetFlags(createCmd, flag.FlagSet{
Required: []flag.Flag{flag.HtUrlFlag, flag.HtFnNameFlag},
Optional: []flag.Flag{flag.HtNameFlag, flag.HtMethodFlag, flag.HtIngressRuleFlag,
flag.HtIngressAnnotationFlag, flag.HtIngressTLSFlag, flag.HtIngressFlag,
flag.HtFnWeightFlag, flag.HtHostFlag, flag.NamespaceFunctionFlag, flag.SpecSaveFlag},
Required: []flag.Flag{flag.HtUrl, flag.HtFnName},
Optional: []flag.Flag{flag.HtName, flag.HtMethod, flag.HtIngressRule,
flag.HtIngressAnnotation, flag.HtIngressTLS, flag.HtIngress,
flag.HtFnWeight, flag.HtHost, flag.NamespaceFunction, flag.SpecSave},
})
getCmd := &cobra.Command{
@@ -43,7 +43,7 @@ func Commands() *cobra.Command {
RunE: wrapper.Wrapper(Get),
}
wrapper.SetFlags(getCmd, flag.FlagSet{
Required: []flag.Flag{flag.HtNameFlag},
Required: []flag.Flag{flag.HtName},
})
updateCmd := &cobra.Command{
@@ -53,10 +53,10 @@ func Commands() *cobra.Command {
RunE: wrapper.Wrapper(Update),
}
wrapper.SetFlags(updateCmd, flag.FlagSet{
Required: []flag.Flag{flag.HtNameFlag},
Optional: []flag.Flag{flag.NamespaceTriggerFlag, flag.HtFnNameFlag, flag.HtUrlFlag,
flag.HtMethodFlag, flag.HtIngressRuleFlag, flag.HtIngressAnnotationFlag,
flag.HtIngressTLSFlag, flag.HtIngressFlag, flag.HtFnWeightFlag, flag.HtHostFlag},
Required: []flag.Flag{flag.HtName},
Optional: []flag.Flag{flag.NamespaceTrigger, flag.HtFnName, flag.HtUrl,
flag.HtMethod, flag.HtIngressRule, flag.HtIngressAnnotation,
flag.HtIngressTLS, flag.HtIngress, flag.HtFnWeight, flag.HtHost},
})
deleteCmd := &cobra.Command{
@@ -66,8 +66,7 @@ func Commands() *cobra.Command {
RunE: wrapper.Wrapper(Delete),
}
wrapper.SetFlags(deleteCmd, flag.FlagSet{
Required: []flag.Flag{flag.HtNameFlag},
Optional: []flag.Flag{flag.NamespaceTriggerFlag, flag.HtFnFilterFlag},
Optional: []flag.Flag{flag.HtName, flag.HtFnFilter, flag.NamespaceTrigger},
})
listCmd := &cobra.Command{
@@ -77,7 +76,7 @@ func Commands() *cobra.Command {
RunE: wrapper.Wrapper(List),
}
wrapper.SetFlags(listCmd, flag.FlagSet{
Optional: []flag.Flag{flag.NamespaceTriggerFlag, flag.HtFnFilterFlag},
Optional: []flag.Flag{flag.NamespaceTrigger, flag.HtFnFilter},
})
command := &cobra.Command{
+38 -41
View File
@@ -31,6 +31,7 @@ import (
"github.com/fission/fission/pkg/fission-cli/cliwrapper/cli"
"github.com/fission/fission/pkg/fission-cli/cmd/spec"
"github.com/fission/fission/pkg/fission-cli/console"
flagkey "github.com/fission/fission/pkg/fission-cli/flag/key"
"github.com/fission/fission/pkg/fission-cli/util"
)
@@ -39,28 +40,28 @@ type CreateSubCommand struct {
trigger *fv1.HTTPTrigger
}
func Create(flags cli.Input) error {
c, err := util.GetServer(flags)
func Create(input cli.Input) error {
c, err := util.GetServer(input)
if err != nil {
return err
}
opts := CreateSubCommand{
client: c,
}
return opts.do(flags)
return opts.do(input)
}
func (opts *CreateSubCommand) do(flags cli.Input) error {
err := opts.complete(flags)
func (opts *CreateSubCommand) do(input cli.Input) error {
err := opts.complete(input)
if err != nil {
return err
}
return opts.run(flags)
return opts.run(input)
}
func (opts *CreateSubCommand) complete(flags cli.Input) error {
functionList := flags.StringSlice("function")
functionWeightsList := flags.IntSlice("weight")
func (opts *CreateSubCommand) complete(input cli.Input) error {
functionList := input.StringSlice(flagkey.HtFnName)
functionWeightsList := input.IntSlice(flagkey.HtFnWeight)
if len(functionList) == 0 {
return errors.New("need a function name to create a trigger, use --function")
@@ -71,8 +72,13 @@ func (opts *CreateSubCommand) complete(flags cli.Input) error {
return err
}
triggerName := flags.String("name")
fnNamespace := flags.String("fnNamespace")
triggerName := input.String(flagkey.HtName)
// just name triggers by uuid.
if triggerName == "" {
console.Warn(fmt.Sprintf("--%v will be soon marked as required flag, see 'help' for details", flagkey.HtName))
triggerName = uuid.NewV4().String()
}
fnNamespace := input.String(flagkey.NamespaceFunction)
m := &metav1.ObjectMeta{
Name: triggerName,
@@ -87,44 +93,35 @@ func (opts *CreateSubCommand) complete(flags cli.Input) error {
return errors.New("duplicate trigger exists, choose a different name or leave it empty for fission to auto-generate it")
}
triggerUrl := flags.String("url")
if len(triggerUrl) == 0 {
return errors.New("need a trigger URL, use --url")
}
if !strings.HasPrefix(triggerUrl, "/") {
triggerUrl := input.String(flagkey.HtUrl)
if triggerUrl == "/" {
return errors.New("url with only root path is not allowed")
} else if !strings.HasPrefix(triggerUrl, "/") {
triggerUrl = fmt.Sprintf("/%s", triggerUrl)
}
method, err := GetMethod(flags.String("method"))
method, err := GetMethod(input.String(flagkey.HtMethod))
if err != nil {
return err
}
// For Specs, the spec validate checks for function reference
if !flags.Bool("spec") {
if !input.Bool(flagkey.SpecSave) {
err = util.CheckFunctionExistence(opts.client, functionList, fnNamespace)
if err != nil {
console.Warn(err.Error())
}
}
createIngress := flags.Bool("createingress")
createIngress := input.Bool(flagkey.HtIngress)
ingressConfig, err := GetIngressConfig(
flags.StringSlice("ingressannotation"), flags.String("ingressrule"),
flags.String("ingresstls"), triggerUrl, nil)
input.StringSlice(flagkey.HtIngressAnnotation), input.String(flagkey.HtIngressRule),
input.String(flagkey.HtIngressTLS), triggerUrl, nil)
if err != nil {
return errors.Wrap(err, "error parsing ingress configuration")
}
host := flags.String("host")
if flags.IsSet("host") {
console.Warn(fmt.Sprintf("--host is now marked as deprecated, see 'help' for details"))
}
// just name triggers by uuid.
if triggerName == "" {
triggerName = uuid.NewV4().String()
}
host := input.String(flagkey.HtHost)
opts.trigger = &fv1.HTTPTrigger{
Metadata: metav1.ObjectMeta{
@@ -144,9 +141,9 @@ func (opts *CreateSubCommand) complete(flags cli.Input) error {
return nil
}
func (opts *CreateSubCommand) run(flags cli.Input) error {
func (opts *CreateSubCommand) run(input cli.Input) error {
// if we're writing a spec, don't call the API
if flags.Bool("spec") {
if input.Bool(flagkey.SpecSave) {
specFile := fmt.Sprintf("route-%v.yaml", opts.trigger.Metadata.Name)
err := spec.SpecSave(*opts.trigger, specFile)
if err != nil {
@@ -168,23 +165,23 @@ func (opts *CreateSubCommand) run(flags cli.Input) error {
// GetMethod returns one of HTTP method
func GetMethod(method string) (string, error) {
switch strings.ToUpper(method) {
case "GET":
case http.MethodGet:
return http.MethodGet, nil
case "HEAD":
case http.MethodHead:
return http.MethodHead, nil
case "POST":
case http.MethodPost:
return http.MethodPost, nil
case "PUT":
case http.MethodPut:
return http.MethodPut, nil
case "PATCH":
case http.MethodPatch:
return http.MethodPatch, nil
case "DELETE":
case http.MethodDelete:
return http.MethodDelete, nil
case "CONNECT":
case http.MethodConnect:
return http.MethodConnect, nil
case "OPTIONS":
case http.MethodOptions:
return http.MethodOptions, nil
case "TRACE":
case http.MethodTrace:
return http.MethodTrace, nil
default:
return "", fmt.Errorf("invalid or unsupported HTTP Method %v", method)
+14 -13
View File
@@ -25,6 +25,7 @@ import (
"github.com/fission/fission/pkg/controller/client"
"github.com/fission/fission/pkg/fission-cli/cliwrapper/cli"
flagkey "github.com/fission/fission/pkg/fission-cli/flag/key"
"github.com/fission/fission/pkg/fission-cli/util"
"github.com/fission/fission/pkg/utils"
)
@@ -36,38 +37,38 @@ type DeleteSubCommand struct {
namespace string
}
func Delete(flags cli.Input) error {
c, err := util.GetServer(flags)
func Delete(input cli.Input) error {
c, err := util.GetServer(input)
if err != nil {
return err
}
opts := DeleteSubCommand{
client: c,
}
return opts.do(flags)
return opts.do(input)
}
func (opts *DeleteSubCommand) do(flags cli.Input) error {
err := opts.complete(flags)
func (opts *DeleteSubCommand) do(input cli.Input) error {
err := opts.complete(input)
if err != nil {
return err
}
return opts.run(flags)
return opts.run(input)
}
func (opts *DeleteSubCommand) complete(flags cli.Input) error {
opts.triggerName = flags.String("name")
opts.functionName = flags.String("function")
func (opts *DeleteSubCommand) complete(input cli.Input) error {
opts.triggerName = input.String(flagkey.HtName)
opts.functionName = input.String(flagkey.HtFnName)
if len(opts.triggerName) == 0 && len(opts.functionName) == 0 {
return errors.New("need --name or --function")
return errors.Errorf("need --%v or --%v", flagkey.HtName, flagkey.HtFnName)
} else if len(opts.triggerName) > 0 && len(opts.functionName) > 0 {
return errors.New("need either of --name or --function and not both arguments")
return errors.Errorf("need either of --%v or --%v and not both arguments", flagkey.HtName, flagkey.HtFnName)
}
opts.namespace = flags.String("triggerNamespace")
opts.namespace = input.String(flagkey.NamespaceTrigger)
return nil
}
func (opts *DeleteSubCommand) run(flags cli.Input) error {
func (opts *DeleteSubCommand) run(input cli.Input) error {
triggers, err := opts.client.HTTPTriggerList(opts.namespace)
if err != nil {
return errors.Wrap(err, "error getting HTTP trigger list")
+10 -25
View File
@@ -28,48 +28,33 @@ import (
fv1 "github.com/fission/fission/pkg/apis/fission.io/v1"
"github.com/fission/fission/pkg/controller/client"
"github.com/fission/fission/pkg/fission-cli/cliwrapper/cli"
flagkey "github.com/fission/fission/pkg/fission-cli/flag/key"
"github.com/fission/fission/pkg/fission-cli/util"
)
type GetSubCommand struct {
client *client.Client
trigger string
namespace string
client *client.Client
}
func Get(flags cli.Input) error {
c, err := util.GetServer(flags)
func Get(input cli.Input) error {
c, err := util.GetServer(input)
if err != nil {
return err
}
opts := GetSubCommand{
client: c,
}
return opts.do(flags)
return opts.do(input)
}
func (opts *GetSubCommand) do(flags cli.Input) error {
err := opts.complete(flags)
if err != nil {
return err
}
return opts.run(flags)
func (opts *GetSubCommand) do(input cli.Input) error {
return opts.run(input)
}
func (opts *GetSubCommand) complete(flags cli.Input) error {
opts.trigger = flags.String("name")
opts.namespace = flags.String("fnNamespace")
if len(opts.trigger) <= 0 {
return errors.New("need a trigger name, use --name")
}
return nil
}
func (opts *GetSubCommand) run(flags cli.Input) error {
func (opts *GetSubCommand) run(input cli.Input) error {
m := &metav1.ObjectMeta{
Name: opts.trigger,
Namespace: opts.namespace,
Name: input.String(flagkey.HtName),
Namespace: input.String(flagkey.NamespaceFunction),
}
ht, err := opts.client.HTTPTriggerGet(m)
if err != nil {
+13 -22
View File
@@ -22,51 +22,42 @@ import (
fv1 "github.com/fission/fission/pkg/apis/fission.io/v1"
"github.com/fission/fission/pkg/controller/client"
"github.com/fission/fission/pkg/fission-cli/cliwrapper/cli"
flagkey "github.com/fission/fission/pkg/fission-cli/flag/key"
"github.com/fission/fission/pkg/fission-cli/util"
)
type ListSubCommand struct {
client *client.Client
triggerNamespace string
filterFunctionName string
client *client.Client
}
func List(flags cli.Input) error {
c, err := util.GetServer(flags)
func List(input cli.Input) error {
c, err := util.GetServer(input)
if err != nil {
return err
}
opts := ListSubCommand{
client: c,
}
return opts.do(flags)
return opts.do(input)
}
func (opts *ListSubCommand) do(flags cli.Input) error {
err := opts.complete(flags)
if err != nil {
return err
}
return opts.run(flags)
func (opts *ListSubCommand) do(input cli.Input) error {
return opts.run(input)
}
func (opts *ListSubCommand) complete(flags cli.Input) error {
opts.triggerNamespace = flags.String("triggerNamespace")
opts.filterFunctionName = flags.String("function")
return nil
}
func (opts *ListSubCommand) run(flags cli.Input) error {
hts, err := opts.client.HTTPTriggerList(opts.triggerNamespace)
func (opts *ListSubCommand) run(input cli.Input) error {
hts, err := opts.client.HTTPTriggerList(input.String(flagkey.NamespaceTrigger))
if err != nil {
return errors.Wrap(err, "error listing HTTP triggers")
}
filterFunctionName := input.String(flagkey.HtFnName)
var triggers []fv1.HTTPTrigger
for _, ht := range hts {
// TODO: list canary http triggers as well.
if len(opts.filterFunctionName) == 0 ||
(len(opts.filterFunctionName) > 0 && opts.filterFunctionName == ht.Spec.FunctionReference.Name) {
if len(filterFunctionName) == 0 ||
(len(filterFunctionName) > 0 && filterFunctionName == ht.Spec.FunctionReference.Name) {
triggers = append(triggers, ht)
}
+2 -1
View File
@@ -24,7 +24,8 @@ import (
)
// GetIngressConfig returns an IngressConfig based on user inputs; return error if any.
func GetIngressConfig(annotations []string, rule string, tls string, fallbackRelativeURL string, oldIngressConfig *fv1.IngressConfig) (*fv1.IngressConfig, error) {
func GetIngressConfig(annotations []string, rule string, tls string,
fallbackRelativeURL string, oldIngressConfig *fv1.IngressConfig) (*fv1.IngressConfig, error) {
removeAnns, anns, err := getIngressAnnotations(annotations)
if err != nil {
+23 -26
View File
@@ -26,6 +26,7 @@ import (
"github.com/fission/fission/pkg/controller/client"
"github.com/fission/fission/pkg/fission-cli/cliwrapper/cli"
"github.com/fission/fission/pkg/fission-cli/console"
flagkey "github.com/fission/fission/pkg/fission-cli/flag/key"
"github.com/fission/fission/pkg/fission-cli/util"
)
@@ -34,31 +35,28 @@ type UpdateSubCommand struct {
trigger *fv1.HTTPTrigger
}
func Update(flags cli.Input) error {
c, err := util.GetServer(flags)
func Update(input cli.Input) error {
c, err := util.GetServer(input)
if err != nil {
return err
}
opts := UpdateSubCommand{
client: c,
}
return opts.do(flags)
return opts.do(input)
}
func (opts *UpdateSubCommand) do(flags cli.Input) error {
err := opts.complete(flags)
func (opts *UpdateSubCommand) do(input cli.Input) error {
err := opts.complete(input)
if err != nil {
return err
}
return opts.run(flags)
return opts.run(input)
}
func (opts *UpdateSubCommand) complete(flags cli.Input) error {
htName := flags.String("name")
if len(htName) == 0 {
return errors.New("need name of trigger, use --name")
}
triggerNamespace := flags.String("triggerNamespace")
func (opts *UpdateSubCommand) complete(input cli.Input) error {
htName := input.String(flagkey.HtName)
triggerNamespace := input.String(flagkey.NamespaceTrigger)
ht, err := opts.client.HTTPTriggerGet(&metav1.ObjectMeta{
Name: htName,
@@ -68,17 +66,17 @@ func (opts *UpdateSubCommand) complete(flags cli.Input) error {
return errors.Wrap(err, "error getting HTTP trigger")
}
if flags.IsSet("function") {
if input.IsSet(flagkey.HtFnName) {
// get the functions and their weights if specified
functionList := flags.StringSlice("function")
functionList := input.StringSlice(flagkey.HtFnName)
err := util.CheckFunctionExistence(opts.client, functionList, triggerNamespace)
if err != nil {
console.Warn(err.Error())
}
var functionWeightsList []int
if flags.IsSet("weight") {
functionWeightsList = flags.IntSlice("weight")
if input.IsSet(flagkey.HtFnWeight) {
functionWeightsList = input.IntSlice(flagkey.HtFnWeight)
}
// set function reference
@@ -90,21 +88,20 @@ func (opts *UpdateSubCommand) complete(flags cli.Input) error {
ht.Spec.FunctionReference = *functionRef
}
if flags.IsSet("createingress") {
ht.Spec.CreateIngress = flags.Bool("createingress")
if input.IsSet(flagkey.HtIngress) {
ht.Spec.CreateIngress = input.Bool(flagkey.HtIngress)
}
if flags.IsSet("host") {
ht.Spec.Host = flags.String("host")
console.Warn(fmt.Sprintf("--host is now marked as deprecated, see 'help' for details"))
if input.IsSet(flagkey.HtHost) {
ht.Spec.Host = input.String(flagkey.HtHost)
}
if flags.IsSet("ingressrule") || flags.IsSet("ingressannotation") || flags.IsSet("ingresstls") {
if input.IsSet(flagkey.HtIngressRule) || input.IsSet(flagkey.HtIngressAnnotation) || input.IsSet(flagkey.HtIngressTLS) {
ingress, err := GetIngressConfig(
flags.StringSlice("ingressannotation"), flags.String("ingressrule"),
flags.String("ingresstls"), ht.Spec.RelativeURL, &ht.Spec.IngressConfig)
input.StringSlice(flagkey.HtIngressAnnotation), input.String(flagkey.HtIngressRule),
input.String(flagkey.HtIngressTLS), ht.Spec.RelativeURL, &ht.Spec.IngressConfig)
if err != nil {
return errors.Wrap(err, "parse ingress configuration")
return errors.Wrap(err, "error parsing ingress configuration")
}
ht.Spec.IngressConfig = *ingress
}
@@ -114,7 +111,7 @@ func (opts *UpdateSubCommand) complete(flags cli.Input) error {
return nil
}
func (opts *UpdateSubCommand) run(flags cli.Input) error {
func (opts *UpdateSubCommand) run(input cli.Input) error {
_, err := opts.client.HTTPTriggerUpdate(opts.trigger)
if err != nil {
return errors.Wrap(err, "error updating the HTTP trigger")