Skip to content

Commit a2935dc

Browse files
authored
Merge pull request #352 from YangSen-qn/support_cmd_result
Support cmd result
2 parents 58ac386 + c50c053 commit a2935dc

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+396
-102
lines changed

cmd/root.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"github.com/qiniu/qshell/v2/iqshell"
66
"github.com/qiniu/qshell/v2/iqshell/common/config"
7+
"github.com/qiniu/qshell/v2/iqshell/common/data"
78
"github.com/qiniu/qshell/v2/iqshell/common/version"
89
"github.com/spf13/cobra"
910
"os"
@@ -74,7 +75,11 @@ func Execute() {
7475

7576
if err := rootCmd.Execute(); err != nil {
7677
_, _ = fmt.Fprintf(os.Stderr, "%v\n", err)
77-
os.Exit(1)
78+
data.SetCmdStatusError()
79+
}
80+
81+
if !data.IsTestMode() && data.GetCmdStatus() != data.StatusOK {
82+
os.Exit(data.GetCmdStatus())
7883
}
7984
}
8085

cmd/servers.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ var bucketsCmdBuilder = func(cfg *iqshell.Config) *cobra.Command {
1818
operations.List(cfg, info)
1919
},
2020
}
21-
cmd.Flags().StringVarP(&info.Region, "region", "", "", "region of bucket; z0, z1, z2, as0, na0")
21+
cmd.Flags().StringVarP(&info.Region, "region", "", "", "region of bucket; z0, z1, z2, as0, na0 etc")
2222
cmd.Flags().BoolVarP(&info.Detail, "detail", "", false, "print detail info for bucket")
2323
return cmd
2424
}

cmd_test/aa_pre_test.go

+3
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,13 @@ package cmd
55
import (
66
"fmt"
77
"github.com/qiniu/qshell/v2/cmd_test/test"
8+
"github.com/qiniu/qshell/v2/iqshell/common/data"
89
"testing"
910
)
1011

1112
func TestCmd(t *testing.T) {
13+
data.SetTestMode()
14+
1215
TestUser(t)
1316
ClearCache(t)
1417
}

cmd_test/test/config.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,6 @@ import (
77
)
88

