|
| 1 | +/* |
| 2 | + * Copyright (c) 2022 NetEase Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +/* |
| 18 | + * Project: CurveCli |
| 19 | + * Created Date: 2023-12-04 |
| 20 | + * Author: ZackSoul |
| 21 | + */ |
| 22 | +package snapshot |
| 23 | + |
| 24 | +import ( |
| 25 | + "strconv" |
| 26 | + "time" |
| 27 | + |
| 28 | + cmderror "github.com/opencurve/curve/tools-v2/internal/error" |
| 29 | + cobrautil "github.com/opencurve/curve/tools-v2/internal/utils" |
| 30 | + snapshotutil "github.com/opencurve/curve/tools-v2/internal/utils/snapshot" |
| 31 | + basecmd "github.com/opencurve/curve/tools-v2/pkg/cli/command" |
| 32 | + listSnapshot "github.com/opencurve/curve/tools-v2/pkg/cli/command/curvebs/list/snapshot" |
| 33 | + "github.com/opencurve/curve/tools-v2/pkg/config" |
| 34 | + "github.com/opencurve/curve/tools-v2/pkg/output" |
| 35 | + "github.com/spf13/cobra" |
| 36 | +) |
| 37 | + |
| 38 | +const ( |
| 39 | + snapshotExample = `$ curve bs query snapshot --path /test/test111 --snapshotstatus done |
| 40 | +(other status: "in-progress" "deleting" "errorDeleteing" "canceling" "error")` |
| 41 | +) |
| 42 | + |
| 43 | +type SnapShotCommand struct { |
| 44 | + basecmd.FinalCurveCmd |
| 45 | + snapshotAddrs []string |
| 46 | + timeout time.Duration |
| 47 | + |
| 48 | + user string |
| 49 | + file string |
| 50 | + uuid string |
| 51 | + status string |
| 52 | +} |
| 53 | + |
| 54 | +var _ basecmd.FinalCurveCmdFunc = (*SnapShotCommand)(nil) |
| 55 | + |
| 56 | +func NewQuerySnapshotCommand() *SnapShotCommand { |
| 57 | + sCmd := &SnapShotCommand{ |
| 58 | + FinalCurveCmd: basecmd.FinalCurveCmd{ |
| 59 | + Use: "snapshot", |
| 60 | + Short: "query snapshot in curve bs", |
| 61 | + Example: snapshotExample, |
| 62 | + }, |
| 63 | + } |
| 64 | + |
| 65 | + basecmd.NewFinalCurveCli(&sCmd.FinalCurveCmd, sCmd) |
| 66 | + return sCmd |
| 67 | +} |
| 68 | + |
| 69 | +func NewSnapshotCommand() *cobra.Command { |
| 70 | + return NewQuerySnapshotCommand().Cmd |
| 71 | +} |
| 72 | + |
| 73 | +func (sCmd *SnapShotCommand) AddFlags() { |
| 74 | + config.AddBsSnapshotCloneFlagOption(sCmd.Cmd) |
| 75 | + config.AddHttpTimeoutFlag(sCmd.Cmd) |
| 76 | + config.AddBsUserOptionFlag(sCmd.Cmd) |
| 77 | + config.AddBsSnapshotIDOptionFlag(sCmd.Cmd) |
| 78 | + config.AddBsPathOptionFlag(sCmd.Cmd) |
| 79 | + config.AddSnapshotStatusOptionalFlag(sCmd.Cmd) |
| 80 | +} |
| 81 | + |
| 82 | +func (sCmd *SnapShotCommand) Init(cmd *cobra.Command, args []string) error { |
| 83 | + snapshotAddrs, err := config.GetBsSnapshotAddrSlice(sCmd.Cmd) |
| 84 | + if err.TypeCode() != cmderror.CODE_SUCCESS { |
| 85 | + sCmd.Error = err |
| 86 | + return err.ToError() |
| 87 | + } |
| 88 | + sCmd.snapshotAddrs = snapshotAddrs |
| 89 | + sCmd.timeout = config.GetFlagDuration(sCmd.Cmd, config.HTTPTIMEOUT) |
| 90 | + sCmd.user = config.GetBsFlagString(sCmd.Cmd, config.CURVEBS_USER) |
| 91 | + sCmd.file = config.GetBsFlagString(sCmd.Cmd, config.CURVEBS_PATH) |
| 92 | + sCmd.uuid = config.GetBsFlagString(sCmd.Cmd, config.CURVEBS_SNAPSHOT_ID) |
| 93 | + sCmd.status = config.GetBsFlagString(sCmd.Cmd, config.CURVEBS_SNAPSHOT_STATUS) |
| 94 | + header := []string{ |
| 95 | + cobrautil.ROW_SNAPSHOT_ID, |
| 96 | + cobrautil.ROW_SNAPSHOT_NAME, |
| 97 | + cobrautil.ROW_USER, |
| 98 | + cobrautil.ROW_STATUS, |
| 99 | + cobrautil.ROW_SNAPSHOT_SEQNUM, |
| 100 | + cobrautil.ROW_FILE_LENGTH, |
| 101 | + cobrautil.ROW_PROGRESS, |
| 102 | + cobrautil.ROW_CREATE_TIME, |
| 103 | + cobrautil.ROW_FILE, |
| 104 | + } |
| 105 | + sCmd.SetHeader(header) |
| 106 | + sCmd.TableNew.SetAutoMergeCellsByColumnIndex(cobrautil.GetIndexSlice( |
| 107 | + sCmd.Header, []string{cobrautil.ROW_FILE}, |
| 108 | + )) |
| 109 | + return nil |
| 110 | +} |
| 111 | + |
| 112 | +func (sCmd *SnapShotCommand) Print(cmd *cobra.Command, args []string) error { |
| 113 | + return output.FinalCmdOutput(&sCmd.FinalCurveCmd, sCmd) |
| 114 | +} |
| 115 | + |
| 116 | +func (sCmd *SnapShotCommand) RunCommand(cmd *cobra.Command, args []string) error { |
| 117 | + params := map[string]any{ |
| 118 | + snapshotutil.QueryAction: snapshotutil.ActionGetFileSnapshotList, |
| 119 | + snapshotutil.QueryVersion: snapshotutil.Version, |
| 120 | + snapshotutil.QueryUser: sCmd.user, |
| 121 | + snapshotutil.QueryFile: sCmd.file, |
| 122 | + snapshotutil.QueryLimit: snapshotutil.Limit, |
| 123 | + snapshotutil.QueryOffset: snapshotutil.Offset, |
| 124 | + } |
| 125 | + if sCmd.uuid != snapshotutil.DefaultSnapID { |
| 126 | + params[snapshotutil.QueryUUID] = sCmd.uuid |
| 127 | + } |
| 128 | + for i, status := range snapshotutil.SnapshotStatus { |
| 129 | + if status == sCmd.status { |
| 130 | + params[snapshotutil.QueryStatus] = i |
| 131 | + } |
| 132 | + } |
| 133 | + snapshotsInfo, err := listSnapshot.ListSnapShot(sCmd.snapshotAddrs, sCmd.timeout, params) |
| 134 | + if err != nil { |
| 135 | + sCmd.Error = err |
| 136 | + return sCmd.Error.ToError() |
| 137 | + } |
| 138 | + rows := make([]map[string]string, 0) |
| 139 | + if len(snapshotsInfo) == 0 { |
| 140 | + rows = append(rows, listSnapshot.EmptyOutput()) |
| 141 | + } |
| 142 | + for _, item := range snapshotsInfo { |
| 143 | + row := make(map[string]string) |
| 144 | + row[cobrautil.ROW_SNAPSHOT_ID] = item.UUID |
| 145 | + row[cobrautil.ROW_SNAPSHOT_NAME] = item.Name |
| 146 | + row[cobrautil.ROW_USER] = item.User |
| 147 | + row[cobrautil.ROW_FILE] = item.File |
| 148 | + row[cobrautil.ROW_STATUS] = strconv.Itoa(item.Status) |
| 149 | + row[cobrautil.ROW_SNAPSHOT_SEQNUM] = strconv.Itoa(item.SeqNum) |
| 150 | + row[cobrautil.ROW_FILE_LENGTH] = strconv.Itoa(item.FileLength) |
| 151 | + row[cobrautil.ROW_PROGRESS] = strconv.FormatFloat(item.Progress, 'f', 2, 64) |
| 152 | + row[cobrautil.ROW_CREATE_TIME] = time.Unix(int64(item.Time/1000000), 0).Format("2006-01-02 15:04:05") |
| 153 | + rows = append(rows, row) |
| 154 | + } |
| 155 | + list := cobrautil.ListMap2ListSortByKeys(rows, sCmd.Header, []string{cobrautil.ROW_SNAPSHOT_NAME, cobrautil.ROW_SNAPSHOT_ID}) |
| 156 | + sCmd.TableNew.AppendBulk(list) |
| 157 | + sCmd.Result = rows |
| 158 | + sCmd.Error = cmderror.Success() |
| 159 | + return nil |
| 160 | +} |
| 161 | + |
| 162 | +func (sCmd *SnapShotCommand) ResultPlainOutput() error { |
| 163 | + return output.FinalCmdOutputPlain(&sCmd.FinalCurveCmd) |
| 164 | +} |
0 commit comments