Skip to content

Commit 3ca53cc

Browse files
committed
Refactored 'inspect' command (scaleway#80)
1 parent 7695f08 commit 3ca53cc

File tree

2 files changed

+124
-92
lines changed

2 files changed

+124
-92
lines changed

pkg/cli/inspect.go

+12-92
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,9 @@
55
package cli
66

77
import (
8-
"encoding/json"
9-
"fmt"
10-
"os"
11-
"text/template"
8+
"github.com/scaleway/scaleway-cli/vendor/github.com/Sirupsen/logrus"
129

13-
log "github.com/scaleway/scaleway-cli/vendor/github.com/Sirupsen/logrus"
14-
"github.com/scaleway/scaleway-cli/vendor/github.com/skratchdot/open-golang/open"
15-
16-
"github.com/scaleway/scaleway-cli/pkg/api"
10+
"github.com/scaleway/scaleway-cli/pkg/commands"
1711
)
1812

1913
var cmdInspect = &Command{
@@ -51,96 +45,22 @@ var inspectFormat string // -f, --format flag
5145
var inspectBrowser bool // -b, --browser flag
5246
var inspectHelp bool // -h, --help flag
5347

54-
func runInspect(cmd *Command, args []string) {
48+
func runInspect(cmd *Command, rawArgs []string) {
5549
if inspectHelp {
5650
cmd.PrintUsage()
5751
}
58-
if len(args) < 1 {
52+
if len(rawArgs) < 1 {
5953
cmd.PrintShortUsage()
6054
}
6155

62-
nbInspected := 0
63-
ci := make(chan api.ScalewayResolvedIdentifier)
64-
cj := make(chan api.InspectIdentifierResult)
65-
go api.ResolveIdentifiers(cmd.API, args, ci)
66-
go api.InspectIdentifiers(cmd.API, ci, cj)
67-
68-
if inspectBrowser {
69-
// --browser will open links in the browser
70-
for {
71-
data, isOpen := <-cj
72-
if !isOpen {
73-
break
74-
}
75-
76-
switch data.Type {
77-
case api.IdentifierServer:
78-
err := open.Start(fmt.Sprintf("https://cloud.scaleway.com/#/servers/%s", data.Object.(*api.ScalewayServer).Identifier))
79-
if err != nil {
80-
log.Fatalf("Cannot open browser: %v", err)
81-
}
82-
nbInspected++
83-
case api.IdentifierImage:
84-
err := open.Start(fmt.Sprintf("https://cloud.scaleway.com/#/images/%s", data.Object.(*api.ScalewayImage).Identifier))
85-
if err != nil {
86-
log.Fatalf("Cannot open browser: %v", err)
87-
}
88-
nbInspected++
89-
case api.IdentifierVolume:
90-
err := open.Start(fmt.Sprintf("https://cloud.scaleway.com/#/volumes/%s", data.Object.(*api.ScalewayVolume).Identifier))
91-
if err != nil {
92-
log.Fatalf("Cannot open browser: %v", err)
93-
}
94-
nbInspected++
95-
case api.IdentifierSnapshot:
96-
log.Errorf("Cannot use '--browser' option for snapshots")
97-
case api.IdentifierBootscript:
98-
log.Errorf("Cannot use '--browser' option for bootscripts")
99-
}
100-
}
101-
102-
} else {
103-
// without --browser option, inspect will print object info to the terminal
104-
res := "["
105-
for {
106-
data, isOpen := <-cj
107-
if !isOpen {
108-
break
109-
}
110-
if inspectFormat == "" {
111-
dataB, err := json.MarshalIndent(data.Object, "", " ")
112-
if err == nil {
113-
if nbInspected != 0 {
114-
res += ",\n"
115-
}
116-
res += string(dataB)
117-
nbInspected++
118-
}
119-
} else {
120-
tmpl, err := template.New("").Funcs(api.FuncMap).Parse(inspectFormat)
121-
if err != nil {
122-
log.Fatalf("Format parsing error: %v", err)
123-
}
124-
125-
err = tmpl.Execute(os.Stdout, data.Object)
126-
if err != nil {
127-
log.Fatalf("Format execution error: %v", err)
128-
}
129-
fmt.Fprint(os.Stdout, "\n")
130-
nbInspected++
131-
}
132-
}
133-
res += "]"
134-
135-
if inspectFormat == "" {
136-
if os.Getenv("SCW_SENSITIVE") != "1" {
137-
res = cmd.API.HideAPICredentials(res)
138-
}
139-
fmt.Println(res)
140-
}
56+
args := commands.InspectArgs{
57+
Format: inspectFormat,
58+
Browser: inspectBrowser,
59+
Identifiers: rawArgs,
14160
}
142-
143-
if len(args) != nbInspected {
144-
os.Exit(1)
61+
ctx := cmd.GetContext(rawArgs)
62+
err := commands.RunInspect(ctx, args)
63+
if err != nil {
64+
logrus.Fatalf("Cannot execute 'inspect': %v", err)
14565
}
14666
}

pkg/commands/inspect.go

+112
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
// Copyright (C) 2015 Scaleway. All rights reserved.
2+
// Use of this source code is governed by a MIT-style
3+
// license that can be found in the LICENSE.md file.
4+
5+
package commands
6+
7+
import (
8+
"encoding/json"
9+
"fmt"
10+
"os"
11+
"text/template"
12+
13+
"github.com/scaleway/scaleway-cli/pkg/api"
14+
"github.com/scaleway/scaleway-cli/vendor/github.com/Sirupsen/logrus"
15+
"github.com/scaleway/scaleway-cli/vendor/github.com/skratchdot/open-golang/open"
16+
)
17+
18+
// InspectArgs are flags for the `RunInspect` function
19+
type InspectArgs struct {
20+
Format string
21+
Browser bool
22+
Identifiers []string
23+
}
24+
25+
// RunInspect is the handler for 'scw inspect'
26+
func RunInspect(ctx CommandContext, args InspectArgs) error {
27+
nbInspected := 0
28+
ci := make(chan api.ScalewayResolvedIdentifier)
29+
cj := make(chan api.InspectIdentifierResult)
30+
go api.ResolveIdentifiers(ctx.API, args.Identifiers, ci)
31+
go api.InspectIdentifiers(ctx.API, ci, cj)
32+
33+
if args.Browser {
34+
// --browser will open links in the browser
35+
for {
36+
data, isOpen := <-cj
37+
if !isOpen {
38+
break
39+
}
40+
41+
switch data.Type {
42+
case api.IdentifierServer:
43+
err := open.Start(fmt.Sprintf("https://cloud.scaleway.com/#/servers/%s", data.Object.(*api.ScalewayServer).Identifier))
44+
if err != nil {
45+
return fmt.Errorf("cannot open browser: %v", err)
46+
}
47+
nbInspected++
48+
case api.IdentifierImage:
49+
err := open.Start(fmt.Sprintf("https://cloud.scaleway.com/#/images/%s", data.Object.(*api.ScalewayImage).Identifier))
50+
if err != nil {
51+
return fmt.Errorf("cannot open browser: %v", err)
52+
}
53+
nbInspected++
54+
case api.IdentifierVolume:
55+
err := open.Start(fmt.Sprintf("https://cloud.scaleway.com/#/volumes/%s", data.Object.(*api.ScalewayVolume).Identifier))
56+
if err != nil {
57+
return fmt.Errorf("cannot open browser: %v", err)
58+
}
59+
nbInspected++
60+
case api.IdentifierSnapshot:
61+
logrus.Errorf("Cannot use '--browser' option for snapshots")
62+
case api.IdentifierBootscript:
63+
logrus.Errorf("Cannot use '--browser' option for bootscripts")
64+
}
65+
}
66+
67+
} else {
68+
// without --browser option, inspect will print object info to the terminal
69+
res := "["
70+
for {
71+
data, isOpen := <-cj
72+
if !isOpen {
73+
break
74+
}
75+
if args.Format == "" {
76+
dataB, err := json.MarshalIndent(data.Object, "", " ")
77+
if err == nil {
78+
if nbInspected != 0 {
79+
res += ",\n"
80+
}
81+
res += string(dataB)
82+
nbInspected++
83+
}
84+
} else {
85+
tmpl, err := template.New("").Funcs(api.FuncMap).Parse(args.Format)
86+
if err != nil {
87+
return fmt.Errorf("format parsing error: %v", err)
88+
}
89+
90+
err = tmpl.Execute(ctx.Stdout, data.Object)
91+
if err != nil {
92+
return fmt.Errorf("format execution error: %v", err)
93+
}
94+
fmt.Fprint(ctx.Stdout, "\n")
95+
nbInspected++
96+
}
97+
}
98+
res += "]"
99+
100+
if args.Format == "" {
101+
if os.Getenv("SCW_SENSITIVE") != "1" {
102+
res = ctx.API.HideAPICredentials(res)
103+
}
104+
fmt.Fprintln(ctx.Stdout, res)
105+
}
106+
}
107+
108+
if len(args.Identifiers) != nbInspected {
109+
os.Exit(1)
110+
}
111+
return nil
112+
}

0 commit comments

Comments
 (0)