@@ -17,8 +17,8 @@ export default class Instance {
1717 constructor ( readonly inputs : IInputs ) {
1818 const opts = parseArgv ( inputs . args , {
1919 alias : { help : 'h' , 'assume-yes' : 'y' } ,
20- boolean : [ 'help' , 'y' ] ,
21- string : [ 'region' , 'function-name' , 'qualifier' ] ,
20+ boolean : [ 'help' , 'y' , 'no-workdir' ] ,
21+ string : [ 'region' , 'function-name' , 'qualifier' , 'shell' , 'workdir' ] ,
2222 } ) ;
2323 logger . debug ( `Instance opts: ${ JSON . stringify ( opts ) } ` ) ;
2424 const { region, _ : subCommands } = opts ;
@@ -58,6 +58,8 @@ export default class Instance {
5858 /**
5959 * s instance exec --instance-id c-64fec1fc-27c4833c325445879a28 --cmd "ls -lh"
6060 * s instance exec --instance-id c-64fec1fc-27c4833c325445879a28
61+ * s instance exec --instance-id c-64fec1fc-27c4833c325445879a28 --shell /bin/sh
62+ * s instance exec --instance-id c-64fec1fc-27c4833c325445879a28 --workdir /app
6163 * s instance exec --instance-id `s invoke | grep 'Invoke instanceId:' | sed 's/.*: //'`
6264 */
6365 async exec ( ) {
@@ -71,11 +73,26 @@ export default class Instance {
7173 }
7274 const qualifier = this . opts . qualifier || 'LATEST' ;
7375 const cmd = this . opts . cmd as string ;
76+ const shell = this . opts . shell || 'bash' ;
77+ const workdir = this . opts . workdir ;
78+ const noWorkdir = this . opts [ 'no-workdir' ] ;
7479 let rawData = [ ] ;
7580 if ( cmd ) {
76- rawData = [ 'bash' , '-c' , cmd ] ;
81+ rawData = [ shell , '-c' , cmd ] ;
7782 } else {
78- rawData = [ 'bash' , '-c' , '(cd /code || cd / ) && bash' ] ;
83+ // Build the default command based on workdir option
84+ let defaultCmd : string ;
85+ if ( noWorkdir || workdir === '' ) {
86+ // --no-workdir flag or empty string: don't cd anywhere, use container's default WORKDIR
87+ defaultCmd = shell ;
88+ } else if ( workdir ) {
89+ // User specified a custom workdir
90+ defaultCmd = `cd ${ workdir } && ${ shell } ` ;
91+ } else {
92+ // Default behavior (workdir undefined): try /code first, fallback to /
93+ defaultCmd = `(cd /code || cd /) && ${ shell } ` ;
94+ }
95+ rawData = [ shell , '-c' , defaultCmd ] ;
7996 }
8097 await this . fcSdk . instanceExec ( functionName , instanceId , rawData , qualifier , true ) ;
8198 }
0 commit comments