@@ -24,6 +24,26 @@ export function buildSshArgs(options: SshArgOptions): string[] {
2424 return args ;
2525}
2626
27+ function escapeShellArg ( value : string ) : string {
28+ return `'${ value . replace ( / ' / g, `'"'"'` ) } '` ;
29+ }
30+
31+ /**
32+ * Build a shell prelude that exports DDEV environment variables (DB_HOST, etc.)
33+ * from .ddev/config.yaml. SSH sessions don't inherit container env vars, so
34+ * commands that need a Drupal bootstrap (like drush) require this.
35+ */
36+ export function buildDdevEnvPrelude ( workingDir ?: string ) : string {
37+ if ( ! workingDir ) {
38+ return "" ;
39+ }
40+
41+ const baseDir = workingDir . replace ( / \/ $ / , "" ) ;
42+ const configPath = escapeShellArg ( `${ baseDir } /.ddev/config.yaml` ) ;
43+
44+ return `if [ -f ${ configPath } ]; then while IFS= read -r kv; do [ -n "$kv" ] && export "$kv"; done < <(awk -F'- ' '/- (DB_(HOST|NAME|USER|PASS)|HASH_SALT|ENVIRONMENT_NAME)=/ {print $2}' ${ configPath } ); fi; ` ;
45+ }
46+
2747/**
2848 * Executes commands on SSH hosts.
2949 * Assumes SSH keys are configured and available (e.g. via homeadditions).
@@ -64,7 +84,7 @@ export class SshExecutor implements ContainerExecutor {
6484 remoteCmd = `cd ${ this . escapeShellArg ( workingDir ) } && ${ remoteCmd } ` ;
6585 }
6686
67- const envPrelude = this . buildDdevEnvPrelude ( workingDir ) ;
87+ const envPrelude = buildDdevEnvPrelude ( workingDir ) ;
6888 if ( envPrelude ) {
6989 remoteCmd = `${ envPrelude } ${ remoteCmd } ` ;
7090 if ( log . isVerbose ( ) ) {
@@ -140,18 +160,7 @@ export class SshExecutor implements ContainerExecutor {
140160 }
141161
142162 private escapeShellArg ( value : string ) : string {
143- return `'${ value . replace ( / ' / g, `'"'"'` ) } '` ;
144- }
145-
146- private buildDdevEnvPrelude ( workingDir ?: string ) : string {
147- if ( ! workingDir ) {
148- return "" ;
149- }
150-
151- const baseDir = workingDir . replace ( / \/ $ / , "" ) ;
152- const configPath = this . escapeShellArg ( `${ baseDir } /.ddev/config.yaml` ) ;
153-
154- return `if [ -f ${ configPath } ]; then while IFS= read -r kv; do [ -n "$kv" ] && export "$kv"; done < <(awk -F'- ' '/- (DB_(HOST|NAME|USER|PASS)|HASH_SALT|ENVIRONMENT_NAME)=/ {print $2}' ${ configPath } ); fi; ` ;
163+ return escapeShellArg ( value ) ;
155164 }
156165}
157166
0 commit comments