Skip to content

Commit

Permalink
Refactored 'inspect' command (scaleway#80)
Browse files Browse the repository at this point in the history
  • Loading branch information
moul committed Aug 6, 2015
1 parent 7695f08 commit 3ca53cc
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 92 deletions.
104 changes: 12 additions & 92 deletions pkg/cli/inspect.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,9 @@
package cli

import (
"encoding/json"
"fmt"
"os"
"text/template"
"github.com/scaleway/scaleway-cli/vendor/github.com/Sirupsen/logrus"

log "github.com/scaleway/scaleway-cli/vendor/github.com/Sirupsen/logrus"
"github.com/scaleway/scaleway-cli/vendor/github.com/skratchdot/open-golang/open"

"github.com/scaleway/scaleway-cli/pkg/api"
"github.com/scaleway/scaleway-cli/pkg/commands"
)

var cmdInspect = &Command{
Expand Down Expand Up @@ -51,96 +45,22 @@ var inspectFormat string // -f, --format flag
var inspectBrowser bool // -b, --browser flag
var inspectHelp bool // -h, --help flag

func runInspect(cmd *Command, args []string) {
func runInspect(cmd *Command, rawArgs []string) {
if inspectHelp {
cmd.PrintUsage()
}
if len(args) < 1 {
if len(rawArgs) < 1 {
cmd.PrintShortUsage()
}

nbInspected := 0
ci := make(chan api.ScalewayResolvedIdentifier)
cj := make(chan api.InspectIdentifierResult)
go api.ResolveIdentifiers(cmd.API, args, ci)
go api.InspectIdentifiers(cmd.API, ci, cj)

if inspectBrowser {
// --browser will open links in the browser
for {
data, isOpen := <-cj
if !isOpen {
break
}

switch data.Type {
case api.IdentifierServer:
err := open.Start(fmt.Sprintf("https://cloud.scaleway.com/#/servers/%s", data.Object.(*api.ScalewayServer).Identifier))
if err != nil {
log.Fatalf("Cannot open browser: %v", err)
}
nbInspected++
case api.IdentifierImage:
err := open.Start(fmt.Sprintf("https://cloud.scaleway.com/#/images/%s", data.Object.(*api.ScalewayImage).Identifier))
if err != nil {
log.Fatalf("Cannot open browser: %v", err)
}
nbInspected++
case api.IdentifierVolume:
err := open.Start(fmt.Sprintf("https://cloud.scaleway.com/#/volumes/%s", data.Object.(*api.ScalewayVolume).Identifier))
if err != nil {
log.Fatalf("Cannot open browser: %v", err)
}
nbInspected++
case api.IdentifierSnapshot:
log.Errorf("Cannot use '--browser' option for snapshots")
case api.IdentifierBootscript:
log.Errorf("Cannot use '--browser' option for bootscripts")
}
}

} else {
// without --browser option, inspect will print object info to the terminal
res := "["
for {
data, isOpen := <-cj
if !isOpen {
break
}
if inspectFormat == "" {
dataB, err := json.MarshalIndent(data.Object, "", " ")
if err == nil {
if nbInspected != 0 {
res += ",\n"
}
res += string(dataB)
nbInspected++
}
} else {
tmpl, err := template.New("").Funcs(api.FuncMap).Parse(inspectFormat)
if err != nil {
log.Fatalf("Format parsing error: %v", err)
}

err = tmpl.Execute(os.Stdout, data.Object)
if err != nil {
log.Fatalf("Format execution error: %v", err)
}
fmt.Fprint(os.Stdout, "\n")
nbInspected++
}
}
res += "]"

if inspectFormat == "" {
if os.Getenv("SCW_SENSITIVE") != "1" {
res = cmd.API.HideAPICredentials(res)
}
fmt.Println(res)
}
args := commands.InspectArgs{
Format: inspectFormat,
Browser: inspectBrowser,
Identifiers: rawArgs,
}

if len(args) != nbInspected {
os.Exit(1)
ctx := cmd.GetContext(rawArgs)
err := commands.RunInspect(ctx, args)
if err != nil {
logrus.Fatalf("Cannot execute 'inspect': %v", err)
}
}
112 changes: 112 additions & 0 deletions pkg/commands/inspect.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
// Copyright (C) 2015 Scaleway. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE.md file.

package commands

import (
"encoding/json"
"fmt"
"os"
"text/template"

"github.com/scaleway/scaleway-cli/pkg/api"
"github.com/scaleway/scaleway-cli/vendor/github.com/Sirupsen/logrus"
"github.com/scaleway/scaleway-cli/vendor/github.com/skratchdot/open-golang/open"
)

// InspectArgs are flags for the `RunInspect` function
type InspectArgs struct {
Format string
Browser bool
Identifiers []string
}

// RunInspect is the handler for 'scw inspect'
func RunInspect(ctx CommandContext, args InspectArgs) error {
nbInspected := 0
ci := make(chan api.ScalewayResolvedIdentifier)
cj := make(chan api.InspectIdentifierResult)
go api.ResolveIdentifiers(ctx.API, args.Identifiers, ci)
go api.InspectIdentifiers(ctx.API, ci, cj)

if args.Browser {
// --browser will open links in the browser
for {
data, isOpen := <-cj
if !isOpen {
break
}

switch data.Type {
case api.IdentifierServer:
err := open.Start(fmt.Sprintf("https://cloud.scaleway.com/#/servers/%s", data.Object.(*api.ScalewayServer).Identifier))
if err != nil {
return fmt.Errorf("cannot open browser: %v", err)
}
nbInspected++
case api.IdentifierImage:
err := open.Start(fmt.Sprintf("https://cloud.scaleway.com/#/images/%s", data.Object.(*api.ScalewayImage).Identifier))
if err != nil {
return fmt.Errorf("cannot open browser: %v", err)
}
nbInspected++
case api.IdentifierVolume:
err := open.Start(fmt.Sprintf("https://cloud.scaleway.com/#/volumes/%s", data.Object.(*api.ScalewayVolume).Identifier))
if err != nil {
return fmt.Errorf("cannot open browser: %v", err)
}
nbInspected++
case api.IdentifierSnapshot:
logrus.Errorf("Cannot use '--browser' option for snapshots")
case api.IdentifierBootscript:
logrus.Errorf("Cannot use '--browser' option for bootscripts")
}
}

} else {
// without --browser option, inspect will print object info to the terminal
res := "["
for {
data, isOpen := <-cj
if !isOpen {
break
}
if args.Format == "" {
dataB, err := json.MarshalIndent(data.Object, "", " ")
if err == nil {
if nbInspected != 0 {
res += ",\n"
}
res += string(dataB)
nbInspected++
}
} else {
tmpl, err := template.New("").Funcs(api.FuncMap).Parse(args.Format)
if err != nil {
return fmt.Errorf("format parsing error: %v", err)
}

err = tmpl.Execute(ctx.Stdout, data.Object)
if err != nil {
return fmt.Errorf("format execution error: %v", err)
}
fmt.Fprint(ctx.Stdout, "\n")
nbInspected++
}
}
res += "]"

if args.Format == "" {
if os.Getenv("SCW_SENSITIVE") != "1" {
res = ctx.API.HideAPICredentials(res)
}
fmt.Fprintln(ctx.Stdout, res)
}
}

if len(args.Identifiers) != nbInspected {
os.Exit(1)
}
return nil
}

0 comments on commit 3ca53cc

Please sign in to comment.