-
Notifications
You must be signed in to change notification settings - Fork 200
/
workspace.go
111 lines (95 loc) · 2.26 KB
/
workspace.go
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package workspace
import (
"context"
"github.com/qiniu/go-sdk/v7/auth/qbox"
"github.com/qiniu/go-sdk/v7/storage"
"github.com/qiniu/qshell/v2/iqshell/common/account"
"github.com/qiniu/qshell/v2/iqshell/common/config"
"github.com/qiniu/qshell/v2/iqshell/common/data"
"github.com/qiniu/qshell/v2/iqshell/common/log"
"sync"
)
const (
workspaceName = ".qshell"
usersDirName = "users"
defaultUserDirName = ".unknown"
usersDBName = "account.db"
currentUserFileName = "account.json"
oldUserFileName = "old_account.json"
usersWorkspaceDirName = "workspace"
taskDirName = "task"
taskDBName = "task.db"
configFileName = ".qshell.json"
)
var (
// config 配置信息
cfg = &config.Config{}
// 当前账户
currentAccount *account.Account
lock sync.Mutex
cancelCtx context.Context
cancelFunc func()
)
// GetConfig 获取之前需要先 Load
func GetConfig() *config.Config {
return cfg
}
func GetLogConfig() *config.LogSetting {
if cfg == nil || cfg.Log == nil {
return nil
}
return cfg.Log
}
func GetStorageConfig() *storage.Config {
r := cfg.GetRegion()
ucHost := cfg.Hosts.GetOneUc()
if len(ucHost) > 0 {
log.DebugF("ucHost: %s", ucHost)
storage.SetUcHost(ucHost, cfg.IsUseHttps())
}
return &storage.Config{
UseHTTPS: cfg.IsUseHttps(),
Region: r,
Zone: r,
CentralRsHost: cfg.Hosts.GetOneRs(),
}
}
func GetAccount() (account.Account, *data.CodeError) {
if currentAccount == nil {
return account.Account{}, data.NewEmptyError().AppendDesc("can't get current user")
}
return *currentAccount, nil
}
func GetUserName() string {
if currentAccount == nil {
return ""
}
if len(currentAccount.Name) > 0 {
return currentAccount.Name
}
return currentAccount.AccessKey
}
func GetMac() (mac *qbox.Mac, err *data.CodeError) {
acc, gErr := GetAccount()
if gErr != nil {
err = gErr
return
}
mac = qbox.NewMac(acc.AccessKey, acc.SecretKey)
return
}
// GetContext 统一使用一个 context
func GetContext() context.Context {
locker.Lock()
defer locker.Unlock()
if cancelCtx != nil {
return cancelCtx
}
cancelCtx, cancelFunc = context.WithCancel(context.Background())
return cancelCtx
}
func Cancel() {
if cancelFunc != nil {
cancelFunc()
}
}