Skip to content

Commit 73204bf

Browse files
authored
fix detection of two part engine tooling commands (#60)
* fix detection of two part engine tooling commands * fix busted tests * add some tooling tests around engine scripts * add some tooling tests around engine scripts part 2 * add some tooling tests around engine scripts part 3
1 parent b49b38d commit 73204bf

File tree

5 files changed

+100
-6
lines changed

5 files changed

+100
-6
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
## v3.20.1 - [September 27, 2023](https://github.com/lando/core/releases/tag/3.20.1)
2+
3+
### Fixes
4+
5+
* Fixed bug where `tooling` commands using `app` bootstrap would use `/` instead of `appMount`
6+
17
## v3.20.0 - [September 22, 2023](https://github.com/lando/core/releases/tag/3.20.0)
28

39
### New Features

examples/tooling/.lando.yml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,29 @@ tooling:
124124
message: What is the word?
125125
default: bird
126126
weight: 600
127+
word-engine [word]:
128+
service: :service
129+
cmd: /app/word-engine.sh
130+
level: engine
131+
options:
132+
service:
133+
default: web
134+
alias:
135+
- s
136+
describe: Runs in this service
137+
random:
138+
passthrough: true
139+
alias:
140+
- w
141+
describe: Random number
142+
interactive:
143+
type: input
144+
message: What is the random number?
145+
default: 7
146+
weight: 600
147+
lonely-bird:
148+
service: web
149+
cmd: /app/word.sh
127150
dynamic:
128151
cmd: env
129152
service: :service
@@ -171,3 +194,12 @@ tooling:
171194
alias:
172195
- s
173196
describe: Runs in this service
197+
'pwd-app [file]':
198+
cmd: pwd
199+
service: :container
200+
options:
201+
container:
202+
default: node
203+
alias:
204+
- s
205+
describe: Runs in this service

examples/tooling/README.md

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,21 @@ lando notme --service l337-node | grep www-data
4646
lando test
4747
lando test2
4848

49-
# Should be able to define and pass down options to a script
49+
# Should be able to define and pass down options and args to a script
5050
lando word --word bird | grep "bird is the word"
5151
lando word -w gird | grep "gird is the word"
5252
lando word --word "this is actually a phrase" | grep "this is actually a phrase"
5353
lando word --service l337-node --word bird | grep "bird is the word"
5454
lando word --service l337-node -w gird | grep "gird is the word"
5555
lando word --service l337-node --word "this is actually a phrase" | grep "this is actually a phrase"
56+
lando word-engine --random 1 | grep "bird is the word"
57+
lando word-engine bird --random 1 | grep "bird is the word"
58+
lando word-engine gird --random 1 | grep "gird is the word"
59+
lando word-engine "this is actually a phrase" --random 1 | grep "this is actually a phrase"
60+
lando word-engine --random 1 --service l337-node bird | grep "bird is the word"
61+
lando word-engine --random 1 --service l337-node gird | grep "gird is the word"
62+
lando word-engine --random 1 --service l337-node "this is actually a phrase" | grep "this is actually a phrase"
63+
lando lonely-bird
5664

5765
# Should be able to run multiple commands on multiple services
5866
lando env
@@ -96,6 +104,10 @@ lando ssh --service node -c "pwd" | grep /app
96104
# Should use and track appMount by default
97105
lando pwd | grep /app
98106
cd folder && lando pwd | grep /app/folder && cd ..
107+
lando pwd-app | grep /app
108+
cd folder && lando pwd-app | grep /app/folder && cd ..
109+
lando pwd-app --container l337-node | grep /app
110+
cd folder && lando pwd-app --container l337-node | grep /app/folder && cd ..
99111
lando ssh -c "pwd" | grep /app
100112
cd folder && lando ssh -c "pwd" | grep /app/folder && cd ..
101113
lando pwd --service l337-node | grep /app
@@ -116,8 +128,6 @@ lando pwd --service l337-slim | grep /tmp
116128

117129
# Should use first lando 3 service as default if no appserver
118130
lando ssh -c "env" | grep PRIMARY_SERVICE | grep yes
119-
120-
121131
```
122132

123133
Destroy tests

examples/tooling/word-engine.sh

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#!/bin/bash
2+
3+
# Set default opts
4+
WORD="bird"
5+
6+
# PARSE THE ARGZZ
7+
# TODO: compress the mostly duplicate code below?
8+
while (( "$#" )); do
9+
case "$1" in
10+
--random|--random=*)
11+
if [ "${1##--random=}" != "$1" ]; then
12+
shift
13+
else
14+
shift 2
15+
fi
16+
;;
17+
--surprise)
18+
echo "MUHAHAH! You've found a non explicitly declared option for this script which is going to give you an error!"
19+
exit 666
20+
;;
21+
--)
22+
shift
23+
break
24+
;;
25+
-*|--*=)
26+
echo "Error: Unsupported flag $1" >&2
27+
exit 1
28+
;;
29+
*)
30+
WORD="$1"
31+
shift
32+
;;
33+
esac
34+
done
35+
36+
# Demonstrate interactive option passthrough
37+
echo "$WORD, $WORD, $WORD the $WORD is the word!"

lib/bootstrap.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const yaml = require('js-yaml');
1212
*/
1313
const getBsLevel = (config, command) => {
1414
if (_.has(config, `tooling.${command}.level`)) return config.tooling[command].level;
15+
else if (_.find(config.tooling, {id: command}).level) return _.find(config.tooling, {id: command}).level;
1516
else return (!fs.existsSync(config.composeCache)) ? 'app' : 'engine';
1617
};
1718

@@ -66,7 +67,8 @@ const engineRunner = (config, command) => (argv, lando) => {
6667
// @NOTE: can we actually assume this will always find something? i **THINK** we catch upstream?
6768
const task = _.find(app.config.tooling, task => task.name === command);
6869
// get service, note this is not trivial because dynamic services are a thing
69-
const service = task.service !== ':service' ? task.service : argv.service;
70+
71+
const service = !_.startsWith(task.service, ':') ? task.service : argv[_.trim(task.service, ':')];
7072
lando.log.debug('resolved tooling command %s service to %s', command, service);
7173

7274
// ensure all v3 services have their appMount set to /app
@@ -269,18 +271,25 @@ exports.getTasks = (config = {}, argv = {}, tasks = []) => {
269271
config.tooling = _.merge({}, loadCacheFile(config.toolingCache), config.tooling);
270272
}
271273

274+
// lets add ids to help match commands with args?
275+
_.forEach(config.tooling, (task, command) => {
276+
task.id = task.id || command.split(' ')[0];
277+
});
278+
272279
// If the tooling command is being called lets assess whether we can get away with engine bootstrap level
273-
const level = (_.includes(_.keys(config.tooling), argv._[0])) ? getBsLevel(config, argv._[0]) : 'app';
280+
const ids = _.map(config.tooling, tooling => tooling.id);
281+
const level = (_.includes(ids, argv._[0])) ? getBsLevel(config, argv._[0]) : 'app';
274282

275283
// Load all the tasks, remember we need to remove "disabled" tasks (eg non-object tasks) here
276284
_.forEach(_.get(config, 'tooling', {}), (task, command) => {
277285
if (_.isObject(task)) {
278286
tasks.push({
279287
command,
288+
id: command.split(' ')[0],
280289
level,
281290
describe: _.get(task, 'description', `Runs ${command} commands`),
282291
options: _.get(task, 'options', {}),
283-
run: (level === 'app') ? appRunner(command) : engineRunner({...config, argv}, command),
292+
run: (level === 'app') ? appRunner(command) : engineRunner({...config, argv}, command, task),
284293
delegate: _.isEmpty(_.get(task, 'options', {})),
285294
});
286295
}

0 commit comments

Comments
 (0)