Skip to content

Commit 5e42bf2

Browse files
authored
Don't fetch code unless is necessary (#263)
1 parent 4ca7c1e commit 5e42bf2

2 files changed

Lines changed: 19 additions & 17 deletions

File tree

‎src/commands/runtime/action/get.js‎

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,11 @@ class ActionGet extends RuntimeBaseCommand {
2020
const { args, flags } = this.parse(ActionGet)
2121
const name = args.actionName
2222
const ow = await this.wsk()
23+
const saveCode = flags.save || flags['save-as']
24+
const fetchCode = !!(saveCode || flags.code || ActionGet.fullGet)
2325

2426
try {
25-
const result = await ow.actions.get(name)
27+
const result = await ow.actions.get({ name, code: fetchCode })
2628
if (flags.url) {
2729
/*
2830
wsk go client uses :
@@ -52,16 +54,14 @@ class ActionGet extends RuntimeBaseCommand {
5254
this.log(`${opts.api}${nsPrefix}/${namespace}/${actionPrefix}/${packageName}${result.name}`)
5355
}
5456
} else {
55-
const bSaveFile = flags['save-as'] && flags['save-as'].length > 0
56-
57-
if (flags.save || bSaveFile) {
57+
if (saveCode) {
5858
if (result.exec.binary) {
59-
const saveFileName = bSaveFile ? flags['save-as'] : `${result.name}.zip`
59+
const saveFileName = flags['save-as'] || `${result.name}.zip`
6060
const data = Buffer.from(result.exec.code, 'base64')
6161
fs.writeFileSync(saveFileName, data, 'buffer')
6262
} else {
6363
const extension = fileExtensionForKind(result.exec.kind)
64-
const saveFileName = bSaveFile ? flags['save-as'] : `${result.name}${extension}`
64+
const saveFileName = flags['save-as'] || `${result.name}${extension}`
6565
fs.writeFileSync(saveFileName, result.exec.code)
6666
}
6767
} else if (ActionGet.fullGet) {
@@ -106,10 +106,12 @@ ActionGet.flags = {
106106
}),
107107
code: flags.boolean({
108108
char: 'c',
109-
description: 'show action code (only works if code is not a zip file)'
109+
description: 'show action code (only works if code is not a zip file)',
110+
default: false
110111
}),
111112
save: flags.boolean({
112-
description: 'save action code to file corresponding with action name'
113+
description: 'save action code to file corresponding with action name',
114+
default: false
113115
}),
114116
'save-as': flags.string({
115117
description: 'file to save action code to'

‎test/commands/runtime/action/get.test.js‎

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ describe('instance methods', () => {
6161
command.argv = ['hello']
6262
return command.run()
6363
.then(() => {
64-
expect(cmd).toHaveBeenCalledWith('hello')
64+
expect(cmd).toHaveBeenCalledWith({ code: false, name: 'hello' })
6565
expect(stdout.output).toMatch('')
6666
})
6767
})
@@ -192,7 +192,7 @@ describe('instance methods', () => {
192192
command.argv = ['hello']
193193
return command.run()
194194
.then(() => {
195-
expect(cmd).toHaveBeenCalledWith('hello')
195+
expect(cmd).toHaveBeenCalledWith({ code: false, name: 'hello' })
196196
expect(stdout.output).toMatch('') // TODO: json output
197197
})
198198
})
@@ -220,7 +220,7 @@ describe('instance methods', () => {
220220
command.argv = ['hello', '--save']
221221
return command.run()
222222
.then(() => {
223-
expect(cmd).toHaveBeenCalledWith('hello')
223+
expect(cmd).toHaveBeenCalledWith({ code: true, name: 'hello' })
224224
expect(fs.writeFileSync).toHaveBeenCalledWith('hello.js', 'this is the code')
225225
})
226226
})
@@ -231,7 +231,7 @@ describe('instance methods', () => {
231231
command.argv = ['pkg/hello', '--save']
232232
return command.run()
233233
.then(() => {
234-
expect(cmd).toHaveBeenCalledWith('pkg/hello')
234+
expect(cmd).toHaveBeenCalledWith({ code: true, name: 'pkg/hello' })
235235
expect(fs.writeFileSync).toHaveBeenCalledWith('hello.js', 'this is the code')
236236
})
237237
})
@@ -242,7 +242,7 @@ describe('instance methods', () => {
242242
command.argv = ['hello', '--save-as', 'filename.js']
243243
return command.run()
244244
.then(() => {
245-
expect(cmd).toHaveBeenCalledWith('hello')
245+
expect(cmd).toHaveBeenCalledWith({ code: true, name: 'hello' })
246246
expect(fs.writeFileSync).toHaveBeenCalledWith('filename.js', 'this is the code')
247247
})
248248
})
@@ -253,7 +253,7 @@ describe('instance methods', () => {
253253
command.argv = ['hello', '--save']
254254
return command.run()
255255
.then(() => {
256-
expect(cmd).toHaveBeenCalledWith('hello')
256+
expect(cmd).toHaveBeenCalledWith({ code: true, name: 'hello' })
257257
expect(fs.writeFileSync).toHaveBeenCalledWith('hello.zip',
258258
bufferData, 'buffer')
259259
})
@@ -265,7 +265,7 @@ describe('instance methods', () => {
265265
command.argv = ['hello', '--save-as', 'filename.zip']
266266
return command.run()
267267
.then(() => {
268-
expect(cmd).toHaveBeenCalledWith('hello')
268+
expect(cmd).toHaveBeenCalledWith({ code: true, name: 'hello' })
269269
expect(fs.writeFileSync).toHaveBeenCalledWith('filename.zip',
270270
bufferData, 'buffer')
271271
})
@@ -277,7 +277,7 @@ describe('instance methods', () => {
277277
command.argv = ['hello']
278278
return command.run()
279279
.then(() => {
280-
expect(cmd).toHaveBeenCalledWith('hello')
280+
expect(cmd).toHaveBeenCalledWith({ name: 'hello', code: true })
281281
const result = JSON.parse(stdout.output)
282282
delete result.date
283283
expect(`${JSON.stringify(result, null, 2)}\n`).toMatchFixture('action/get.json')
@@ -290,7 +290,7 @@ describe('instance methods', () => {
290290
command.argv = ['hello', '--code']
291291
return command.run()
292292
.then(() => {
293-
expect(cmd).toHaveBeenCalledWith('hello')
293+
expect(cmd).toHaveBeenCalledWith({ code: true, name: 'hello' })
294294
expect(stdout.output).toMatch('this is the code')
295295
})
296296
})

0 commit comments

Comments
 (0)