|
| 1 | +package lib |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + |
| 6 | + oss "github.com/aliyun/aliyun-oss-go-sdk/oss" |
| 7 | +) |
| 8 | + |
| 9 | +var specChineseListCloudBox = SpecText{ |
| 10 | + |
| 11 | + synopsisText: "列举云盒信息", |
| 12 | + |
| 13 | + paramText: "[options]", |
| 14 | + |
| 15 | + syntaxText: ` |
| 16 | + ossutil lcb [-e endpoint] |
| 17 | +`, |
| 18 | + |
| 19 | + detailHelpText: ` |
| 20 | + 该命令列举云盒的详细信息 |
| 21 | +`, |
| 22 | + |
| 23 | + sampleText: ` |
| 24 | + 1) ossutil lcb --sign-version v4 --region cloudbox-id |
| 25 | +`, |
| 26 | +} |
| 27 | + |
| 28 | +var specEnglishListCloudBox = SpecText{ |
| 29 | + |
| 30 | + synopsisText: "List cloud box information", |
| 31 | + |
| 32 | + paramText: "[options]", |
| 33 | + |
| 34 | + syntaxText: ` |
| 35 | + ossutil lcb [-e endpoint] |
| 36 | +`, |
| 37 | + |
| 38 | + detailHelpText: ` |
| 39 | + This command lists cloud box information |
| 40 | +`, |
| 41 | + |
| 42 | + sampleText: ` |
| 43 | + 1) ossutil lcb --sign-version v4 --region cloudbox-id |
| 44 | +`, |
| 45 | +} |
| 46 | + |
| 47 | +// LcbCommand is the command list region buckets or objects |
| 48 | +type LcbCommand struct { |
| 49 | + command Command |
| 50 | +} |
| 51 | + |
| 52 | +var lcbCommand = LcbCommand{ |
| 53 | + command: Command{ |
| 54 | + name: "lcb", |
| 55 | + nameAlias: []string{"lcb"}, |
| 56 | + minArgc: 0, |
| 57 | + maxArgc: 1, |
| 58 | + specChinese: specChineseListCloudBox, |
| 59 | + specEnglish: specEnglishListCloudBox, |
| 60 | + group: GroupTypeNormalCommand, |
| 61 | + validOptionNames: []string{ |
| 62 | + OptionConfigFile, |
| 63 | + OptionEndpoint, |
| 64 | + OptionAccessKeyID, |
| 65 | + OptionAccessKeySecret, |
| 66 | + OptionSTSToken, |
| 67 | + OptionProxyHost, |
| 68 | + OptionProxyUser, |
| 69 | + OptionProxyPwd, |
| 70 | + OptionRetryTimes, |
| 71 | + OptionLogLevel, |
| 72 | + OptionPassword, |
| 73 | + OptionMode, |
| 74 | + OptionECSRoleName, |
| 75 | + OptionTokenTimeout, |
| 76 | + OptionRamRoleArn, |
| 77 | + OptionRoleSessionName, |
| 78 | + OptionReadTimeout, |
| 79 | + OptionConnectTimeout, |
| 80 | + OptionSTSRegion, |
| 81 | + OptionSkipVerfiyCert, |
| 82 | + OptionUserAgent, |
| 83 | + OptionRegion, |
| 84 | + OptionSignVersion, |
| 85 | + OptionLimitedNum, |
| 86 | + OptionMarker, |
| 87 | + }, |
| 88 | + }, |
| 89 | +} |
| 90 | + |
| 91 | +// function for FormatHelper interface |
| 92 | +func (lc *LcbCommand) formatHelpForWhole() string { |
| 93 | + return lc.command.formatHelpForWhole() |
| 94 | +} |
| 95 | + |
| 96 | +func (lc *LcbCommand) formatIndependHelp() string { |
| 97 | + return lc.command.formatIndependHelp() |
| 98 | +} |
| 99 | + |
| 100 | +// Init simulate inheritance, and polymorphism |
| 101 | +func (lc *LcbCommand) Init(args []string, options OptionMapType) error { |
| 102 | + return lc.command.Init(args, options, lc) |
| 103 | +} |
| 104 | + |
| 105 | +// RunCommand simulate inheritance, and polymorphism |
| 106 | +func (lc *LcbCommand) RunCommand() error { |
| 107 | + prefix := "" |
| 108 | + if len(lc.command.args) > 0 { |
| 109 | + cloudURL, err := CloudURLFromString(lc.command.args[0], "") |
| 110 | + if err != nil { |
| 111 | + return err |
| 112 | + } |
| 113 | + prefix = cloudURL.bucket |
| 114 | + } |
| 115 | + |
| 116 | + limitedNum, _ := GetInt(OptionLimitedNum, lc.command.options) |
| 117 | + vmarker, _ := GetString(OptionMarker, lc.command.options) |
| 118 | + if vmarker, err := lc.command.getRawMarker(vmarker); err != nil { |
| 119 | + return fmt.Errorf("invalid marker: %s, marker is not url encoded, %s", vmarker, err.Error()) |
| 120 | + } |
| 121 | + |
| 122 | + var num int64 |
| 123 | + num = 0 |
| 124 | + |
| 125 | + client, err := lc.command.ossClient("") |
| 126 | + if err != nil { |
| 127 | + return err |
| 128 | + } |
| 129 | + |
| 130 | + // list all cloudbox |
| 131 | + pre := oss.Prefix(prefix) |
| 132 | + marker := oss.Marker(vmarker) |
| 133 | + for limitedNum < 0 || num < limitedNum { |
| 134 | + lcr, err := lc.ossListCloudBoxesRetry(client, pre, marker) |
| 135 | + if err != nil { |
| 136 | + return err |
| 137 | + } |
| 138 | + pre = oss.Prefix(lcr.Prefix) |
| 139 | + marker = oss.Marker(lcr.NextMarker) |
| 140 | + if num == 0 && len(lcr.CloudBoxes) > 0 { |
| 141 | + fmt.Printf("%-30s %20s%s%12s%s%s\n", "ID", "Name", "Owner", "Region", "ControlEndpoint", "DataEndpoint") |
| 142 | + } |
| 143 | + for _, box := range lcr.CloudBoxes { |
| 144 | + if limitedNum >= 0 && num >= limitedNum { |
| 145 | + break |
| 146 | + } |
| 147 | + fmt.Printf("%-30s %20s%s%12s%s%s\n", box.Id, box.Name, box.Owner, box.Region, box.ControlEndpoint, box.DataEndpoint) |
| 148 | + num++ |
| 149 | + } |
| 150 | + if !lcr.IsTruncated { |
| 151 | + break |
| 152 | + } |
| 153 | + } |
| 154 | + return nil |
| 155 | +} |
| 156 | + |
| 157 | +func (lc *LcbCommand) ossListCloudBoxesRetry(client *oss.Client, options ...oss.Option) (oss.ListCloudBoxResult, error) { |
| 158 | + retryTimes, _ := GetInt(OptionRetryTimes, lc.command.options) |
| 159 | + for i := 1; ; i++ { |
| 160 | + lbr, err := client.ListCloudBoxes(options...) |
| 161 | + if err == nil || int64(i) >= retryTimes { |
| 162 | + return lbr, err |
| 163 | + } |
| 164 | + } |
| 165 | +} |
0 commit comments