Skip to content

Commit 29afd72

Browse files
authored
support for reading the value of a parameter from a file (#199)
1 parent c92116c commit 29afd72

File tree

3 files changed

+44
-152
lines changed

3 files changed

+44
-152
lines changed

CHANGELOG.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,20 @@
22

33
### Master
44

5+
- new: support for reading the value of a parameter from a file
6+
>*Note: Only works on api parameters and rpc style*
7+
8+
>The value of some parameters is very long, which is not conducive to terminal input.
9+
>You can add `-FILE` to the original parameter to specify to read from the file,
10+
>followed by the file path.
11+
12+
>example:
13+
>command `aliyun ecs CreateInstance` has a parameter `--UserData`, You can specify the value of this parameter from a file by using `--UserData-FILE <filePath>`
14+
```shell
15+
aliyun ecs CreateInstance --UserData-FILE '/home/Document/user_data'
16+
```
17+
>*NOTE: The above example is not a complete command, just a demonstration of how new features are used.*
18+
519
### 3.0.19
620

721
- add product: oos, default version 2019-06-01

openapi/rpc.go

Lines changed: 15 additions & 152 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ package openapi
1515

1616
import (
1717
"fmt"
18+
"io/ioutil"
19+
"strings"
20+
1821
"github.com/aliyun/alibaba-cloud-sdk-go/sdk/responses"
1922
"github.com/aliyun/aliyun-cli/cli"
2023
"github.com/aliyun/aliyun-cli/meta"
@@ -42,8 +45,11 @@ func (a *RpcInvoker) Prepare(ctx *cli.Context) error {
4245

4346
// assign parameters
4447
for _, f := range ctx.UnknownFlags().Flags() {
48+
if strings.HasSuffix(f.Name, "-FILE") {
49+
f.Name = strings.TrimSuffix(f.Name, "-FILE")
50+
replaceValueWithFile(f)
51+
}
4552
param := api.FindParameter(f.Name)
46-
// cli.Printf("flag: %s %v\n", f.Name, param)
4753
if param == nil {
4854
return &InvalidParameterError{Name: f.Name, api: api, flags: ctx.Flags()}
4955
}
@@ -85,154 +91,11 @@ func (a *RpcInvoker) Call() (*responses.CommonResponse, error) {
8591
return resp, err
8692
}
8793

88-
// Deprecated Code
89-
//
90-
//func (c *Caller) InvokeRpc(ctx *cli.Context, product *meta.Product, apiName string) error {
91-
// api, ok := c.library.GetApi(product.Code, product.Version, apiName)
92-
// if !ok {
93-
// return &InvalidApiError{Name: apiName, product: product}
94-
// }
95-
//
96-
// //
97-
// // call OpenApi
98-
// // return: if check failed return error, otherwise return nil
99-
// client, request, err := c.InitClient(ctx, product, true)
100-
// if err != nil {
101-
// return err
102-
// }
103-
//
104-
// request.ApiName = apiName
105-
// err = c.FillRpcParameters(ctx, request, &api)
106-
// if err != nil {
107-
// return err
108-
// }
109-
//
110-
// request.Scheme = api.GetProtocol()
111-
// request.Method = api.GetMethod()
112-
//
113-
// err = c.UpdateRequest(ctx, request)
114-
// if err != nil {
115-
// return err
116-
// }
117-
//
118-
// if v, ok := PagerFlag.GetValue(); ok {
119-
// pager := GetPager(v)
120-
// r, err := c.InvokeRpcWithPager(client, request, pager)
121-
// if err != nil {
122-
// ctx.Command().PrintFailed(err, "")
123-
// } else {
124-
// filter, err := GetOutputFilter()
125-
// if err != nil {
126-
// ctx.Command().PrintFailed(err,"init output filter failed")
127-
// return nil
128-
// }
129-
//
130-
// if filter != nil {
131-
// r, err = filter.FilterOutput(r)
132-
// if err != nil {
133-
// ctx.Command().PrintFailed(err,"output filter process failed")
134-
// return nil
135-
// }
136-
// }
137-
// cli.Println(r)
138-
// if err != nil {
139-
// ctx.Command().PrintFailed(err, "")
140-
// }
141-
// return nil
142-
// }
143-
// }
144-
//
145-
// // waiter := NewWaiterWithCTX(ctx, client, request)
146-
//
147-
//
148-
// filter, err := GetOutputFilter()
149-
// if err != nil {
150-
// ctx.Command().PrintFailed(err,"init output filter failed")
151-
// return nil
152-
// }
153-
//
154-
// if filter != nil {
155-
// out, err = filter.FilterOutput(out)
156-
// if err != nil {
157-
// ctx.Command().PrintFailed(err,"output filter process failed")
158-
// return nil
159-
// }
160-
// }
161-
// cli.Println(out)
162-
//
163-
// //
164-
// return nil
165-
//}
166-
//
167-
//func (c *Caller) InvokeRpcForce(ctx *cli.Context, product *meta.Product, apiName string) {
168-
// //
169-
// // call OpenApi
170-
// // return: if check failed return error, otherwise return nil
171-
// client, request, err := c.InitClient(ctx, product, true)
172-
//
173-
// if err != nil {
174-
// ctx.Command().PrintFailed(fmt.Errorf("init client failed: %v", err), "")
175-
// return
176-
// }
177-
//
178-
//
179-
// err = c.UpdateRequest(ctx, request)
180-
// if err != nil {
181-
// ctx.Command().PrintFailed(err, "")
182-
// return
183-
// }
184-
//
185-
// resp, err := client.ProcessCommonRequest(request)
186-
// if err != nil {
187-
// ctx.Command().PrintFailed(err, "")
188-
// }
189-
//
190-
// // err = outputProcessor(ctx, resp.GetHttpContentString())
191-
// //if err != nil {
192-
// // ctx.Command().PrintFailed(err, "")
193-
// //}
194-
//
195-
// out := resp.GetHttpContentString()
196-
//
197-
// filter, err := GetOutputFilter()
198-
//
199-
// if err != nil {
200-
// ctx.Command().PrintFailed(err,"init output filter failed")
201-
// return
202-
// }
203-
//
204-
// if filter != nil {
205-
// out, err = filter.FilterOutput(out)
206-
// if err != nil {
207-
// ctx.Command().PrintFailed(err,"output filter process failed")
208-
// return
209-
// }
210-
// }
211-
// cli.Println(out)
212-
//}
213-
//
214-
//func (c *Caller) FillRpcParameters(ctx *cli.Context, request *requests.CommonRequest, api *meta.Api) error {
215-
//
216-
//}
217-
//
218-
//func (c *Caller) InvokeRpcWithPager(client *sdk.Client, request *requests.CommonRequest, pager *Pager) (string, error) {
219-
// for {
220-
// resp, err := client.ProcessCommonRequest(request)
221-
// if err != nil {
222-
// return "", fmt.Errorf("call failed %s", err)
223-
// }
224-
// //
225-
// // jmespath.Parser{}
226-
// err = pager.FeedResponse(resp.GetHttpContentBytes())
227-
// if err != nil {
228-
// return "", fmt.Errorf("call failed %s", err)
229-
// }
230-
//
231-
// if !pager.HasMore() {
232-
// break
233-
// }
234-
// pager.MoveNextPage(request)
235-
// }
236-
//
237-
// return pager.GetResponseCollection(), nil
238-
//}
94+
func replaceValueWithFile(f *cli.Flag) {
95+
value, _ := f.GetValue()
96+
data, err := ioutil.ReadFile(value)
97+
if err != nil {
98+
panic(err)
99+
}
100+
f.SetValue(string(data))
101+
}

openapi/rpc_test.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,21 @@ func TestRpcInvoker_Prepare(t *testing.T) {
101101
}
102102
err = a.Prepare(ctx)
103103
assert.Nil(t, err)
104+
105+
a.api.Parameters = []meta.Parameter{
106+
{
107+
Name: "body",
108+
Position: "Domain",
109+
},
110+
}
111+
ctx.SetUnknownFlags(cli.NewFlagSet())
112+
ctx.UnknownFlags().AddByName("body-FILE")
113+
defer func() {
114+
e := recover()
115+
assert.NotNil(t, e)
116+
}()
117+
a.Prepare(ctx)
118+
104119
}
105120

106121
func TestRpcInvoker_Call(t *testing.T) {

0 commit comments

Comments
 (0)