99
var (
10-
Debug = true
11-
Local = len(os.Getenv("QSHELL_LOCAL")) > 0
12-
ShouldTestUser = len(os.Getenv("QSHELL_Test_User")) > 0
13-
1410
AccessKey = os.Getenv("accessKey")
1511
SecretKey = os.Getenv("secretKey")
1612
Bucket = testBucket()
@@ -19,6 +15,10 @@ var (
1915
DocumentOption = "--doc"
2016
)
2117

18+
func IsDebug() bool {
19+
return true
20+
}
21+
2222
func testBucket() string {
2323
if b := os.Getenv("bucket"); len(b) > 0 {
2424
return b

cmd_test/test/flow.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func (t *testFlow) Run() {
3636
fmt.Println("========== CMD Start:", t.args, "==========")
3737

3838
var err error
39-
if Debug {
39+
if IsDebug() {
4040
err = t.runByDebug()
4141
} else {
4242
err = t.runByCommand()

cmd_test/user.go

+2-4
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,11 @@ func TestUser(t *testing.T) {
1818
cmd.Execute()
1919
}()
2020

21-
if test.Local {
21+
if test.IsDebug() {
2222
return
2323
}
2424

25-
if test.ShouldTestUser {
26-
TestUserIntegration(t)
27-
}
25+
TestUserIntegration(t)
2826
}
2927

3028
func TestUserIntegration(t *testing.T) {

docs/account.md

+7
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@ qshell account [--overwrite | -w]<Your AccessKey> <Your SecretKey> <Your Account
2222
- SecretKey:七牛账号对应的 SecretKey [获取](https://portal.qiniu.com/user/key) 。【必选】
2323
- Name:AccessKey 和 SecretKey 对的 id, 可以任意取,但同一台机器此 id 不可重复;和在七牛注册的邮箱信息没有关系, 只是 qshell 本地用来标示 <ak, sk> 对。【必选】
2424

25+
### 注:
26+
如果你的参数里面含有 `-`,可以在命令中添加 `--` 以避免参数解析错误。
27+
```
28+
eg:
29+
qshell account -- -jhhjsjjj*** -ksnndn*** name
30+
```
31+
2532
# 选项
2633
-w/--overwrite: 强制覆盖已经存在的账户
2734

iqshell/ali/list_bucket.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -52,25 +52,29 @@ func ListBucket(cfg *iqshell.Config, info ListBucketInfo) {
5252
fp, err := os.Create(info.SaveToFile)
5353
if err != nil {
5454
log.Error("create file error:", err)
55+
data.SetCmdStatusError()
5556
return
5657
}
5758
defer func(fp *os.File) {
5859
err := fp.Close()
5960
if err != nil {
6061
log.Error("file close error:", err)
62+
data.SetCmdStatusError()
6163
}
6264
}(fp)
6365

6466
bw := bufio.NewWriter(fp)
6567
ossClient, err := oss.New(info.DataCenter, info.AccessKey, info.SecretKey)
6668
if err != nil {
6769
log.Error("create oss client error:", err)
70+
data.SetCmdStatusError()
6871
return
6972
}
7073

7174
ossBucket, err := ossClient.Bucket(info.Bucket)
7275
if err != nil {
7376
log.Error("create oss bucket error:", err)
77+
data.SetCmdStatusError()
7478
return
7579
}
7680

@@ -92,6 +96,7 @@ func ListBucket(cfg *iqshell.Config, info ListBucketInfo) {
9296
retryTimes += 1
9397
continue
9498
} else {
99+
data.SetCmdStatusError()
95100
break
96101
}
97102
} else {
@@ -115,7 +120,7 @@ func ListBucket(cfg *iqshell.Config, info ListBucketInfo) {
115120
fErr := bw.Flush()
116121
if fErr != nil {
117122
log.Error("Write data to buffer writer failed", fErr)
118-
err = fErr
123+
data.SetCmdStatusError()
119124
return
120125
}
121126

iqshell/aws/fetch.go

+8
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ func Fetch(cfg *iqshell.Config, info FetchInfo) {
7171

7272
if err != nil {
7373
log.ErrorF("get export error:%v", err)
74+
data.SetCmdStatusError()
7475
return
7576
}
7677

@@ -90,10 +91,12 @@ func Fetch(cfg *iqshell.Config, info FetchInfo) {
9091
}
9192
log.DebugF("get object:%s\t%d\t%s\t%s\n%s", *obj.Key, *obj.Size, *obj.ETag, *obj.LastModified, downloadUrl)
9293
} else {
94+
data.SetCmdStatusError()
9395
log.ErrorF("fetch([%s:%s]) create download url error: %v", info.AwsBucketInfo.Bucket, *obj.Key, e)
9496
}
9597
}); e != nil {
9698
log.Error(e)
99+
data.SetCmdStatusError()
97100
}
98101
close(fetchInfoChan)
99102
}()
@@ -198,6 +201,7 @@ func Fetch(cfg *iqshell.Config, info FetchInfo) {
198201
// 输出结果
199202
resultPath := filepath.Join(workspace.GetJobDir(), ".result")
200203
if e := utils.MarshalToFile(resultPath, metric); e != nil {
204+
data.SetCmdStatusError()
201205
log.ErrorF("save aws batch fetch result to path:%s error:%v", resultPath, e)
202206
} else {
203207
log.DebugF("save aws batch fetch result to path:%s", resultPath)
@@ -210,4 +214,8 @@ func Fetch(cfg *iqshell.Config, info FetchInfo) {
210214
log.InfoF("%20s%10d", "Skipped:", metric.SkippedCount)
211215
log.InfoF("%20s%10ds", "Duration:", metric.Duration)
212216
log.InfoF("--------------------------------------------")
217+
218+
if metric.FailureCount > 0 {
219+
data.SetCmdStatusError()
220+
}
213221
}

iqshell/aws/list_bucket.go

+1
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ func ListBucket(cfg *iqshell.Config, info ListBucketInfo) {
5959
log.AlertF("%s\t%d\t%s\t%s", *object.Key, *object.Size, *object.ETag, *object.LastModified)
6060
}); err != nil {
6161
log.Error(err)
62+
data.SetCmdStatusError()
6263
}
6364
}
6465

iqshell/cdn/operations/prefetch.go

+3
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ func Prefetch(cfg *iqshell.Config, info PrefetchInfo) {
4444
}))
4545
if err != nil {
4646
log.Error(err)
47+
data.SetCmdStatusError()
4748
return
4849
}
4950

@@ -53,6 +54,7 @@ func Prefetch(cfg *iqshell.Config, info PrefetchInfo) {
5354
for {
5455
hasMore, workInfo, pErr := workProvider.Provide()
5556
if pErr != nil {
57+
data.SetCmdStatusError()
5658
log.ErrorF("read work error:%v", pErr)
5759
continue
5860
}
@@ -84,6 +86,7 @@ func prefetchWithQps(urlsToPrefetch []string) {
8486
err := cdn.Prefetch(urlsToPrefetch)
8587
if err != nil {
8688
log.Error(err)
89+
data.SetCmdStatusError()
8790
}
8891
}
8992
}

iqshell/cdn/operations/refresh.go

+3
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ func Refresh(cfg *iqshell.Config, info RefreshInfo) {
4747
}))
4848
if err != nil {
4949
log.Error(err)
50+
data.SetCmdStatusError()
5051
return
5152
}
5253

@@ -56,6 +57,7 @@ func Refresh(cfg *iqshell.Config, info RefreshInfo) {
5657
for {
5758
hasMore, workInfo, pErr := workProvider.Provide()
5859
if pErr != nil {
60+
data.SetCmdStatusError()
5961
log.ErrorF("read work error:%v", pErr)
6062
continue
6163
}
@@ -97,6 +99,7 @@ func refreshWithQps(info RefreshInfo, items []string, force bool) (isRefresh boo
9799

98100
if err != nil {
99101
log.Error(err)
102+
data.SetCmdStatusError()
100103
}
101104
return
102105
}

iqshell/common/account/actions.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func SaveToDB(acc Account, accountOver bool) (err *data.CodeError) {
6262
ldb, lErr := leveldb.OpenFile(info.AccountDBPath, nil)
6363
if lErr != nil {
6464
err = data.NewEmptyError().AppendDescF("open db: %v", err)
65-
os.Exit(data.StatusHalt)
65+
return
6666
}
6767
defer ldb.Close()
6868

iqshell/common/account/operations/add.go

+2
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,13 @@ func Add(cfg *iqshell.Config, info AddInfo) {
4343
}
4444

4545
if err := account.SaveToDB(acc, info.Over); err != nil {
46+
data.SetCmdStatusError()
4647
log.ErrorF("user add: save user to db error:%v", err)
4748
return
4849
}
4950

5051
if err := account.SetAccountToLocalFile(acc); err != nil {
52+
data.SetCmdStatusError()
5153
log.ErrorF("user add: set current error:%v", err)
5254
return
5355
}

iqshell/common/account/operations/delete.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"github.com/qiniu/qshell/v2/iqshell/common/alert"
77
"github.com/qiniu/qshell/v2/iqshell/common/data"
88
"github.com/qiniu/qshell/v2/iqshell/common/log"
9-
"os"
109
)
1110

1211
type CleanInfo struct {
@@ -26,7 +25,7 @@ func Clean(cfg *iqshell.Config, info CleanInfo) {
2625
err := account.CleanUser()
2726
if err != nil {
2827
log.Error(err)
29-
os.Exit(data.StatusError)
28+
data.SetCmdStatusError()
3029
}
3130
}
3231

@@ -51,6 +50,7 @@ func Remove(cfg *iqshell.Config, info RemoveInfo) {
5150
err := account.RmUser(info.Name)
5251
if err != nil {
5352
log.Error(err)
54-
os.Exit(data.StatusError)
53+
data.SetCmdStatusError()
54+
return
5555
}
5656
}

iqshell/common/account/operations/query.go

+9-5
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import (
66
"github.com/qiniu/qshell/v2/iqshell/common/alert"
77
"github.com/qiniu/qshell/v2/iqshell/common/data"
88
"github.com/qiniu/qshell/v2/iqshell/common/log"
9-
"os"
109
)
1110

1211
type UserInfo struct {
@@ -40,14 +39,17 @@ func List(cfg *iqshell.Config, info ListInfo) {
4039
accounts, err := account.GetUsers()
4140
if err != nil {
4241
log.ErrorF("user list error:%v", err)
43-
os.Exit(data.StatusError)
42+
data.SetCmdStatusError()
4443
return
4544
}
4645

47-
for _, acc := range accounts {
46+
for index, acc := range accounts {
4847
if info.OnlyListName {
4948
log.AlertF(acc.Name)
5049
} else {
50+
if index > 0 {
51+
log.Alert(" ")
52+
}
5153
log.AlertF(acc.String())
5254
}
5355
}
@@ -64,7 +66,8 @@ func Current(cfg *iqshell.Config) {
6466
acc, err := account.GetAccount()
6567
if err != nil {
6668
log.ErrorF("user current error: %v", err)
67-
os.Exit(data.StatusError)
69+
data.SetCmdStatusError()
70+
return
6871
}
6972
log.AlertF(acc.String())
7073
}
@@ -91,7 +94,8 @@ func LookUp(cfg *iqshell.Config, info LookUpInfo) {
9194
accounts, err := account.LookUp(info.Name)
9295
if err != nil {
9396
log.ErrorF("user lookup error: %v", err)
94-
os.Exit(data.StatusError)
97+
data.SetCmdStatusError()
98+
return
9599
}
96100
for _, acc := range accounts {
97101
log.AlertF(acc.String())

iqshell/common/account/operations/update.go

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ func Change(cfg *iqshell.Config, info ChangeInfo) {
2525

2626
name, err := account.ChUser(info.Name)
2727
if err != nil {
28+
data.SetCmdStatusError()
2829
log.ErrorF("user change to %s failed, error:%v", name, err)
2930
return
3031
} else {

iqshell/common/data/define.go

-8
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,3 @@ const (
1212
BLOCK_BITS = 22 // Indicate that the blocksize is 4M
1313
BLOCK_SIZE = 1 << BLOCK_BITS // BLOCK SIZE
1414
)
15-
16-
// 此处需要明确出值
17-
const (
18-
StatusOK = 0 // process success
19-
StatusError = 1 // process error
20-
StatusUserCancel = 2 // 用户取消
21-
StatusHalt = 3 // local error
22-
)

iqshell/common/data/env.go

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package data
2+
3+
import "os"
4+
5+
const (
6+
EnvKeyTest = "qshell_test"
7+
)
8+
9+
func IsTestMode() bool {
10+
return os.Getenv(EnvKeyTest) == TrueString
11+
}
12+
13+
func SetTestMode() *CodeError {
14+
return ConvertError(os.Setenv(EnvKeyTest, TrueString))
15+
}

0 commit comments

Comments
 (0)