yunion.io/x/cloudmux@v0.3.10-0-alpha.1/cmd/hcsocli/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 huawei "yunion.io/x/cloudmux/pkg/multicloud/hcso" 29 _ "yunion.io/x/cloudmux/pkg/multicloud/hcso/shell" 30 "yunion.io/x/onecloud/pkg/util/shellutils" 31 ) 32 33 type BaseOptions struct { 34 cloudprovider.SHCSOEndpoints 35 Debug bool `help:"Show debug" default:"false"` 36 AccessKey string `help:"Access key" default:"$HUAWEI_ACCESS_KEY" metavar:"HUAWEI_ACCESS_KEY"` 37 Secret string `help:"Secret" default:"$HUAWEI_SECRET" metavar:"HUAWEI_SECRET"` 38 RegionId string `help:"RegionId" default:"$HUAWEI_REGION" metavar:"HUAWEI_REGION"` 39 DEFAULT_REGION string `help:"Default Region" default:"$HUAWEI_DEFAULT_REGION" metavar:"HUAWEI_DEFAULT_REGION"` 40 ProjectId string `help:"ProjectId" default:"$HUAWEI_PROJECT" metavar:"HUAWEI_PROJECT"` 41 SUBCOMMAND string `help:"huaweicli subcommand" subcommand:"true"` 42 } 43 44 func getSubcommandParser() (*structarg.ArgumentParser, error) { 45 parse, e := structarg.NewArgumentParserWithHelp(&BaseOptions{}, 46 "hcsocli", 47 "Command-line interface to huawei API.", 48 `See "hcsocli COMMAND --help" for help on a specific command.`) 49 50 if e != nil { 51 return nil, e 52 } 53 54 subcmd := parse.GetSubcommand() 55 if subcmd == nil { 56 return nil, fmt.Errorf("No subcommand argument.") 57 } 58 for _, v := range shellutils.CommandTable { 59 _, e := subcmd.AddSubParserWithHelp(v.Options, v.Command, v.Desc, v.Callback) 60 if e != nil { 61 return nil, e 62 } 63 } 64 return parse, nil 65 } 66 67 func showErrorAndExit(e error) { 68 fmt.Fprintf(os.Stderr, "%s", e) 69 fmt.Fprintln(os.Stderr) 70 os.Exit(1) 71 } 72 73 func newClient(options *BaseOptions) (*huawei.SRegion, error) { 74 if len(options.AccessKey) == 0 { 75 return nil, fmt.Errorf("Missing accessKey") 76 } 77 78 if len(options.Secret) == 0 { 79 return nil, fmt.Errorf("Missing secret") 80 } 81 82 cfg := &httpproxy.Config{ 83 HTTPProxy: os.Getenv("HTTP_PROXY"), 84 HTTPSProxy: os.Getenv("HTTPS_PROXY"), 85 NoProxy: os.Getenv("NO_PROXY"), 86 } 87 cfgProxyFunc := cfg.ProxyFunc() 88 proxyFunc := func(req *http.Request) (*url.URL, error) { 89 return cfgProxyFunc(req.URL) 90 } 91 92 cli, err := huawei.NewHuaweiClient( 93 huawei.NewHuaweiClientConfig( 94 options.AccessKey, 95 options.Secret, 96 options.ProjectId, 97 &options.SHCSOEndpoints, 98 ).Debug(options.Debug). 99 CloudproviderConfig( 100 cloudprovider.ProviderConfig{ 101 ProxyFunc: proxyFunc, 102 DefaultRegion: options.DEFAULT_REGION, 103 }, 104 ), 105 ) 106 if err != nil { 107 return nil, err 108 } 109 110 region := cli.GetRegion(options.RegionId) 111 if region == nil { 112 return nil, fmt.Errorf("No such region %s", options.RegionId) 113 } 114 115 return region, nil 116 } 117 118 func main() { 119 parser, e := getSubcommandParser() 120 if e != nil { 121 showErrorAndExit(e) 122 } 123 e = parser.ParseArgs(os.Args[1:], false) 124 options := parser.Options().(*BaseOptions) 125 126 if parser.IsHelpSet() { 127 fmt.Print(parser.HelpString()) 128 return 129 } 130 subcmd := parser.GetSubcommand() 131 subparser := subcmd.GetSubParser() 132 if e != nil || subparser == nil { 133 if subparser != nil { 134 fmt.Print(subparser.Usage()) 135 } else { 136 fmt.Print(parser.Usage()) 137 } 138 showErrorAndExit(e) 139 } 140 suboptions := subparser.Options() 141 if subparser.IsHelpSet() { 142 fmt.Print(subparser.HelpString()) 143 return 144 } 145 var region *huawei.SRegion 146 region, e = newClient(options) 147 if e != nil { 148 showErrorAndExit(e) 149 } 150 e = subcmd.Invoke(region, suboptions) 151 if e != nil { 152 showErrorAndExit(e) 153 } 154 }