yunion.io/x/cloudmux@v0.3.10-0-alpha.1/cmd/ncli/main.go (about) 1 // Copyright 2019 Yunion 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package main 16 17 import ( 18 "fmt" 19 "net/http" 20 "net/url" 21 "os" 22 23 "golang.org/x/net/http/httpproxy" 24 25 "yunion.io/x/structarg" 26 27 "yunion.io/x/cloudmux/pkg/cloudprovider" 28 "yunion.io/x/cloudmux/pkg/multicloud/nutanix" 29 _ "yunion.io/x/cloudmux/pkg/multicloud/nutanix/shell" 30 "yunion.io/x/onecloud/pkg/util/shellutils" 31 ) 32 33 type BaseOptions struct { 34 Debug bool `help:"debug mode"` 35 Host string `help:"Host" default:"$NUTANIX_HOST" metavar:"NUTANIX_HOST"` 36 Username string `help:"Username" default:"$NUTANIX_USERNAME" metavar:"NUTANIX_USERNAME"` 37 Password string `help:"Password" default:"$NUTANIX_PASSWORD" metavar:"NUTANIX_PASSWORD"` 38 Port int `help:"Port" default:"$NUTANIX_PORT|9440" metavar:"NUTANIX_PORT"` 39 SUBCOMMAND string `help:"ncli subcommand" subcommand:"true"` 40 } 41 42 func getSubcommandParser() (*structarg.ArgumentParser, error) { 43 parse, e := structarg.NewArgumentParserWithHelp(&BaseOptions{}, 44 "ncli", 45 "Command-line interface to nutanix API.", 46 `See "ncli COMMAND --help" for help on a specific command.`) 47 48 if e != nil { 49 return nil, e 50 } 51 52 subcmd := parse.GetSubcommand() 53 if subcmd == nil { 54 return nil, fmt.Errorf("No subcommand argument.") 55 } 56 for _, v := range shellutils.CommandTable { 57 _, e := subcmd.AddSubParserWithHelp(v.Options, v.Command, v.Desc, v.Callback) 58 if e != nil { 59 return nil, e 60 } 61 } 62 return parse, nil 63 } 64 65 func showErrorAndExit(e error) { 66 fmt.Fprintf(os.Stderr, "%s", e) 67 fmt.Fprintln(os.Stderr) 68 os.Exit(1) 69 } 70 71 func newClient(options *BaseOptions) (*nutanix.SRegion, error) { 72 if len(options.Host) == 0 { 73 return nil, fmt.Errorf("Missing host") 74 } 75 76 if len(options.Username) == 0 { 77 return nil, fmt.Errorf("Missing username") 78 } 79 80 if len(options.Password) == 0 { 81 return nil, fmt.Errorf("Missing password") 82 } 83 84 cfg := &httpproxy.Config{ 85 HTTPProxy: os.Getenv("HTTP_PROXY"), 86 HTTPSProxy: os.Getenv("HTTPS_PROXY"), 87 NoProxy: os.Getenv("NO_PROXY"), 88 } 89 cfgProxyFunc := cfg.ProxyFunc() 90 proxyFunc := func(req *http.Request) (*url.URL, error) { 91 return cfgProxyFunc(req.URL) 92 } 93 94 cli, err := nutanix.NewNutanixClient( 95 nutanix.NewNutanixClientConfig( 96 options.Host, 97 options.Username, 98 options.Password, 99 options.Port, 100 ).Debug(options.Debug). 101 CloudproviderConfig( 102 cloudprovider.ProviderConfig{ 103 ProxyFunc: proxyFunc, 104 }, 105 ), 106 ) 107 if err != nil { 108 return nil, err 109 } 110 111 return cli.GetRegion() 112 } 113 114 func main() { 115 parser, e := getSubcommandParser() 116 if e != nil { 117 showErrorAndExit(e) 118 } 119 e = parser.ParseArgs(os.Args[1:], false) 120 options := parser.Options().(*BaseOptions) 121 122 if parser.IsHelpSet() { 123 fmt.Print(parser.HelpString()) 124 return 125 } 126 subcmd := parser.GetSubcommand() 127 subparser := subcmd.GetSubParser() 128 if e != nil || subparser == nil { 129 if subparser != nil { 130 fmt.Print(subparser.Usage()) 131 } else { 132 fmt.Print(parser.Usage()) 133 } 134 showErrorAndExit(e) 135 return 136 } 137 suboptions := subparser.Options() 138 if subparser.IsHelpSet() { 139 fmt.Print(subparser.HelpString()) 140 return 141 } 142 var region *nutanix.SRegion 143 region, e = newClient(options) 144 if e != nil { 145 showErrorAndExit(e) 146 } 147 e = subcmd.Invoke(region, suboptions) 148 if e != nil { 149 showErrorAndExit(e) 150 } 151 }