yunion.io/x/cloudmux@v0.3.10-0-alpha.1/cmd/awscli/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/pkg/errors" 26 "yunion.io/x/structarg" 27 28 "yunion.io/x/cloudmux/pkg/cloudprovider" 29 "yunion.io/x/cloudmux/pkg/multicloud/aws" 30 _ "yunion.io/x/cloudmux/pkg/multicloud/aws/shell" 31 "yunion.io/x/onecloud/pkg/util/shellutils" 32 ) 33 34 type BaseOptions struct { 35 Debug bool `help:"debug mode"` 36 AccessUrl string `help:"Access key" default:"$AWS_ACCESS_URL" choices:"ChinaCloud|InternationalCloud" metavar:"AWS_ACCESS_URL"` 37 AccessKey string `help:"Access key" default:"$AWS_ACCESS_KEY" metavar:"AWS_ACCESS_KEY"` 38 Secret string `help:"Secret" default:"$AWS_SECRET" metavar:"AWS_SECRET"` 39 RegionId string `help:"RegionId" default:"$AWS_REGION" metavar:"AWS_REGION"` 40 AccountId string `help:"Subaccount ID" default:"$AWS_ACCOUNT_ID" metavar:"AWS_ACCOUNT_ID"` 41 SUBCOMMAND string `help:"awscli subcommand" subcommand:"true"` 42 } 43 44 func getSubcommandParser() (*structarg.ArgumentParser, error) { 45 parse, e := structarg.NewArgumentParserWithHelp(&BaseOptions{}, 46 "awscli", 47 "Command-line interface to aws API.", 48 `See "awscli 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) (*aws.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 := aws.NewAwsClient( 93 aws.NewAwsClientConfig( 94 options.AccessUrl, 95 options.AccessKey, 96 options.Secret, 97 options.AccountId, 98 ).Debug(options.Debug). 99 CloudproviderConfig( 100 cloudprovider.ProviderConfig{ 101 ProxyFunc: proxyFunc, 102 }, 103 ), 104 ) 105 if err != nil { 106 return nil, err 107 } 108 109 region, err := cli.GetRegion(options.RegionId) 110 if err != nil { 111 return nil, errors.Wrapf(err, "GetRegion(%s)", options.RegionId) 112 } 113 114 return region, nil 115 } 116 117 func main() { 118 parser, e := getSubcommandParser() 119 if e != nil { 120 showErrorAndExit(e) 121 } 122 e = parser.ParseArgs(os.Args[1:], false) 123 options := parser.Options().(*BaseOptions) 124 125 if parser.IsHelpSet() { 126 fmt.Print(parser.HelpString()) 127 return 128 } 129 subcmd := parser.GetSubcommand() 130 subparser := subcmd.GetSubParser() 131 if e != nil || subparser == nil { 132 if subparser != nil { 133 fmt.Print(subparser.Usage()) 134 } else { 135 fmt.Print(parser.Usage()) 136 } 137 showErrorAndExit(e) 138 return 139 } 140 suboptions := subparser.Options() 141 if subparser.IsHelpSet() { 142 fmt.Print(subparser.HelpString()) 143 return 144 } 145 var region *aws.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 }