yunion.io/x/cloudmux@v0.3.10-0-alpha.1/cmd/hcscli/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/hcs" 29 _ "yunion.io/x/cloudmux/pkg/multicloud/hcs/shell" 30 "yunion.io/x/onecloud/pkg/util/shellutils" 31 ) 32 33 type BaseOptions struct { 34 Debug bool `help:"debug mode"` 35 AuthUrl string `help:"Auth url" default:"$HCS_AUTH_URL"` 36 AccessKey string `help:"Access key" default:"$HCS_ACCESS_KEY" metavar:"HCS_ACCESS_KEY"` 37 Secret string `help:"Secret" default:"$HCS_SECRET" metavar:"HCS_SECRET"` 38 RegionId string `help:"RegionId" default:"$HCS_REGION" metavar:"HCS_REGION"` 39 ProjectId string `help:"RegionId" default:"$HCS_PROJECT_ID" metavar:"HCS_PROJECT_ID"` 40 SUBCOMMAND string `help:"hcscli subcommand" subcommand:"true"` 41 } 42 43 func getSubcommandParser() (*structarg.ArgumentParser, error) { 44 parse, e := structarg.NewArgumentParserWithHelp(&BaseOptions{}, 45 "hcscli", 46 "Command-line interface to hcs API.", 47 `See "hcscli COMMAND --help" for help on a specific command.`) 48 49 if e != nil { 50 return nil, e 51 } 52 53 subcmd := parse.GetSubcommand() 54 if subcmd == nil { 55 return nil, fmt.Errorf("No subcommand argument.") 56 } 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) (*hcs.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 if len(options.AuthUrl) == 0 { 83 return nil, fmt.Errorf("Missing authUrl") 84 } 85 86 if len(options.RegionId) == 0 { 87 return nil, fmt.Errorf("Missing regionId") 88 } 89 90 cfg := &httpproxy.Config{ 91 HTTPProxy: os.Getenv("HTTP_PROXY"), 92 HTTPSProxy: os.Getenv("HTTPS_PROXY"), 93 NoProxy: os.Getenv("NO_PROXY"), 94 } 95 cfgProxyFunc := cfg.ProxyFunc() 96 proxyFunc := func(req *http.Request) (*url.URL, error) { 97 return cfgProxyFunc(req.URL) 98 } 99 100 cli, err := hcs.NewHcsClient( 101 hcs.NewHcsConfig( 102 options.AccessKey, 103 options.Secret, 104 options.ProjectId, 105 options.AuthUrl, 106 ).Debug(options.Debug). 107 CloudproviderConfig( 108 cloudprovider.ProviderConfig{ 109 ProxyFunc: proxyFunc, 110 }, 111 ), 112 ) 113 if err != nil { 114 return nil, err 115 } 116 117 return cli.GetRegion(options.RegionId) 118 } 119 120 func main() { 121 parser, e := getSubcommandParser() 122 if e != nil { 123 showErrorAndExit(e) 124 } 125 e = parser.ParseArgs(os.Args[1:], false) 126 options := parser.Options().(*BaseOptions) 127 128 if parser.IsHelpSet() { 129 fmt.Print(parser.HelpString()) 130 return 131 } 132 subcmd := parser.GetSubcommand() 133 subparser := subcmd.GetSubParser() 134 if e != nil || subparser == nil { 135 if subparser != nil { 136 fmt.Print(subparser.Usage()) 137 } else { 138 fmt.Print(parser.Usage()) 139 } 140 showErrorAndExit(e) 141 return 142 } 143 suboptions := subparser.Options() 144 if subparser.IsHelpSet() { 145 fmt.Print(subparser.HelpString()) 146 return 147 } 148 var region *hcs.SRegion 149 region, e = newClient(options) 150 if e != nil { 151 showErrorAndExit(e) 152 } 153 e = subcmd.Invoke(region, suboptions) 154 if e != nil { 155 showErrorAndExit(e) 156 } 157 }