github.com/scaleway/scaleway-cli@v1.11.1/pkg/commands/inspect.go (about) 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 "text/template" 11 12 "github.com/Sirupsen/logrus" 13 "github.com/scaleway/scaleway-cli/pkg/api" 14 "github.com/skratchdot/open-golang/open" 15 ) 16 17 // InspectArgs are flags for the `RunInspect` function 18 type InspectArgs struct { 19 Format string 20 Browser bool 21 Identifiers []string 22 Arch 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, args.Arch) 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 ctx.Getenv("SCW_SENSITIVE") != "1" { 102 res = ctx.API.HideAPICredentials(res) 103 } 104 if len(res) > 2 { 105 fmt.Fprintln(ctx.Stdout, res) 106 } 107 } 108 } 109 110 if len(args.Identifiers) != nbInspected { 111 return fmt.Errorf("at least 1 item failed to be inspected") 112 } 113 return nil 114 }