yunion.io/x/cloudmux@v0.3.10-0-alpha.1/cmd/jdcloudcli/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 "github.com/sirupsen/logrus" 24 "golang.org/x/net/http/httpproxy" 25 26 "yunion.io/x/log" 27 "yunion.io/x/structarg" 28 29 "yunion.io/x/cloudmux/pkg/cloudprovider" 30 "yunion.io/x/cloudmux/pkg/multicloud/jdcloud" 31 _ "yunion.io/x/cloudmux/pkg/multicloud/jdcloud/shell" 32 "yunion.io/x/onecloud/pkg/util/shellutils" 33 ) 34 35 type Options struct { 36 Debug bool `help:"Show debug" default:"false"` 37 AccessKey string `help:"Access key" default:"$JDCLOUD_ACCESS_KEY"` 38 AccessSecret string `help:"Secret" default:"$JDCLOUD_SECRET"` 39 RegionId string `help:"RegionId" default:"$JDCLOUD_REGION"` 40 SUBCOMMAND string `help:"jdcloudcli subcommand" subcommand:"true"` 41 } 42 43 func getSubcommandParser() (*structarg.ArgumentParser, error) { 44 parse, err := structarg.NewArgumentParserWithHelp(&Options{}, 45 "jdcloudcli", 46 "Command-line interface to ecloud API.", 47 `See "jdcloudcli COMMAND --help" for help on a specific command.`, 48 ) 49 if err != nil { 50 return nil, err 51 } 52 53 subcmd := parse.GetSubcommand() 54 if subcmd == nil { 55 return nil, fmt.Errorf("No subcommand argument") 56 } 57 for _, v := range shellutils.CommandTable { 58 _, e := subcmd.AddSubParserWithHelp(v.Options, v.Command, v.Desc, v.Callback) 59 if e != nil { 60 return nil, e 61 } 62 } 63 return parse, nil 64 } 65 66 func showErrorAndExit(e error) { 67 fmt.Fprintf(os.Stderr, "%s", e) 68 fmt.Fprintln(os.Stderr) 69 os.Exit(1) 70 } 71 72 func newClient(options *Options) (*jdcloud.SJDCloudClient, error) { 73 if len(options.AccessKey) == 0 { 74 return nil, fmt.Errorf("Missing access key") 75 } 76 if len(options.AccessSecret) == 0 { 77 return nil, fmt.Errorf("Missing access secret") 78 } 79 regionId := options.RegionId 80 if regionId == "" { 81 regionId = jdcloud.JDCLOUD_DEFAULT_REGION 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 cfcg := cloudprovider.ProviderConfig{ 95 ProxyFunc: proxyFunc, 96 } 97 98 return jdcloud.NewJDCloudClient( 99 jdcloud.NewJDCloudClientConfig( 100 options.AccessKey, 101 options.AccessSecret, 102 ).CloudproviderConfig(cfcg).Debug(options.Debug), 103 ) 104 } 105 106 func main() { 107 parser, err := getSubcommandParser() 108 if err != nil { 109 showErrorAndExit(err) 110 } 111 err = parser.ParseArgs(os.Args[1:], false) 112 options := parser.Options().(*Options) 113 114 if parser.IsHelpSet() { 115 fmt.Print(parser.HelpString()) 116 return 117 } 118 if options.Debug { 119 log.SetLogLevel(log.Logger(), logrus.DebugLevel) 120 } else { 121 log.SetLogLevel(log.Logger(), logrus.InfoLevel) 122 } 123 subcmd := parser.GetSubcommand() 124 subparser := subcmd.GetSubParser() 125 if err != nil || subparser == nil { 126 if subparser != nil { 127 fmt.Print(subparser.Usage()) 128 } else { 129 fmt.Print(parser.Usage()) 130 } 131 showErrorAndExit(err) 132 } 133 suboptions := subparser.Options() 134 if subparser.IsHelpSet() { 135 fmt.Print(subparser.HelpString()) 136 return 137 } 138 client, err := newClient(options) 139 if err != nil { 140 showErrorAndExit(err) 141 } 142 region := client.GetRegion(options.RegionId) 143 if region == nil { 144 fmt.Printf("not found region: %s", options.RegionId) 145 return 146 } 147 err = subcmd.Invoke(region, suboptions) 148 if err != nil { 149 showErrorAndExit(err) 150 } 151 }