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