Migrate from urfave/cli to cobra (#1385)

This commit is contained in:
Ta-Ching Chen
2019-11-08 21:22:00 +08:00
committed by GitHub
parent 4b456db517
commit 1888cd2ac7
66 changed files with 2901 additions and 1130 deletions
@@ -0,0 +1,90 @@
/*
Copyright 2019 The Fission Authors.
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 canaryconfig
import (
"github.com/spf13/cobra"
wrapper "github.com/fission/fission/pkg/fission-cli/cliwrapper/driver/cobra"
"github.com/fission/fission/pkg/fission-cli/flag"
)
func Commands() *cobra.Command {
createCmd := &cobra.Command{
Use: "create",
Short: "Create a canary config",
RunE: wrapper.Wrapper(Create),
}
wrapper.SetFlags(createCmd, flag.FlagSet{
Required: []flag.Flag{flag.CanaryNameFlag, flag.CanaryTriggerNameFlag, flag.CanaryNewFuncFlag, flag.CanaryOldFuncFlag},
Optional: []flag.Flag{flag.NamespaceFunctionFlag, flag.CanaryWeightIncrementFlag, flag.CanaryIncrementIntervalFlag, flag.CanaryFailureThresholdFlag},
})
getCmd := &cobra.Command{
Use: "get",
Aliases: []string{},
Short: "View parameters in a canary config",
RunE: wrapper.Wrapper(Get),
}
wrapper.SetFlags(getCmd, flag.FlagSet{
Required: []flag.Flag{flag.CanaryNameFlag},
Optional: []flag.Flag{flag.NamespaceCanaryFlag},
})
updateCmd := &cobra.Command{
Use: "update",
Aliases: []string{},
Short: "Update parameters of a canary config",
RunE: wrapper.Wrapper(Update),
}
wrapper.SetFlags(updateCmd, flag.FlagSet{
Required: []flag.Flag{flag.CanaryNameFlag},
Optional: []flag.Flag{flag.NamespaceCanaryFlag, flag.CanaryWeightIncrementFlag, flag.CanaryIncrementIntervalFlag, flag.CanaryFailureThresholdFlag},
})
deleteCmd := &cobra.Command{
Use: "delete",
Aliases: []string{},
Short: "Delete a canary config",
RunE: wrapper.Wrapper(Delete),
}
wrapper.SetFlags(deleteCmd, flag.FlagSet{
Required: []flag.Flag{flag.CanaryNameFlag},
Optional: []flag.Flag{flag.NamespaceCanaryFlag},
})
listCmd := &cobra.Command{
Use: "list",
Aliases: []string{},
Short: "List canary configs",
Long: "List all canary configs in a namespace if specified, else, list canary configs across all namespaces",
RunE: wrapper.Wrapper(List),
}
wrapper.SetFlags(listCmd, flag.FlagSet{
Optional: []flag.Flag{flag.NamespaceCanaryFlag},
})
command := &cobra.Command{
Use: "canary",
Aliases: []string{"canary-config"},
Short: "Create, Update and manage canary configs",
}
command.AddCommand(createCmd, getCmd, updateCmd, deleteCmd, listCmd)
return command
}
+21 -22
View File
@@ -21,10 +21,12 @@ import (
"time"
"github.com/pkg/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
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"
"github.com/fission/fission/pkg/types"
)
@@ -56,13 +58,14 @@ func (opts *CreateSubCommand) do(flags cli.Input) error {
func (opts *CreateSubCommand) complete(flags cli.Input) error {
// canary configs can be created for functions in the same namespace
trigger := flags.String("httptrigger")
newFunc := flags.String("newfunction")
oldFunc := flags.String("oldfunction")
ns := flags.String("fnNamespace")
incrementStep := flags.Int("increment-step")
failureThreshold := flags.Int("failure-threshold")
incrementInterval := flags.String("increment-interval")
name := flags.String(flagkey.CanaryName)
ht := flags.String(flagkey.CanaryHTTPTriggerName)
newFunc := flags.String(flagkey.CanaryNewFunc)
oldFunc := flags.String(flagkey.CanaryOldFunc)
fnNs := flags.String(flagkey.NamespaceFunction)
incrementStep := flags.Int(flagkey.CanaryWeightIncrement)
failureThreshold := flags.Int(flagkey.CanaryFailureThreshold)
incrementInterval := flags.String(flagkey.CanaryIncrementInterval)
// check for time parsing
_, err := time.ParseDuration(incrementInterval)
@@ -71,14 +74,12 @@ func (opts *CreateSubCommand) complete(flags cli.Input) error {
}
// check that the trigger exists in the same namespace.
m, err := util.GetMetadata("httptrigger", "fnNamespace", flags)
htTrigger, err := opts.client.HTTPTriggerGet(&metav1.ObjectMeta{
Name: ht,
Namespace: fnNs,
})
if err != nil {
return errors.Wrap(err, "error finding http trigger in given namespace")
}
htTrigger, err := opts.client.HTTPTriggerGet(m)
if err != nil {
return errors.Wrap(err, "error finding trigger referenced in the canary config")
return errors.Wrap(err, "error finding http trigger referenced in the canary config")
}
// check that the trigger has function reference type function weights
@@ -99,21 +100,19 @@ func (opts *CreateSubCommand) complete(flags cli.Input) error {
// check that the functions exist in the same namespace
fnList := []string{newFunc, oldFunc}
err = util.CheckFunctionExistence(opts.client, fnList, ns)
err = util.CheckFunctionExistence(opts.client, fnList, fnNs)
if err != nil {
return errors.Wrap(err, "error checking functions existence")
}
canaryMetadata, err := util.GetMetadata("name", "fnNamespace", flags)
if err != nil {
return err
}
// finally create canaryCfg in the same namespace as the functions referenced
opts.canary = &fv1.CanaryConfig{
Metadata: *canaryMetadata,
Metadata: metav1.ObjectMeta{
Name: name,
Namespace: fnNs,
},
Spec: fv1.CanaryConfigSpec{
Trigger: trigger,
Trigger: ht,
NewFunction: newFunc,
OldFunction: oldFunc,
WeightIncrement: incrementStep,
+12 -9
View File
@@ -20,9 +20,11 @@ import (
"fmt"
"github.com/pkg/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/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"
)
@@ -30,27 +32,28 @@ type DeleteSubCommand struct {
client *client.Client
}
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.run(flags)
return opts.run(input)
}
func (opts *DeleteSubCommand) run(flags cli.Input) error {
metadata, err := util.GetMetadata("name", "canaryNamespace", flags)
if err != nil {
return err
func (opts *DeleteSubCommand) run(input cli.Input) error {
m := &metav1.ObjectMeta{
Name: input.String(flagkey.CanaryName),
Namespace: input.String(flagkey.NamespaceCanary),
}
err = opts.client.CanaryConfigDelete(metadata)
err := opts.client.CanaryConfigDelete(m)
if err != nil {
return errors.Wrap(err, "error deleting canary config")
}
fmt.Printf("canaryconfig '%v.%v' deleted\n", metadata.Name, metadata.Namespace)
fmt.Printf("canaryconfig '%v.%v' deleted\n", m.Name, m.Namespace)
return nil
}
+10 -10
View File
@@ -22,9 +22,11 @@ import (
"text/tabwriter"
"github.com/pkg/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/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"
)
@@ -32,24 +34,22 @@ type GetSubCommand struct {
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.run(flags)
return opts.run(input)
}
func (opts *GetSubCommand) run(flags cli.Input) error {
m, err := util.GetMetadata("name", "canaryNamespace", flags)
if err != nil {
return err
}
canaryCfg, err := opts.client.CanaryConfigGet(m)
func (opts *GetSubCommand) run(input cli.Input) error {
canaryCfg, err := opts.client.CanaryConfigGet(&metav1.ObjectMeta{
Name: input.String(flagkey.CanaryName),
Namespace: input.String(flagkey.NamespaceCanary),
})
if err != nil {
return errors.Wrap(err, "error getting canary config")
}
+8 -7
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"
)
@@ -33,27 +34,27 @@ type ListSubCommand struct {
namespace string
}
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)
func (opts *ListSubCommand) do(input cli.Input) error {
err := opts.complete(input)
if err != nil {
return err
}
return opts.run(flags)
return opts.run(input)
}
func (opts *ListSubCommand) complete(flags cli.Input) error {
opts.namespace = flags.String("canaryNamespace")
opts.namespace = flags.String(flagkey.NamespaceCanary)
return nil
}
+20 -18
View File
@@ -21,10 +21,12 @@ import (
"time"
"github.com/pkg/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
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"
)
@@ -33,43 +35,43 @@ type UpdateSubCommand struct {
canary *fv1.CanaryConfig
}
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 {
func (opts *UpdateSubCommand) complete(input cli.Input) error {
// get the current config
m, err := util.GetMetadata("name", "canaryNamespace", flags)
if err != nil {
return err
}
incrementStep := flags.Int("increment-step")
failureThreshold := flags.Int("failure-threshold")
incrementInterval := flags.String("increment-interval")
name := input.String(flagkey.CanaryName)
ns := input.String(flagkey.NamespaceCanary)
incrementStep := input.Int(flagkey.CanaryWeightIncrement)
failureThreshold := input.Int(flagkey.CanaryFailureThreshold)
incrementInterval := input.String(flagkey.CanaryIncrementInterval)
// check for time parsing
_, err = time.ParseDuration(incrementInterval)
_, err := time.ParseDuration(incrementInterval)
if err != nil {
return errors.Wrap(err, "error parsing time duration")
}
canaryCfg, err := opts.client.CanaryConfigGet(m)
canaryCfg, err := opts.client.CanaryConfigGet(&metav1.ObjectMeta{
Name: name,
Namespace: ns,
})
if err != nil {
return errors.Wrap(err, "error getting canary config")
}
@@ -97,7 +99,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.CanaryConfigUpdate(opts.canary)
if err != nil {
return errors.Wrap(err, "error updating canary config")