diff --git a/commands/command_registry.go b/commands/command_registry.go index 6cb6c27..a999b83 100644 --- a/commands/command_registry.go +++ b/commands/command_registry.go @@ -35,7 +35,7 @@ func init() { type CckCommand interface { fill(context *context.Context, service string, env string) - execute() + execute() error } func fill(cck CckCommand, config *utils.Config, service string, env string) error { @@ -69,6 +69,6 @@ func fill(cck CckCommand, config *utils.Config, service string, env string) erro return nil } -func execute(cck CckCommand) { - cck.execute() +func execute(cck CckCommand) error { + return cck.execute() } diff --git a/commands/contact_key.go b/commands/contact_key.go index d49b079..04f948e 100644 --- a/commands/contact_key.go +++ b/commands/contact_key.go @@ -14,13 +14,11 @@ func Execute() { cfg, err := utils.LoadConfig() if err != nil { log.Fatalln(fmt.Sprintf("Failed load config: %q", err)) - return } services, err := cfg.DiscoverServices() if err != nil { log.Fatalln(fmt.Sprintf("Failed to find services: %q", err)) - return } rootCmd := &cobra.Command{ diff --git a/commands/deploy.go b/commands/deploy.go index 5c888d7..549adc7 100644 --- a/commands/deploy.go +++ b/commands/deploy.go @@ -1,6 +1,7 @@ package commands import ( + "errors" "fmt" "io" "os" @@ -40,7 +41,7 @@ type Deploy struct { Writer io.Writer } -func (d *Deploy) execute() { +func (d *Deploy) execute() error { timer := prometheus.NewTimer(prometheus.ObserverFunc( deployDuration.With(prometheus.Labels{"env": d.Env, "project": d.Service}).Set)) d.Context.Metrics.Add(deployDuration) @@ -62,8 +63,7 @@ func (d *Deploy) execute() { } if err := utils.CheckIfIsLaunchedInAScreen(); err != nil && d.Context.ScreenMandatory == true { - log.Errorln(fmt.Sprintf("Screen error raised: %q", err)) - return + return errors.New(fmt.Sprintf("Screen error raised: %q", err)) } // The lock system is not mandatory @@ -71,17 +71,15 @@ func (d *Deploy) execute() { log.Println(fmt.Sprintf("Trying to lock the lock command for service %q and env %q", d.Service, d.Env)) canLock, err := d.Context.LockSystem.Lock(d.Env, d.Service) if err != nil { - log.Fatalln(fmt.Sprintf("Failed to lock, error raised: %q", err)) - return + return errors.New(fmt.Sprintf("Failed to lock, error raised: %q", err)) } if canLock == false { - log.Fatalln("Another command is currently running") - return + return errors.New("Another command is currently running") } defer func(d *Deploy) { - d.Context.LockSystem.Unlock(d.Env, d.Service) + err = d.Context.LockSystem.Unlock(d.Env, d.Service) if err != nil { log.Errorln(fmt.Sprintf("Failed to unlock, error raised: %q", err)) } @@ -91,26 +89,22 @@ func (d *Deploy) execute() { // If the branch is null it will use the default one. sha1ToDeploy, err := d.Context.Vcs.RetrieveSha1ForProject(branch) if err != nil { - log.Fatalln(fmt.Sprintf("Failed to retrieve source changes for %q : %q", d.Service, err)) - return + return errors.New(fmt.Sprintf("Failed to retrieve source changes for %q : %q", d.Service, err)) } podVersion, err := d.Context.Binaries.RetrievePodVersion(sha1ToDeploy) if err != nil { - log.Fatalln(fmt.Sprintf("Failed to retrieve pod version: %q", err)) - return + return errors.New(fmt.Sprintf("Failed to retrieve pod version: %q", err)) } deployedVersions, err := d.Context.Deployer.ListVcsVersions(d.Env) if err != nil { - log.Fatalln(fmt.Sprintf("Failed to retrieve DEPLOYED version from service(%q) in env %q: %q", d.Service, d.Env, err)) - return + return errors.New(fmt.Sprintf("Failed to retrieve DEPLOYED version from service(%q) in env %q: %q", d.Service, d.Env, err)) } if podVersion == "" { - log.Fatalln(fmt.Sprintf("We have not found the pod version with the the sha1 %q \n"+ + return errors.New(fmt.Sprintf("We have not found the pod version with the the sha1 %q \n"+ "The pod has not been created.", sha1ToDeploy)) - return } needToDeploy := force @@ -125,16 +119,14 @@ func (d *Deploy) execute() { } if needToDeploy == false { - log.Fatalln(fmt.Sprintf("Version %q is already deployed.", sha1ToDeploy)) - return + return errors.New(fmt.Sprintf("Version %q is already deployed.", sha1ToDeploy)) } log.Println(fmt.Sprintf("Going to deploy pod version %q \n", podVersion)) for _, hook := range d.Context.Hooks { err = hook.PreDeployment(userName, d.Env, d.Service, podVersion) if hook.StopOnError() == true && err != nil { - log.Fatalln(fmt.Sprintf("Predeployment failed: %q", err)) - return + return errors.New(fmt.Sprintf("Predeployment failed: %q", err)) } else if err != nil { log.Debugln(fmt.Sprintf("Predeployment failed: %q", err)) } @@ -150,8 +142,7 @@ func (d *Deploy) execute() { }() err = d.Context.Deployer.Deploy(d.Env, podVersion, stateStream) if err != nil { - log.Fatalf("Deployment failed: %q", err) - return + return errors.New(fmt.Sprintf("Deployment failed: %q", err)) } for _, hook := range d.Context.Hooks { @@ -168,6 +159,7 @@ func (d *Deploy) execute() { } log.Println(d.Writer, "Deployment has successfully ended") + return nil } func (d *Deploy) fill(context *context.Context, service string, env string) { diff --git a/commands/diff.go b/commands/diff.go index 670cbb8..2c6ddaf 100644 --- a/commands/diff.go +++ b/commands/diff.go @@ -4,6 +4,8 @@ import ( "fmt" "os" + "errors" + "github.com/olekukonko/tablewriter" "github.com/remyLemeunier/contactkey/context" log "github.com/sirupsen/logrus" @@ -26,27 +28,23 @@ type Diff struct { TableWriter *tablewriter.Table } -func (d Diff) execute() { +func (d Diff) execute() error { // If the branch is null it will use the default one. sha1, err := d.Context.Vcs.RetrieveSha1ForProject(branch) if err != nil { - log.Fatal(fmt.Sprintf("Failed to retrieve sha1: %q \n", err)) - return + return errors.New(fmt.Sprintf("Failed to retrieve sha1: %q \n", err)) } if sha1 == "" { - log.Fatal(fmt.Sprintf("No sha1 found for service %q \n", d.Service)) - return + return errors.New(fmt.Sprintf("No sha1 found for service %q \n", d.Service)) } versions, err := d.Context.Deployer.ListVcsVersions(d.Env) if err != nil { - log.Fatal(fmt.Sprintf("Failed to list versions with error %q \n", err)) - return + return errors.New(fmt.Sprintf("Failed to list versions with error %q \n", err)) } if len(versions) == 0 { - log.Fatal(fmt.Sprintf("No service (%q) versions found for the Env: %q \n", d.Service, d.Env)) - return + return errors.New(fmt.Sprintf("No service (%q) versions found for the Env: %q \n", d.Service, d.Env)) } // Retrieve only unique versions @@ -62,8 +60,7 @@ func (d Diff) execute() { for _, uniqueVersion := range uniqueVersions { changes, err := d.Context.Vcs.Diff(uniqueVersion, sha1) if err != nil { - log.Fatal(fmt.Sprintf("Failed to retrieve sha1: %q \n", err)) - return + return errors.New(fmt.Sprintf("Failed to retrieve sha1: %q \n", err)) } log.Println(fmt.Sprintf("Diff between %q(deployed) and %q(branch) \n", uniqueVersion, sha1)) @@ -73,6 +70,7 @@ func (d Diff) execute() { } d.TableWriter.Render() } + return nil } func (d *Diff) fill(context *context.Context, service string, env string) { diff --git a/commands/env_service.go b/commands/env_service.go index 775c780..98fed18 100644 --- a/commands/env_service.go +++ b/commands/env_service.go @@ -30,9 +30,8 @@ func addServiceNameToCommand(cmd *cobra.Command, cfg *utils.Config, services []s if err != nil { return err } - execute(cckCommand) - return nil + return execute(cckCommand) }, } cmd.AddCommand(serviceCmd) diff --git a/commands/list.go b/commands/list.go index 2bb2b4b..4f834a7 100644 --- a/commands/list.go +++ b/commands/list.go @@ -1,13 +1,13 @@ package commands import ( + "errors" "fmt" "io" "os" "github.com/olekukonko/tablewriter" "github.com/remyLemeunier/contactkey/context" - log "github.com/sirupsen/logrus" "github.com/spf13/cobra" ) @@ -24,11 +24,10 @@ type List struct { Writer io.Writer } -func (l List) execute() { +func (l List) execute() error { instances, err := l.Context.Deployer.ListInstances(l.Env) if err != nil { - log.Fatalln(fmt.Sprintf("Failed to list versions : %q", err)) - return + return errors.New(fmt.Sprintf("Failed to list versions : %q", err)) } l.TableWriter.SetHeader([]string{"instance", "state", "version"}) @@ -36,7 +35,7 @@ func (l List) execute() { l.TableWriter.Append([]string{instance.Name, instance.State, instance.Version}) } l.TableWriter.Render() - + return nil } func (l *List) fill(context *context.Context, service string, env string) { diff --git a/commands/rollback.go b/commands/rollback.go index d926dfa..f9337d4 100644 --- a/commands/rollback.go +++ b/commands/rollback.go @@ -16,7 +16,8 @@ type Rollback struct { Context *context.Context } -func (r *Rollback) execute() { +func (r *Rollback) execute() error { + return nil } func (r *Rollback) fill(context *context.Context, service string, env string) {