From 93d2f45c211c89b8210386b710a1ad6880d8680a Mon Sep 17 00:00:00 2001 From: Quentin Perez Date: Mon, 7 Sep 2015 08:48:32 +0200 Subject: [PATCH] little review --- pkg/api/api.go | 16 +++++++++++++++- pkg/commands/commit.go | 2 +- pkg/commands/events.go | 2 +- pkg/commands/info.go | 2 +- pkg/commands/login.go | 3 ++- pkg/commands/top.go | 4 +++- 6 files changed, 23 insertions(+), 6 deletions(-) diff --git a/pkg/api/api.go b/pkg/api/api.go index 816c923920..b2eea2f9ec 100644 --- a/pkg/api/api.go +++ b/pkg/api/api.go @@ -637,12 +637,14 @@ func (s *ScalewayAPI) GetResponse(resource string) (*http.Response, error) { req.Header.Set("Content-Type", "application/json") curl, err := http2curl.GetCurlCommand(req) + if err != nil { + return nil, err + } if os.Getenv("SCW_SENSITIVE") != "1" { log.Debug(s.HideAPICredentials(curl.String())) } else { log.Debug(curl.String()) } - return s.client.Do(req) } @@ -663,6 +665,9 @@ func (s *ScalewayAPI) PostResponse(resource string, data interface{}) (*http.Res req.Header.Set("Content-Type", "application/json") curl, err := http2curl.GetCurlCommand(req) + if err != nil { + return nil, err + } if os.Getenv("SCW_SENSITIVE") != "1" { log.Debug(s.HideAPICredentials(curl.String())) } else { @@ -689,6 +694,9 @@ func (s *ScalewayAPI) PatchResponse(resource string, data interface{}) (*http.Re req.Header.Set("Content-Type", "application/json") curl, err := http2curl.GetCurlCommand(req) + if err != nil { + return nil, err + } if os.Getenv("SCW_SENSITIVE") != "1" { log.Debug(s.HideAPICredentials(curl.String())) } else { @@ -715,6 +723,9 @@ func (s *ScalewayAPI) PutResponse(resource string, data interface{}) (*http.Resp req.Header.Set("Content-Type", "application/json") curl, err := http2curl.GetCurlCommand(req) + if err != nil { + return nil, err + } if os.Getenv("SCW_SENSITIVE") != "1" { log.Debug(s.HideAPICredentials(curl.String())) } else { @@ -736,6 +747,9 @@ func (s *ScalewayAPI) DeleteResponse(resource string) (*http.Response, error) { req.Header.Set("Content-Type", "application/json") curl, err := http2curl.GetCurlCommand(req) + if err != nil { + return nil, err + } if os.Getenv("SCW_SENSITIVE") != "1" { log.Debug(s.HideAPICredentials(curl.String())) } else { diff --git a/pkg/commands/commit.go b/pkg/commands/commit.go index 6db8ff1720..9196babe38 100644 --- a/pkg/commands/commit.go +++ b/pkg/commands/commit.go @@ -33,6 +33,6 @@ func RunCommit(ctx CommandContext, args CommitArgs) error { if err != nil { return fmt.Errorf("Cannot create snapshot: %v", err) } - fmt.Println(snapshot) + fmt.Fprintln(ctx.Stdout, snapshot) return nil } diff --git a/pkg/commands/events.go b/pkg/commands/events.go index a97ded273e..d5fc1c15ab 100644 --- a/pkg/commands/events.go +++ b/pkg/commands/events.go @@ -36,7 +36,7 @@ func RunEvents(ctx CommandContext, args EventsArgs) error { terminatedAt = units.HumanDuration(time.Now().UTC().Sub(terminatedAtTime)) } - fmt.Printf("%s %s: %s (%s %d) %s\n", startedAt, event.HrefFrom, event.Description, event.Status, event.Progress, terminatedAt) + fmt.Fprintf(ctx.Stdout, "%s %s: %s (%s %d) %s\n", startedAt, event.HrefFrom, event.Description, event.Status, event.Progress, terminatedAt) } return nil } diff --git a/pkg/commands/info.go b/pkg/commands/info.go index 501cf2ef97..ee19a39132 100644 --- a/pkg/commands/info.go +++ b/pkg/commands/info.go @@ -55,7 +55,7 @@ func RunInfo(ctx CommandContext, args InfoArgs) error { if err != nil { return err } else { - fmt.Fprintf(ctx.Stdout, " [%d] %s", id, fingerprint) + fmt.Fprintf(ctx.Stdout, " [%d] %s\n", id, fingerprint) } } } diff --git a/pkg/commands/login.go b/pkg/commands/login.go index 941eccf6f0..3ed26e9812 100644 --- a/pkg/commands/login.go +++ b/pkg/commands/login.go @@ -92,13 +92,14 @@ func getToken(connect api.ScalewayConnect) (string, error) { if err != nil { return "", fmt.Errorf("unable to connect %v", err) } + defer resp.Body.Close() + // Succeed POST code if resp.StatusCode != 201 { return "", fmt.Errorf("[%d] maybe your email or your password is not valid", resp.StatusCode) } var data api.ScalewayConnectResponse - defer resp.Body.Close() decoder := json.NewDecoder(resp.Body) err = decoder.Decode(&data) if err != nil { diff --git a/pkg/commands/top.go b/pkg/commands/top.go index 1f06de1e6e..be5d120bbb 100644 --- a/pkg/commands/top.go +++ b/pkg/commands/top.go @@ -45,6 +45,8 @@ func RunTop(ctx CommandContext, args TopArgs) error { sshCommand := utils.NewSSHExecCmd(server.PublicAddress.IP, server.PrivateIP, true, []string{command}, gateway) logrus.Debugf("Executing: %s", sshCommand) out, err := exec.Command("ssh", sshCommand.Slice()[1:]...).CombinedOutput() - fmt.Printf("%s", out) + if err != nil { + fmt.Fprintf(ctx.Stdout, "%s", out) + } return err }