/
cli_bridge.go
157 lines (136 loc) · 3.83 KB
/
cli_bridge.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package lib
import (
"fmt"
"github.com/aliyun/aliyun-cli/cli"
"github.com/aliyun/aliyun-cli/config"
"github.com/aliyun/aliyun-cli/i18n"
"os"
"strings"
"time"
)
func NewOssCommand() *cli.Command {
result := &cli.Command{
Name: "oss",
Usage: "oss [command] [args...] [options...]",
Hidden: false,
Short: i18n.T("Object Storage Service", "阿里云OSS对象存储"),
}
result.AddSubCommand(NewCommandBridge(&makeBucketCommand))
result.AddSubCommand(NewCommandBridge(&listCommand))
result.AddSubCommand(NewCommandBridge(&removeCommand))
result.AddSubCommand(NewCommandBridge(&statCommand))
result.AddSubCommand(NewCommandBridge(&setACLCommand))
result.AddSubCommand(NewCommandBridge(&setMetaCommand))
result.AddSubCommand(NewCommandBridge(©Command))
result.AddSubCommand(NewCommandBridge(&restoreCommand))
result.AddSubCommand(NewCommandBridge(&createSymlinkCommand))
result.AddSubCommand(NewCommandBridge(&readSymlinkCommand))
result.AddSubCommand(NewCommandBridge(&signURLCommand))
result.AddSubCommand(NewCommandBridge(&hashCommand))
result.AddSubCommand(NewCommandBridge(&helpCommand))
return result
}
func NewCommandBridge(a Commander) *cli.Command {
cmd := a.GetCommand()
result := &cli.Command{
Name: cmd.name,
Usage: cmd.specEnglish.syntaxText,
Short: i18n.T(cmd.specEnglish.synopsisText, cmd.specChinese.synopsisText),
Long: i18n.T(cmd.specEnglish.detailHelpText, cmd.specChinese.detailHelpText),
Run: func(ctx *cli.Context, args []string) error {
return ParseAndRunCommandFromCli(ctx, args)
},
}
config.AddFlags(result.Flags())
for _, s := range cmd.validOptionNames {
opt, ok := OptionMap[s]
if !ok {
// fmt.Printf("INIT ERROR: unknown oss options: %s\n", s)
break
}
name := opt.nameAlias[2:]
shorthand := rune(0)
if len(opt.name) > 0 {
shorthand = rune(opt.name[1])
}
if result.Flags().Get(name) == nil {
result.Flags().Add(&cli.Flag{
Name: name,
Shorthand: shorthand,
Short: i18n.T(opt.helpEnglish, opt.helpChinese),
// Assignable: opt.optionType todo
})
}
}
return result
}
func ParseAndRunCommandFromCli(ctx *cli.Context, args []string) error {
profile, err := config.LoadProfileWithContext(ctx)
if err != nil {
return fmt.Errorf("config failed: %s", err.Error())
}
sc, err := profile.GetSessionCredential()
if err != nil {
return fmt.Errorf("can't get credential %s", err)
}
configs := make(map[string]string, 0)
configs["access-key-id"] = sc.AccessKeyId
configs["access-key-secret"] = sc.AccessKeySecret
configs["sts-token"] = sc.StsToken
if ep, ok := ctx.Flags().GetValue("endpoint"); !ok {
configs["endpoint"] = "oss-" + profile.RegionId + ".aliyuncs.com"
} else {
configs["endpoint"] = ep
}
//if i18n.GetLanguage() == "zh" {
// configs[OptionLanguage] = "CH"
//} else {
// configs[OptionLanguage] = "EN"
//}
ts := time.Now().UnixNano()
commandLine = strings.Join(os.Args[1:], " ")
// os.Args = []string {"aliyun", "oss", "ls"}
clearEnv()
a2 := []string{"aliyun", "oss"}
a2 = append(a2, ctx.Command().Name)
for _, a := range args {
a2 = append(a2, a)
}
configFlagSet := cli.NewFlagSet()
config.AddFlags(configFlagSet)
for _, f := range ctx.Flags().Flags() {
if configFlagSet.Get(f.Name) != nil {
continue
}
if f.IsAssigned() {
a2 = append(a2, "--"+f.Name)
if s2, ok := f.GetValue(); ok && s2 != "" {
a2 = append(a2, s2)
}
}
}
for k, v := range configs {
if v != "" {
a2 = append(a2, "--"+k)
a2 = append(a2, v)
}
}
os.Args = a2
// cli.Noticef("%v", os.Args)
args, options, err := ParseArgOptions()
if err != nil {
return err
}
args = args[1:]
// fmt.Printf("%v", args)
showElapse, err := RunCommand(args, options)
if err != nil {
return err
}
if showElapse {
te := time.Now().UnixNano()
fmt.Printf("%.6f(s) elapsed\n", float64(te-ts)/1e9)
return nil
}
return nil
}