-
Notifications
You must be signed in to change notification settings - Fork 32
DOS-879: Add ability to Permission Scripts #4405
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: development
Are you sure you want to change the base?
Changes from 5 commits
5cfa041
d847a7b
7616c3b
c197564
305fcbf
42c9af4
1b83c8b
077e25c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,7 +11,8 @@ foam.CLASS({ | |
| imports: [ | ||
| 'data as block', | ||
| 'eval_', | ||
| 'scope' | ||
| 'scope', | ||
| 'notify?' | ||
| ], | ||
|
|
||
| documentation: ` | ||
|
|
@@ -24,6 +25,19 @@ foam.CLASS({ | |
| `, | ||
|
|
||
| properties: [ | ||
| { | ||
| class: 'Long', | ||
| name: 'id', | ||
| visibility: 'RO' | ||
| }, | ||
| { | ||
| class: 'String', | ||
| name: 'scriptName' | ||
| }, | ||
| { | ||
| class: 'String', | ||
| name: 'description' | ||
| }, | ||
| { | ||
| class: 'String', | ||
| name: 'code', | ||
|
|
@@ -61,10 +75,16 @@ foam.CLASS({ | |
| actions: [ | ||
| function run() { | ||
| let self = this; | ||
| with ( this.scope ) { | ||
| var _scope = this.scope || {}; | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why have _scope?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Because when i hit run from flow, scope returns undefined |
||
| with ( _scope ) { | ||
| with ( { log: this.log.bind(this) } ) { | ||
| var ret = eval('(async function() {' + self.code + '})').call(self.block); | ||
| ret.then(v => this.log(v), v => this.log(v)).catch(e => this.log(e.stack)); | ||
| ret | ||
| .then(v => { | ||
| this.log(v); | ||
| try { this.notify && this.notify(`Script${this.scriptName ? ' "' + this.scriptName + '"' : ''} executed successfully`); } catch (e) {} | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do this?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @kgrgreer Just wanted to add some feedback: when I tried it, there was no way to tell if the script had run. |
||
| }, v => this.log(v)) | ||
| .catch(e => this.log(e.stack)); | ||
| return ret; | ||
| } | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -332,6 +332,47 @@ foam.CLASS({ | |
| }); | ||
|
|
||
|
|
||
| foam.CLASS({ | ||
| package: 'foam.core.reflow.cmd', | ||
| name: 'Scripts', | ||
| extends: 'foam.core.reflow.cmd.Command', | ||
|
|
||
| requires: [ 'foam.core.reflow.Script', 'foam.core.reflow.cmd.DAORowView' ], | ||
|
|
||
| imports: [ 'reflowLibDAO', 'scope' ], | ||
|
|
||
|
|
||
|
|
||
| properties: [ | ||
| [ 'description', 'Display available scripts' ] | ||
| ], | ||
|
|
||
| methods: [ | ||
| function execute(opt_nameQuery) { | ||
| var self = this; | ||
| var dao = this.reflowLibDAO; | ||
| var count = foam.lang.SimpleSlot.create({value: 0}); | ||
| if ( opt_nameQuery ) dao = dao.where( | ||
| this.OR( | ||
| this.CONTAINS_IC(this.Script.SCRIPT_NAME, opt_nameQuery), | ||
| )); | ||
| this.out.tag('br'); | ||
| this.out.start('table').attr('width', '100%'). | ||
| select(dao, function(n) { | ||
| count.value++; | ||
|
|
||
| this.start('tr'). | ||
| start('td').attr('align', 'left').add(n.scriptName).end(). | ||
| start('td').attr('align', 'left').add(n.description).end(). | ||
| start('td').attr('align', 'left').start(foam.u2.Link).add('Run').on('click', function() { n.run(); }).end(). | ||
| end(); | ||
| }). | ||
| end(). | ||
| start('b').add(count, ' selected').end(); | ||
| } | ||
| ] | ||
| }) | ||
|
|
||
| foam.CLASS({ | ||
| package: 'foam.core.reflow.cmd', | ||
| name: 'DAOS', | ||
|
|
@@ -391,6 +432,84 @@ foam.CLASS({ | |
| ] | ||
| }); | ||
|
|
||
| foam.CLASS({ | ||
| package: 'foam.core.reflow.cmd', | ||
| name: 'RunScript', | ||
| extends: 'foam.core.reflow.cmd.Command', | ||
|
|
||
| mixins: [ 'foam.mlang.Expressions' ], | ||
|
|
||
| imports: [ 'reflowLibDAO', 'notify?' ], | ||
|
|
||
| properties: [ | ||
| { name: 'id', value: 'runScript' }, | ||
| [ 'description', 'Run a saved script by name: runScript <scriptName>' ] | ||
| ], | ||
|
|
||
| methods: [ | ||
| async function execute(scriptName) { | ||
| if ( ! scriptName ) { | ||
| this.notify && this.notify('Usage: runScript <scriptName>'); | ||
| this.out && this.out.add('Usage: runScript <scriptName>'); | ||
| return; | ||
| } | ||
|
|
||
| let script = null; | ||
| try { | ||
| // Try exact match on scriptName field | ||
| const result = await this.reflowLibDAO | ||
| .where(this.EQ(foam.core.reflow.Script.SCRIPT_NAME, scriptName)) | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can do find() instead of limit(1).select(). |
||
| .limit(1) | ||
| .select(); | ||
| script = result && result.array && result.array[0]; | ||
| } catch (e) {} | ||
|
|
||
| if ( ! script ) { | ||
| try { | ||
| // Fallback: try DAO find by id | ||
| script = await this.reflowLibDAO.find(scriptName); | ||
| } catch (e) {} | ||
| } | ||
|
|
||
| if ( ! script ) { | ||
| const msg = `Script "${scriptName}" not found`; | ||
| (this.notify && this.notify(msg)) || (this.out && this.out.add(msg)); | ||
| return; | ||
| } | ||
|
|
||
| try { | ||
| await script.run(); | ||
| this.notify && this.notify(`Script "${script.scriptName || scriptName}" executed successfully`); | ||
| } catch (e) { | ||
| const errMsg = `Script "${scriptName}" failed: ${e && e.message ? e.message : e}`; | ||
| (this.notify && this.notify(errMsg)) || (this.out && this.out.add(errMsg)); | ||
| } | ||
| } | ||
| ] | ||
| }); | ||
|
|
||
| foam.CLASS({ | ||
| package: 'foam.core.reflow.cmd', | ||
| name: 'HelpScript', | ||
| extends: 'foam.core.reflow.cmd.Command', | ||
|
|
||
| properties: [ | ||
| { name: 'id', value: 'helpScript' }, | ||
| [ 'description', 'Display usage for scripts-related commands' ] | ||
| ], | ||
|
|
||
| methods: [ | ||
| function execute() { | ||
| this.out | ||
| .start('div') | ||
| .start('h3').add('Script Commands').end() | ||
| .start('p').add('scripts: Display available scripts').end() | ||
| .start('p').add('runScript <scriptName>: Run a saved script by name').end() | ||
| .end(); | ||
| } | ||
| ] | ||
| }); | ||
|
|
||
| /* | ||
| foam.CLASS({ | ||
| package: 'foam.core.reflow', | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.