1
+ package cmd
2
+
3
+ import (
4
+ "fmt"
5
+ "os"
6
+ "github.com/spf13/cobra"
7
+ "github.com/spf13/viper"
8
+ )
9
+
10
+ // RootCmd represents the base command when called without any subcommands
11
+ var RootCmd = & cobra.Command {
12
+ Use : "vcloud-cli" ,
13
+ Short : "a command line interface for the vcloud director api" ,
14
+ Long : `a command line interface for the vcloud director api` ,
15
+ }
16
+
17
+ // Execute adds all child commands to the root command and sets flags appropriately.
18
+ // This is called by main.main(). It only needs to happen once to the rootCmd.
19
+ func Execute () {
20
+ if err := RootCmd .Execute (); err != nil {
21
+ fmt .Println (err )
22
+ os .Exit (1 )
23
+ }
24
+ }
25
+
26
+ func init () {
27
+
28
+ RootCmd .PersistentFlags ().String ("url" , "" , "url of vcloud director api" )
29
+ RootCmd .PersistentFlags ().String ("user" , "" , "Port to run Application server on" )
30
+ RootCmd .PersistentFlags ().String ( "password" , "" , "password of vcloud director api" )
31
+ RootCmd .PersistentFlags ().String ("org" , "" , "org of vcloud director api" )
32
+ viper .BindPFlag ("url" , RootCmd .PersistentFlags ().Lookup ("url" ))
33
+ viper .BindPFlag ("user" , RootCmd .PersistentFlags ().Lookup ("user" ))
34
+ viper .BindPFlag ("password" , RootCmd .PersistentFlags ().Lookup ("password" ))
35
+ viper .BindPFlag ("org" , RootCmd .PersistentFlags ().Lookup ("org" ))
36
+
37
+ viper .SetEnvPrefix ("vcd" ) // will be uppercased automatically
38
+ viper .AutomaticEnv ()
39
+
40
+ url := viper .GetString ("url" )
41
+ if len (url ) == 0 {
42
+ fmt .Println ("url has to be set, either as env var VCD_URL or as flag url" )
43
+ os .Exit (1 )
44
+ }
45
+ user := viper .GetString ("user" )
46
+ if len (user ) == 0 {
47
+ fmt .Println ("user has to be set, either as env var VCD_USER or as flag user" )
48
+ os .Exit (1 )
49
+ }
50
+ password := viper .GetString ("password" )
51
+ if len (password ) == 0 {
52
+ fmt .Println ("password has to be set, either as env var VCD_PASSWORD or as flag password" )
53
+ os .Exit (1 )
54
+ }
55
+ org := viper .GetString ("org" )
56
+ if len (org ) == 0 {
57
+ fmt .Println ("user has to be set, either as env var VCD_ORG or as flag org" )
58
+ os .Exit (1 )
59
+ }
60
+
61
+ fmt .Printf ("VCD_URL: [%s]\n " , url )
62
+ fmt .Printf ("VCD_USER: [%s]\n " , user )
63
+ fmt .Printf ("VCD_PASSWORD: [%s]\n " , "***************" )
64
+ fmt .Printf ("VCD_ORG: [%s]\n " , org )
65
+ }
0 commit comments