yunion.io/x/cloudmux@v0.3.10-0-alpha.1/cmd/s3cli/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 "os" 20 21 "yunion.io/x/structarg" 22 23 api "yunion.io/x/onecloud/pkg/apis/compute" 24 "yunion.io/x/cloudmux/pkg/cloudprovider" 25 "yunion.io/x/cloudmux/pkg/multicloud/objectstore" 26 "yunion.io/x/cloudmux/pkg/multicloud/objectstore/ceph" 27 _ "yunion.io/x/cloudmux/pkg/multicloud/objectstore/shell" 28 "yunion.io/x/cloudmux/pkg/multicloud/objectstore/xsky" 29 "yunion.io/x/onecloud/pkg/util/shellutils" 30 ) 31 32 type BaseOptions struct { 33 Debug bool `help:"debug mode"` 34 AccessUrl string `help:"Access url" default:"$S3_ACCESS_URL" metavar:"S3_ACCESS_URL"` 35 AccessKey string `help:"Access key" default:"$S3_ACCESS_KEY" metavar:"S3_ACCESS_KEY"` 36 Secret string `help:"Secret" default:"$S3_SECRET" metavar:"S3_SECRET"` 37 Backend string `help:"Backend driver" default:"$S3_BACKEND" metavar:"S3_BACKEND"` 38 SUBCOMMAND string `help:"s3cli subcommand" subcommand:"true"` 39 } 40 41 func getSubcommandParser() (*structarg.ArgumentParser, error) { 42 parse, e := structarg.NewArgumentParserWithHelp(&BaseOptions{}, 43 "s3cli", 44 "Command-line interface to standard S3 API.", 45 `See "s3cli 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 *BaseOptions) (cloudprovider.ICloudRegion, error) { 71 if len(options.AccessUrl) == 0 { 72 return nil, fmt.Errorf("Missing accessUrl") 73 } 74 75 if len(options.AccessKey) == 0 { 76 return nil, fmt.Errorf("Missing accessKey") 77 } 78 79 if len(options.Secret) == 0 { 80 return nil, fmt.Errorf("Missing secret") 81 } 82 83 if options.Backend == api.CLOUD_PROVIDER_CEPH { 84 return ceph.NewCephRados( 85 objectstore.NewObjectStoreClientConfig( 86 options.AccessUrl, options.AccessKey, options.Secret, 87 ).Debug(options.Debug), 88 ) 89 } else if options.Backend == api.CLOUD_PROVIDER_XSKY { 90 return xsky.NewXskyClient( 91 objectstore.NewObjectStoreClientConfig( 92 options.AccessUrl, options.AccessKey, options.Secret, 93 ).Debug(options.Debug), 94 ) 95 } 96 return objectstore.NewObjectStoreClient( 97 objectstore.NewObjectStoreClientConfig( 98 options.AccessUrl, options.AccessKey, options.Secret, 99 ).Debug(options.Debug), 100 ) 101 } 102 103 func main() { 104 parser, e := getSubcommandParser() 105 if e != nil { 106 showErrorAndExit(e) 107 } 108 e = parser.ParseArgs(os.Args[1:], false) 109 options := parser.Options().(*BaseOptions) 110 111 if parser.IsHelpSet() { 112 fmt.Print(parser.HelpString()) 113 return 114 } 115 subcmd := parser.GetSubcommand() 116 subparser := subcmd.GetSubParser() 117 if e != nil || subparser == nil { 118 if subparser != nil { 119 fmt.Print(subparser.Usage()) 120 } else { 121 fmt.Print(parser.Usage()) 122 } 123 showErrorAndExit(e) 124 } 125 suboptions := subparser.Options() 126 if subparser.IsHelpSet() { 127 fmt.Print(subparser.HelpString()) 128 return 129 } 130 var client cloudprovider.ICloudRegion 131 client, e = newClient(options) 132 if e != nil { 133 showErrorAndExit(e) 134 } 135 e = subcmd.Invoke(client, suboptions) 136 if e != nil { 137 showErrorAndExit(e) 138 } 139 }