Make CLI functions return error instead of fatal out (#1379)

Before this PR, CLI functions fatal out when encountering error
instead of returning it. Such behavior makes it hard to reuse 
the functions nor writing unit tests. This PR aims to make functions 
return errors instead of error out.
This commit is contained in:
Ta-Ching Chen
2019-11-05 16:50:13 +08:00
committed by GitHub
parent b0d27ee5d2
commit 93d4b88d84
72 changed files with 894 additions and 632 deletions
+7 -4
View File
@@ -25,7 +25,6 @@ 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"
"github.com/fission/fission/pkg/fission-cli/cmd"
"github.com/fission/fission/pkg/fission-cli/util"
"github.com/fission/fission/pkg/types"
)
@@ -36,8 +35,12 @@ type CreateSubCommand struct {
}
func Create(flags cli.Input) error {
c, err := util.GetServer(flags)
if err != nil {
return err
}
opts := CreateSubCommand{
client: cmd.GetServer(flags),
client: c,
}
return opts.do(flags)
}
@@ -68,7 +71,7 @@ func (opts *CreateSubCommand) complete(flags cli.Input) error {
}
// check that the trigger exists in the same namespace.
m, err := cmd.GetMetadata("httptrigger", "fnNamespace", flags)
m, err := util.GetMetadata("httptrigger", "fnNamespace", flags)
if err != nil {
return errors.Wrap(err, "error finding http trigger in given namespace")
}
@@ -101,7 +104,7 @@ func (opts *CreateSubCommand) complete(flags cli.Input) error {
return errors.Wrap(err, "error checking functions existence")
}
canaryMetadata, err := cmd.GetMetadata("name", "fnNamespace", flags)
canaryMetadata, err := util.GetMetadata("name", "fnNamespace", flags)
if err != nil {
return err
}
+7 -3
View File
@@ -23,7 +23,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/cmd"
"github.com/fission/fission/pkg/fission-cli/util"
)
type DeleteSubCommand struct {
@@ -31,14 +31,18 @@ type DeleteSubCommand struct {
}
func Delete(flags cli.Input) error {
c, err := util.GetServer(flags)
if err != nil {
return err
}
opts := DeleteSubCommand{
client: cmd.GetServer(flags),
client: c,
}
return opts.run(flags)
}
func (opts *DeleteSubCommand) run(flags cli.Input) error {
metadata, err := cmd.GetMetadata("name", "canaryNamespace", flags)
metadata, err := util.GetMetadata("name", "canaryNamespace", flags)
if err != nil {
return err
}
+7 -3
View File
@@ -25,7 +25,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/cmd"
"github.com/fission/fission/pkg/fission-cli/util"
)
type GetSubCommand struct {
@@ -33,14 +33,18 @@ type GetSubCommand struct {
}
func Get(flags cli.Input) error {
c, err := util.GetServer(flags)
if err != nil {
return err
}
opts := GetSubCommand{
client: cmd.GetServer(flags),
client: c,
}
return opts.run(flags)
}
func (opts *GetSubCommand) run(flags cli.Input) error {
m, err := cmd.GetMetadata("name", "canaryNamespace", flags)
m, err := util.GetMetadata("name", "canaryNamespace", flags)
if err != nil {
return err
}
+6 -2
View File
@@ -25,7 +25,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/cmd"
"github.com/fission/fission/pkg/fission-cli/util"
)
type ListSubCommand struct {
@@ -34,8 +34,12 @@ type ListSubCommand struct {
}
func List(flags cli.Input) error {
c, err := util.GetServer(flags)
if err != nil {
return err
}
opts := ListSubCommand{
client: cmd.GetServer(flags),
client: c,
}
return opts.do(flags)
}
+7 -3
View File
@@ -25,7 +25,7 @@ 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"
"github.com/fission/fission/pkg/fission-cli/cmd"
"github.com/fission/fission/pkg/fission-cli/util"
)
type UpdateSubCommand struct {
@@ -34,8 +34,12 @@ type UpdateSubCommand struct {
}
func Update(flags cli.Input) error {
c, err := util.GetServer(flags)
if err != nil {
return err
}
opts := UpdateSubCommand{
client: cmd.GetServer(flags),
client: c,
}
return opts.do(flags)
}
@@ -50,7 +54,7 @@ func (opts *UpdateSubCommand) do(flags cli.Input) error {
func (opts *UpdateSubCommand) complete(flags cli.Input) error {
// get the current config
m, err := cmd.GetMetadata("name", "canaryNamespace", flags)
m, err := util.GetMetadata("name", "canaryNamespace", flags)
if err != nil {
return err
}