|
5 | 5 | package cli
|
6 | 6 |
|
7 | 7 | import (
|
8 |
| - "encoding/json" |
9 |
| - "fmt" |
10 |
| - "os" |
11 |
| - "text/template" |
| 8 | + "github.com/scaleway/scaleway-cli/vendor/github.com/Sirupsen/logrus" |
12 | 9 |
|
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" |
17 | 11 | )
|
18 | 12 |
|
19 | 13 | var cmdInspect = &Command{
|
@@ -51,96 +45,22 @@ var inspectFormat string // -f, --format flag
|
51 | 45 | var inspectBrowser bool // -b, --browser flag
|
52 | 46 | var inspectHelp bool // -h, --help flag
|
53 | 47 |
|
54 |
| -func runInspect(cmd *Command, args []string) { |
| 48 | +func runInspect(cmd *Command, rawArgs []string) { |
55 | 49 | if inspectHelp {
|
56 | 50 | cmd.PrintUsage()
|
57 | 51 | }
|
58 |
| - if len(args) < 1 { |
| 52 | + if len(rawArgs) < 1 { |
59 | 53 | cmd.PrintShortUsage()
|
60 | 54 | }
|
61 | 55 |
|
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, |
141 | 60 | }
|
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) |
145 | 65 | }
|
146 | 66 | }
|
0 commit comments