-
Notifications
You must be signed in to change notification settings - Fork 201
/
Copy pathmain.go
145 lines (127 loc) · 3.89 KB
/
main.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
// Copyright (c) 2009-present, Alibaba Cloud All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"embed"
"encoding/json"
"fmt"
"os"
"path"
"strings"
aliyunopenapimeta "github.com/aliyun/aliyun-cli/aliyun-openapi-meta"
"github.com/aliyun/aliyun-cli/cli"
"github.com/aliyun/aliyun-cli/config"
"github.com/aliyun/aliyun-cli/i18n"
"github.com/aliyun/aliyun-cli/openapi"
"github.com/aliyun/aliyun-cli/oss/lib"
)
func Main(args []string) {
stdout := cli.DefaultStdoutWriter()
stderr := cli.DefaultStderrWriter()
// load current configuration
profile, err := config.LoadCurrentProfile()
if err != nil {
cli.Errorf(stderr, "ERROR: load current configuration failed %s", err)
return
}
// set language with current profile
i18n.SetLanguage(profile.Language)
// create root command
rootCmd := &cli.Command{
Name: "aliyun",
Short: i18n.T("Alibaba Cloud Command Line Interface Version "+cli.Version, "阿里云CLI命令行工具 "+cli.Version),
Usage: "aliyun <product> <operation> [--parameter1 value1 --parameter2 value2 ...]",
Sample: "aliyun ecs DescribeRegions",
EnableUnknownFlag: true,
}
// add default flags
config.AddFlags(rootCmd.Flags())
openapi.AddFlags(rootCmd.Flags())
// new open api commando to process rootCmd
commando := openapi.NewCommando(stdout, profile)
commando.InitWithCommand(rootCmd)
ctx := cli.NewCommandContext(stdout, stderr)
ctx.EnterCommand(rootCmd)
ctx.SetCompletion(cli.ParseCompletionForShell())
rootCmd.AddSubCommand(config.NewConfigureCommand())
rootCmd.AddSubCommand(lib.NewOssCommand())
rootCmd.AddSubCommand(cli.NewVersionCommand())
rootCmd.AddSubCommand(cli.NewAutoCompleteCommand())
if os.Getenv("GENERATE_METADATA") == "YES" {
generateMetadata(rootCmd)
} else {
rootCmd.Execute(ctx, args)
}
}
func main() {
Main(os.Args[1:])
}
func dumpFiles(fs embed.FS, filePath string, outputDir string) {
filePath = strings.TrimPrefix(filePath, "./")
entries, err := fs.ReadDir(filePath)
if err != nil {
fmt.Println(err.Error())
return
}
for _, entry := range entries {
entryPath := path.Join(filePath, entry.Name())
if entry.IsDir() {
dumpFiles(fs, entryPath, outputDir)
} else {
content, err := fs.ReadFile(entryPath)
if err != nil {
fmt.Println(err.Error())
return
}
targetPath := path.Join(outputDir, entryPath)
fmt.Println("copy file from " + entryPath + " to " + targetPath)
_, err = os.Stat(path.Dir(targetPath))
if os.IsNotExist(err) {
err = os.MkdirAll(path.Dir(targetPath), 0755)
if err != nil {
fmt.Println(err.Error())
return
}
}
err = os.WriteFile(targetPath, content, 0666)
if err != nil {
fmt.Println(err.Error())
return
}
}
}
}
func generateMetadata(rootCmd *cli.Command) {
metadata := make(map[string]*cli.Metadata)
rootCmd.GetMetadata(metadata)
b, _ := json.MarshalIndent(metadata, "", " ")
cwd, _ := os.Getwd()
targetDir := cwd + "/cli-metadata"
_, err := os.Stat(targetDir)
if os.IsNotExist(err) {
err := os.Mkdir(targetDir, 0755)
if err != nil {
fmt.Println(err.Error())
return
}
}
targetPath := targetDir + "/commands.json"
err = os.WriteFile(targetPath, b, 0666)
if err != nil {
fmt.Println(err.Error())
}
versionPath := targetDir + "/version"
os.WriteFile(versionPath, []byte(cli.Version), 0666)
dumpFiles(aliyunopenapimeta.Metadatas, ".", targetDir)
}