main.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. package main
  15. import (
  16. "fmt"
  17. "net/http"
  18. "net/url"
  19. "os"
  20. "golang.org/x/net/http/httpproxy"
  21. "yunion.io/x/cloudmux/pkg/cloudprovider"
  22. "yunion.io/x/pkg/util/shellutils"
  23. "yunion.io/x/structarg"
  24. "yunion.io/x/onecloud/pkg/mcclient/cloudpods"
  25. _ "yunion.io/x/onecloud/pkg/mcclient/cloudpods/shell"
  26. )
  27. type BaseOptions struct {
  28. Debug bool `help:"debug mode"`
  29. AuthURL string `help:"Auth URL" default:"$CLOUDPODS_AUTH_URL" metavar:"CLOUDPODS_AUTH_URL"`
  30. AccessKey string `help:"AccessKey" default:"$CLOUDPODS_ACCESS_KEY" metavar:"CLOUDPODS_ACCESS_KEY"`
  31. AccessSecret string `help:"AccessSecret" default:"$CLOUDPODS_ACCESS_SECRET" metavar:"CLOUDPODS_ACCESS_SECRET"`
  32. RegionId string `help:"RegionId" default:"$CLOUDPODS_REGION_ID|default" metavar:"CLOUDPODS_REGION_ID"`
  33. SUBCOMMAND string `help:"cloudpodscli subcommand" subcommand:"true"`
  34. }
  35. func getSubcommandParser() (*structarg.ArgumentParser, error) {
  36. parse, err := structarg.NewArgumentParserWithHelp(&BaseOptions{},
  37. "cloudpodscli",
  38. "Command-line interface to cloudpods API.",
  39. `See "cloudpodscli COMMAND --help" for help on a specific command.`)
  40. if err != nil {
  41. return nil, err
  42. }
  43. subcmd := parse.GetSubcommand()
  44. if subcmd == nil {
  45. return nil, fmt.Errorf("No subcommand argument.")
  46. }
  47. for _, v := range shellutils.CommandTable {
  48. _, err = subcmd.AddSubParserWithHelp(v.Options, v.Command, v.Desc, v.Callback)
  49. if err != nil {
  50. return nil, err
  51. }
  52. }
  53. return parse, nil
  54. }
  55. func showErrorAndExit(e error) {
  56. fmt.Fprintf(os.Stderr, "%s", e)
  57. fmt.Fprintln(os.Stderr)
  58. os.Exit(1)
  59. }
  60. func newClient(options *BaseOptions) (*cloudpods.SRegion, error) {
  61. if len(options.AuthURL) == 0 {
  62. return nil, fmt.Errorf("Missing AuthURL")
  63. }
  64. if len(options.AccessKey) == 0 {
  65. return nil, fmt.Errorf("Missing access key")
  66. }
  67. if len(options.AccessSecret) == 0 {
  68. return nil, fmt.Errorf("Missing access secret")
  69. }
  70. cfg := &httpproxy.Config{
  71. HTTPProxy: os.Getenv("HTTP_PROXY"),
  72. HTTPSProxy: os.Getenv("HTTPS_PROXY"),
  73. NoProxy: os.Getenv("NO_PROXY"),
  74. }
  75. cfgProxyFunc := cfg.ProxyFunc()
  76. proxyFunc := func(req *http.Request) (*url.URL, error) {
  77. return cfgProxyFunc(req.URL)
  78. }
  79. cli, err := cloudpods.NewCloudpodsClient(
  80. cloudpods.NewCloudpodsClientConfig(
  81. options.AuthURL,
  82. options.AccessKey,
  83. options.AccessSecret,
  84. ).Debug(options.Debug).
  85. CloudproviderConfig(
  86. cloudprovider.ProviderConfig{
  87. ProxyFunc: proxyFunc,
  88. },
  89. ),
  90. )
  91. if err != nil {
  92. return nil, err
  93. }
  94. region, err := cli.GetRegion(options.RegionId)
  95. if err != nil {
  96. return nil, err
  97. }
  98. return region, nil
  99. }
  100. func main() {
  101. parser, err := getSubcommandParser()
  102. if err != nil {
  103. showErrorAndExit(err)
  104. }
  105. err = parser.ParseArgs(os.Args[1:], false)
  106. options := parser.Options().(*BaseOptions)
  107. if parser.IsHelpSet() {
  108. fmt.Print(parser.HelpString())
  109. return
  110. }
  111. subcmd := parser.GetSubcommand()
  112. subparser := subcmd.GetSubParser()
  113. if err != nil || subparser == nil {
  114. if subparser != nil {
  115. fmt.Print(subparser.Usage())
  116. } else {
  117. fmt.Print(parser.Usage())
  118. }
  119. showErrorAndExit(err)
  120. return
  121. }
  122. suboptions := subparser.Options()
  123. if subparser.IsHelpSet() {
  124. fmt.Print(subparser.HelpString())
  125. return
  126. }
  127. var region *cloudpods.SRegion
  128. region, err = newClient(options)
  129. if err != nil {
  130. showErrorAndExit(err)
  131. }
  132. err = subcmd.Invoke(region, suboptions)
  133. if err != nil {
  134. showErrorAndExit(err)
  135. }
  136. }