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
+14 -5
View File
@@ -17,9 +17,10 @@ limitations under the License.
package spec
import (
"github.com/pkg/errors"
"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"
)
@@ -29,8 +30,12 @@ type DestroySubCommand struct {
// Destroy destroys everything in the spec.
func Destroy(flags cli.Input) error {
c, err := util.GetServer(flags)
if err != nil {
return err
}
opts := &DestroySubCommand{
client: cmd.GetServer(flags),
client: c,
}
return opts.do(flags)
}
@@ -41,11 +46,13 @@ func (opts *DestroySubCommand) do(flags cli.Input) error {
func (opts *DestroySubCommand) run(flags cli.Input) error {
// get specdir
specDir := cmd.GetSpecDir(flags)
specDir := util.GetSpecDir(flags)
// read everything
fr, err := ReadSpecs(specDir)
util.CheckErr(err, "read specs")
if err != nil {
return errors.Wrap(err, "error reading specs")
}
// set desired state to nothing, but keep the UID so "apply" can find it
emptyFr := FissionResources{}
@@ -53,7 +60,9 @@ func (opts *DestroySubCommand) run(flags cli.Input) error {
// "apply" the empty state
_, _, err = applyResources(opts.client, specDir, &emptyFr, true)
util.CheckErr(err, "delete resources")
if err != nil {
return errors.Wrap(err, "error deleting resources")
}
return nil
}