1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
| package main
import ( "fmt"
"github.com/spf13/cobra" )
var RootCmd = &cobra.Command{ Use: "cloud-station-cli", Long: "欢迎使用云存储文件中转站", Short: "欢迎使用云存储文件中转站", Example: "cloud-station-cli cmds", RunE: func(cmd *cobra.Command, args []string) error { if version { fmt.Println("cloud-station cli v0.0.1") } return nil }, }
var ( version bool )
func init() { RootCmd.PersistentFlags().BoolVarP(&version, "version", "v", false, "show version") UploadCmd.PersistentFlags().StringVarP(&accessKey, "access_id", "k", "", "the ali oss access id") UploadCmd.PersistentFlags().StringVarP(&secretKey, "secret_key", "s", "", "the ali oss access key") UploadCmd.PersistentFlags().StringVarP(&ossProvider, "oss_provider", "p", "aliyun", "the oss provider [aliyun/qcloud]") UploadCmd.PersistentFlags().StringVarP(&ossEndpoint, "oss_endpoint", "e", "oss-cn-beijing.aliyuncs.com", "oss store endpointer") UploadCmd.PersistentFlags().StringVarP(&bucketName, "bucket_name", "b", "devcloud-station", "bucket name") UploadCmd.PersistentFlags().StringVarP(&uploadFile, "upload_file", "f", "", "upload file") RootCmd.AddCommand(UploadCmd) }
var UploadCmd = &cobra.Command{ Use: "upload", Long: "upload 文件上传", Short: "upload 文件上传", Example: "upload -f filename", RunE: func(cmd *cobra.Command, args []string) error { setAk()
var ( uploader store.Uploader err error ) switch ossProvider { case "aliyun": uploader, err = aliyun.NewAliOssStore(ossEndpoint, accessKey, secretKey) case "qcloud": default: return fmt.Errorf("not support oss storage provider") }
if err != nil { return err }
return uploader.Upload(bucketName, uploadFile, uploadFile) }, }
func main() { if err := RootCmd.Execute(); err != nil { log.Fatal(err) } }
|