forked from aliyun/aliyun-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
aliyunCliMain.py
executable file
·132 lines (111 loc) · 5.67 KB
/
aliyunCliMain.py
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
'''
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you 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.
'''
import aliyunCliParser
import aliyunOpenApiData
import aliyunExtensionCliHandler
import response
import aliyunCliHelp
import aliyunCliUpgrade
import aliyunCompleter
import sys
class AliyunCommandLine:
def __init__(self):
self.parser = aliyunCliParser.aliyunCliParser()
self.handler = aliyunOpenApiData.aliyunOpenApiDataHandler()
self.extensionHandler = aliyunExtensionCliHandler.aliyunExtensionCliHandler()
self.helper = aliyunCliHelp.aliyunCliHelper()
self.upgradeHandler = aliyunCliUpgrade.aliyunCliUpgradeHandler()
self.args = sys.argv[1:]
self.completer = aliyunCompleter.Completer()
def main(self):
# fetch command
cmd = self.parser.getCliCmd()
extensionCmdList = self.extensionHandler.getAllExtensionCommands()
if cmd in extensionCmdList:
self.handlerExtensionCmd(cmd)
return
# fetch operation
operation = self.parser.getCliOperation()
# fetch paramlist
keyValues = self.parser._getKeyValues()
outPutFormat = self.parser.getOutPutFormat(keyValues)
if outPutFormat is None or len(outPutFormat) == 0:
outPutFormat = self.extensionHandler.getUserFormat()
if outPutFormat is None or outPutFormat == "":
outPutFormat = 'json'
else:
outPutFormat = outPutFormat[0]
if self.handler.isAvailableCmd(cmd):
# fetch getversion
version=self.handler.getSdkVersion(cmd,keyValues)
if version is None:
return
# here handler the openapi cmd
if self.handler.isAvailableOperation(cmd, operation,version): # cmd and operation both are right
instanceAndClassName=self.handler.getInstanceByCmdOperation(cmd, operation,version)
if instanceAndClassName is not None and len(instanceAndClassName)==2:
cmdInstance = instanceAndClassName[0]
className = instanceAndClassName[1]
if cmdInstance is not None and className is not None:
if self.showInstanceAttribute(cmd, operation, cmdInstance):
return
# here should handle the keyValues first
keyValues = self.parser.getOpenApiKeyValues(keyValues)
if self.handler.needSetDefaultRegion(cmdInstance, keyValues):
keyValues["RegionId"] = [self.extensionHandler.getUserRegion()]
#check necessaryArgs as:accesskeyid accesskeysecret regionId
if not self.handler.hasNecessaryArgs(keyValues):
print 'accesskeyid/accesskeysecret/regionId is absence'
return
try:
result = self.handler.getResponse(cmd,operation,className,cmdInstance,keyValues)
if result is None:
return
if("Code" in result):
response.display_response("error", result, "json")
# print("failed")
# print(result["Code"])
# print(result["Message"])
# print("Please check your parameters first.")
else:
#print(result)
response.display_response(operation, result, outPutFormat,keyValues)
except Exception as e:
print(e)
else:
print 'aliyuncli internal error, please contact: xixi.xxx'
elif self.handler.isAvailableExtensionOperation(cmd, operation):
if self.args.__len__() >= 3 and self.args[2] == 'help':
import commandConfigure
configure = commandConfigure.commandConfigure()
configure.showExtensionOperationHelp(cmd, operation)
else:
self.extensionHandler.handlerExtensionOperation(cmd,operation,version)
# self.extensionHandler.handlerExtensionOperation(cmd,operation,version)
else:
# cmd is right but operation is not right
self.helper.showOperationError(cmd, operation)
else:
self.helper.showCmdError(cmd)
def handlerExtensionCmd(self, cmd):
self.extensionHandler.handlerExtensionCmd(cmd)
def showInstanceAttribute(self, cmd, operation, cmdInstance):
if self.args.__len__() >= 3 and self.args[2] == "help":
self.helper.showParameterError(cmd, operation, self.completer._help_to_show_instance_attribute(cmdInstance))
#print self.completer._help_to_show_instance_attribute(cmdInstance)
return True
return False