Skip to content

Commit

Permalink
Merge pull request #9 from smartcontractkit/features/cli-stderr
Browse files Browse the repository at this point in the history
CLI returns an exit code of 1 on error
  • Loading branch information
se3000 authored Jan 10, 2018
2 parents 2d2705e + e48d9d2 commit a41f149
Showing 1 changed file with 22 additions and 30 deletions.
52 changes: 22 additions & 30 deletions commands/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package commands

import (
"encoding/json"
"fmt"
"errors"
"io"
"io/ioutil"
"net/http"
Expand Down Expand Up @@ -31,13 +31,6 @@ func (self *Client) PrettyPrintJSON(v interface{}) error {
return nil
}

func (self *Client) cliError(err error) error {
if err != nil {
self.Write([]byte(err.Error()))
}
return err
}

func (self *Client) RunNode(c *cli.Context) error {
cl := services.NewApplication(store.NewConfig())
services.Authenticate(cl.Store)
Expand All @@ -54,7 +47,7 @@ func (self *Client) RunNode(c *cli.Context) error {
func (self *Client) ShowJob(c *cli.Context) error {
cfg := store.NewConfig()
if !c.Args().Present() {
return self.cliError(fmt.Errorf("Must pass the job id to be shown"))
return self.cliError(errors.New("Must pass the job id to be shown"))
}
resp, err := utils.BasicAuthGet(
cfg.BasicAuthUsername,
Expand All @@ -66,25 +59,7 @@ func (self *Client) ShowJob(c *cli.Context) error {
}
defer resp.Body.Close()
var job models.Job
err = deserializeResponse(resp, &job)
if err != nil {
return self.cliError(err)
}
return self.cliError(self.PrettyPrintJSON(job))
}

func deserializeResponse(resp *http.Response, dst interface{}) error {
if resp.StatusCode >= 300 {
return fmt.Errorf(resp.Status)
}
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
if err = json.Unmarshal(b, &dst); err != nil {
return err
}
return nil
return self.deserializeResponse(resp, &job)
}

func (self *Client) GetJobs(c *cli.Context) error {
Expand All @@ -100,9 +75,26 @@ func (self *Client) GetJobs(c *cli.Context) error {
defer resp.Body.Close()

var jobs []models.Job
err = deserializeResponse(resp, &jobs)
return self.deserializeResponse(resp, &jobs)
}

func (self *Client) deserializeResponse(resp *http.Response, dst interface{}) error {
if resp.StatusCode >= 300 {
return self.cliError(errors.New(resp.Status))
}
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return self.cliError(err)
}
return self.cliError(self.PrettyPrintJSON(jobs))
if err = json.Unmarshal(b, &dst); err != nil {
return self.cliError(err)
}
return self.cliError(self.PrettyPrintJSON(dst))
}

func (self *Client) cliError(err error) error {
if err != nil {
return cli.NewExitError(err.Error(), 1)
}
return nil
}

0 comments on commit a41f149

Please sign in to comment.