@@ -52,13 +52,72 @@ func (c *Commando) InitWithCommand(cmd *cli.Command) {
5252	cmd .AutoComplete  =  c .complete 
5353}
5454
55+ func  DetectInConfigureMode (flags  * cli.FlagSet ) bool  {
56+ 	_ , modeExist  :=  flags .GetValue (config .ModeFlagName )
57+ 	if  ! modeExist  {
58+ 		return  true 
59+ 	}
60+ 	// if mode exist, check if other flags exist 
61+ 	_ , akExist  :=  flags .GetValue (config .AccessKeyIdFlagName )
62+ 	if  akExist  {
63+ 		return  true 
64+ 	}
65+ 	_ , skExist  :=  flags .GetValue (config .AccessKeySecretFlagName )
66+ 	if  skExist  {
67+ 		return  true 
68+ 	}
69+ 	_ , stsExist  :=  flags .GetValue (config .StsTokenFlagName )
70+ 	if  stsExist  {
71+ 		return  true 
72+ 	}
73+ 	// RamRoleNameFlagName 
74+ 	_ , ramRoleNameExist  :=  flags .GetValue (config .RamRoleNameFlagName )
75+ 	if  ramRoleNameExist  {
76+ 		return  true 
77+ 	}
78+ 	// RamRoleArnFlagName 
79+ 	_ , ramRoleArnExist  :=  flags .GetValue (config .RamRoleArnFlagName )
80+ 	if  ramRoleArnExist  {
81+ 		return  true 
82+ 	}
83+ 	// RoleSessionNameFlagName 
84+ 	_ , roleSessionNameExist  :=  flags .GetValue (config .RoleSessionNameFlagName )
85+ 	if  roleSessionNameExist  {
86+ 		return  true 
87+ 	}
88+ 	// PrivateKeyFlagName 
89+ 	_ , privateKeyExist  :=  flags .GetValue (config .PrivateKeyFlagName )
90+ 	if  privateKeyExist  {
91+ 		return  true 
92+ 	}
93+ 	// KeyPairNameFlagName 
94+ 	_ , keyPairNameExist  :=  flags .GetValue (config .KeyPairNameFlagName )
95+ 	if  keyPairNameExist  {
96+ 		return  true 
97+ 	}
98+ 	// OIDCProviderARNFlagName 
99+ 	_ , oidcProviderArnExist  :=  flags .GetValue (config .OIDCProviderARNFlagName )
100+ 	if  oidcProviderArnExist  {
101+ 		return  true 
102+ 	}
103+ 	// OIDCTokenFileFlagName 
104+ 	_ , oidcTokenFileExist  :=  flags .GetValue (config .OIDCTokenFileFlagName )
105+ 	if  oidcTokenFileExist  {
106+ 		return  true 
107+ 	}
108+ 	return  false 
109+ }
110+ 
55111func  (c  * Commando ) main (ctx  * cli.Context , args  []string ) error  {
56112	// aliyun 
57113	if  len (args ) ==  0  {
58114		c .printUsage (ctx )
59115		return  nil 
60116	}
61117
118+ 	// detect if in configure mode 
119+ 	ctx .SetInConfigureMode (DetectInConfigureMode (ctx .Flags ()))
120+ 
62121	// update current `Profile` with flags 
63122	var  err  error 
64123	c .profile , err  =  config .LoadProfileWithContext (ctx )
@@ -96,14 +155,27 @@ func (c *Commando) main(ctx *cli.Context, args []string) error {
96155		}
97156		if  product .ApiStyle  ==  "restful"  {
98157			api , _  :=  c .library .GetApi (product .Code , product .Version , args [1 ])
158+ 			c .CheckApiParamWithBuildInArgs (ctx , api )
99159			ctx .Command ().Name  =  args [1 ]
100160			return  c .processInvoke (ctx , productName , api .Method , api .PathPattern )
161+ 		} else  {
162+ 			// RPC need check API parameters too 
163+ 			api , _  :=  c .library .GetApi (product .Code , product .Version , args [1 ])
164+ 			c .CheckApiParamWithBuildInArgs (ctx , api )
101165		}
102166
103167		return  c .processInvoke (ctx , productName , args [1 ], "" )
104168	} else  if  len (args ) ==  3  {
105169		// restful call 
106170		// aliyun <productCode> {GET|PUT|POST|DELETE} <path> -- 
171+ 		product , _  :=  c .library .GetProduct (productName )
172+ 		api , find  :=  c .library .GetApiByPath (product .Code , product .Version , args [1 ], args [2 ])
173+ 		if  ! find  {
174+ 			// throw error, can not find api by path 
175+ 			return  cli .NewErrorWithTip (fmt .Errorf ("can not find api by path %s" , args [2 ]),
176+ 				"Please confirm if the API path exists" )
177+ 		}
178+ 		c .CheckApiParamWithBuildInArgs (ctx , api )
107179		return  c .processInvoke (ctx , productName , args [1 ], args [2 ])
108180	} else  {
109181		return  cli .NewErrorWithTip (fmt .Errorf ("too many arguments" ),
@@ -418,3 +490,20 @@ func (c *Commando) printUsage(ctx *cli.Context) {
418490	cmd .PrintSample (ctx )
419491	cmd .PrintTail (ctx )
420492}
493+ 
494+ func  (c  * Commando ) CheckApiParamWithBuildInArgs (ctx  * cli.Context , api  meta.Api ) {
495+ 	for  _ , p  :=  range  api .Parameters  {
496+ 		// 如果参数中包含了known参数,且 known参数已经被赋值,则将 known 参数拷贝给 unknown 参数 
497+ 		if  ep , ok  :=  ctx .Flags ().GetValue (p .Name ); ok  {
498+ 			if  p .Position  !=  "Query"  {
499+ 				continue 
500+ 			}
501+ 			var  flagNew  =  & cli.Flag {
502+ 				Name : p .Name ,
503+ 			}
504+ 			flagNew .SetValue (ep )
505+ 			flagNew .SetAssigned (true )
506+ 			ctx .UnknownFlags ().Add (flagNew )
507+ 		}
508+ 	}
509+ }
0 commit comments