|
| 1 | +package lib |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "strings" |
| 7 | +) |
| 8 | + |
| 9 | +var specChineseBucketCname = SpecText{ |
| 10 | + synopsisText: "管理bucket cname", |
| 11 | + |
| 12 | + paramText: "bucket_url [local_xml_file] [options]", |
| 13 | + |
| 14 | + syntaxText: ` |
| 15 | + ossutil bucket-cname --method get oss://bucket [local_xml_file] [options] |
| 16 | +`, |
| 17 | + detailHelpText: ` |
| 18 | + cname命令通过设置method选项值为get可以查询bucket的cname配置 |
| 19 | +
|
| 20 | +用法: |
| 21 | + 该命令只有一种种用法: |
| 22 | + 1) ossutil bucket-cname --method get oss://bucket [local_xml_file] [options] |
| 23 | + 这个命令查询bucket的cname配置 |
| 24 | + 如果输入参数local_xml_file,cname配置将输出到该文件,否则输出到屏幕上 |
| 25 | +`, |
| 26 | + sampleText: ` |
| 27 | + 1) 查询bucket的cname配置,结果输出到标准输出 |
| 28 | + ossutil bucket-cname --method get oss://bucket |
| 29 | +`, |
| 30 | +} |
| 31 | + |
| 32 | +var specEnglishBucketCname = SpecText{ |
| 33 | + synopsisText: "get bucket cname configuration", |
| 34 | + |
| 35 | + paramText: "bucket_url [local_xml_file] [options]", |
| 36 | + |
| 37 | + syntaxText: ` |
| 38 | + ossutil bucket-cname --method get oss://bucket [local_xml_file] [options] |
| 39 | +`, |
| 40 | + detailHelpText: ` |
| 41 | + bucket-cname command can get the cname configuration of the oss bucket by |
| 42 | + set method option value to get |
| 43 | +
|
| 44 | +Usage: |
| 45 | + There are only one usage for this command: |
| 46 | + 1) ossutil bucket-cname --method get oss://bucket [local_xml_file] [options] |
| 47 | + The command gets the cname configuration of bucket |
| 48 | + If you input parameter local_xml_file,the configuration will be output to local_xml_file |
| 49 | + If you don't input parameter local_xml_file,the configuration will be output to stdout |
| 50 | +`, |
| 51 | + sampleText: ` |
| 52 | + 1) get cname configuration to stdout |
| 53 | + ossutil bucket-cname --method get oss://bucket |
| 54 | +`, |
| 55 | +} |
| 56 | + |
| 57 | +type bucketCnameOptionType struct { |
| 58 | + bucketName string |
| 59 | +} |
| 60 | + |
| 61 | +type BucketCnameCommand struct { |
| 62 | + command Command |
| 63 | + bwOption bucketCnameOptionType |
| 64 | +} |
| 65 | + |
| 66 | +var bucketCnameCommand = BucketCnameCommand{ |
| 67 | + command: Command{ |
| 68 | + name: "bucket-cname", |
| 69 | + nameAlias: []string{"bucket-cname"}, |
| 70 | + minArgc: 1, |
| 71 | + maxArgc: 2, |
| 72 | + specChinese: specChineseBucketCname, |
| 73 | + specEnglish: specEnglishBucketCname, |
| 74 | + group: GroupTypeNormalCommand, |
| 75 | + validOptionNames: []string{ |
| 76 | + OptionConfigFile, |
| 77 | + OptionEndpoint, |
| 78 | + OptionAccessKeyID, |
| 79 | + OptionAccessKeySecret, |
| 80 | + OptionSTSToken, |
| 81 | + OptionProxyHost, |
| 82 | + OptionProxyUser, |
| 83 | + OptionProxyPwd, |
| 84 | + OptionLogLevel, |
| 85 | + OptionMethod, |
| 86 | + OptionPassword, |
| 87 | + OptionMode, |
| 88 | + OptionECSRoleName, |
| 89 | + OptionTokenTimeout, |
| 90 | + OptionRamRoleArn, |
| 91 | + OptionRoleSessionName, |
| 92 | + OptionReadTimeout, |
| 93 | + OptionConnectTimeout, |
| 94 | + OptionSTSRegion, |
| 95 | + OptionSkipVerfiyCert, |
| 96 | + OptionUserAgent, |
| 97 | + }, |
| 98 | + }, |
| 99 | +} |
| 100 | + |
| 101 | +// function for FormatHelper interface |
| 102 | +func (bwc *BucketCnameCommand) formatHelpForWhole() string { |
| 103 | + return bwc.command.formatHelpForWhole() |
| 104 | +} |
| 105 | + |
| 106 | +func (bwc *BucketCnameCommand) formatIndependHelp() string { |
| 107 | + return bwc.command.formatIndependHelp() |
| 108 | +} |
| 109 | + |
| 110 | +// Init simulate inheritance, and polymorphism |
| 111 | +func (bwc *BucketCnameCommand) Init(args []string, options OptionMapType) error { |
| 112 | + return bwc.command.Init(args, options, bwc) |
| 113 | +} |
| 114 | + |
| 115 | +// RunCommand simulate inheritance, and polymorphism |
| 116 | +func (bwc *BucketCnameCommand) RunCommand() error { |
| 117 | + strMethod, _ := GetString(OptionMethod, bwc.command.options) |
| 118 | + if strMethod == "" { |
| 119 | + return fmt.Errorf("--method value is empty") |
| 120 | + } |
| 121 | + |
| 122 | + strMethod = strings.ToLower(strMethod) |
| 123 | + if strMethod != "get" { |
| 124 | + return fmt.Errorf("--method only support get") |
| 125 | + } |
| 126 | + |
| 127 | + srcBucketUrL, err := GetCloudUrl(bwc.command.args[0], "") |
| 128 | + if err != nil { |
| 129 | + return err |
| 130 | + } |
| 131 | + |
| 132 | + bwc.bwOption.bucketName = srcBucketUrL.bucket |
| 133 | + return bwc.GetBucketCname() |
| 134 | +} |
| 135 | + |
| 136 | +func (bwc *BucketCnameCommand) GetBucketCname() error { |
| 137 | + client, err := bwc.command.ossClient(bwc.bwOption.bucketName) |
| 138 | + if err != nil { |
| 139 | + return err |
| 140 | + } |
| 141 | + |
| 142 | + output, err := client.GetBucketCname(bwc.bwOption.bucketName) |
| 143 | + if err != nil { |
| 144 | + return err |
| 145 | + } |
| 146 | + |
| 147 | + var outFile *os.File |
| 148 | + if len(bwc.command.args) >= 2 { |
| 149 | + fileName := bwc.command.args[1] |
| 150 | + _, err = os.Stat(fileName) |
| 151 | + if err == nil { |
| 152 | + bConitnue := bwc.confirm(fileName) |
| 153 | + if !bConitnue { |
| 154 | + return nil |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + outFile, err = os.OpenFile(fileName, os.O_CREATE|os.O_TRUNC|os.O_RDWR, 0660) |
| 159 | + if err != nil { |
| 160 | + return err |
| 161 | + } |
| 162 | + defer outFile.Close() |
| 163 | + } else { |
| 164 | + outFile = os.Stdout |
| 165 | + } |
| 166 | + |
| 167 | + outFile.Write([]byte(output)) |
| 168 | + |
| 169 | + fmt.Printf("\n\n") |
| 170 | + |
| 171 | + return nil |
| 172 | +} |
| 173 | + |
| 174 | +func (bwc *BucketCnameCommand) confirm(str string) bool { |
| 175 | + var val string |
| 176 | + fmt.Printf(getClearStr(fmt.Sprintf("bucket cname: overwrite \"%s\"(y or N)? ", str))) |
| 177 | + if _, err := fmt.Scanln(&val); err != nil || (strings.ToLower(val) != "yes" && strings.ToLower(val) != "y") { |
| 178 | + return false |
| 179 | + } |
| 180 | + return true |
| 181 | +} |
0 commit comments