This repository has been archived by the owner on Sep 15, 2021. It is now read-only.
forked from scaleway/scaleway-cli
/
inspect.go
114 lines (103 loc) · 2.95 KB
/
inspect.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
// 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"
"text/template"
"github.com/Sirupsen/logrus"
"github.com/scaleway/scaleway-cli/pkg/api"
"github.com/skratchdot/open-golang/open"
)
// InspectArgs are flags for the `RunInspect` function
type InspectArgs struct {
Format string
Browser bool
Identifiers []string
Arch 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, args.Arch)
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 ctx.Getenv("SCW_SENSITIVE") != "1" {
res = ctx.API.HideAPICredentials(res)
}
if len(res) > 2 {
fmt.Fprintln(ctx.Stdout, res)
}
}
}
if len(args.Identifiers) != nbInspected {
return fmt.Errorf("at least 1 item failed to be inspected")
}
return nil
}