custom environment vars (fixes #346)

This commit is contained in:
Eugene Pankov 2018-10-25 15:51:46 +02:00
parent 4682ef72a1
commit 7566bcaaac
4 changed files with 51 additions and 2 deletions

View File

@ -51,3 +51,19 @@ h3.mb-3 Shell
.input-group-btn .input-group-btn
button.btn.btn-secondary((click)='pickWorkingDirectory()') button.btn.btn-secondary((click)='pickWorkingDirectory()')
i.fa.fa-folder-open i.fa.fa-folder-open
.form-line
.header
.title Environment
.description Inject additional environment variables
div
.mb-2.d-flex.align-items-center(*ngFor='let pair of environmentVars')
input.form-control.w-50([(ngModel)]='pair.key', (blur)='saveEnvironment()', placeholder='Variable name')
input.form-control.w-50.mr-1([(ngModel)]='pair.value', (blur)='saveEnvironment()', placeholder='Value')
button.btn.btn-secondary((click)='removeEnvironmentVar(pair.key)')
i.fa.fa-trash-o
button.btn.btn-secondary((click)='addEnvironmentVar()')
i.fa.fa-plus.mr-2
span Add

View File

@ -1,4 +1,5 @@
import { Component, Inject } from '@angular/core' import { Component, Inject } from '@angular/core'
import { Subscription } from 'rxjs'
import { ConfigService, ElectronService } from 'terminus-core' import { ConfigService, ElectronService } from 'terminus-core'
import { IShell, ShellProvider, SessionPersistenceProvider } from '../api' import { IShell, ShellProvider, SessionPersistenceProvider } from '../api'
@ -9,6 +10,9 @@ export class ShellSettingsTabComponent {
shells: IShell[] = [] shells: IShell[] = []
persistenceProviders: SessionPersistenceProvider[] persistenceProviders: SessionPersistenceProvider[]
environmentVars: {key: string, value: string}[] = []
private configSubscription: Subscription
constructor ( constructor (
public config: ConfigService, public config: ConfigService,
private electron: ElectronService, private electron: ElectronService,
@ -16,12 +20,20 @@ export class ShellSettingsTabComponent {
@Inject(SessionPersistenceProvider) persistenceProviders: SessionPersistenceProvider[], @Inject(SessionPersistenceProvider) persistenceProviders: SessionPersistenceProvider[],
) { ) {
this.persistenceProviders = this.config.enabledServices(persistenceProviders).filter(x => x.isAvailable()) this.persistenceProviders = this.config.enabledServices(persistenceProviders).filter(x => x.isAvailable())
config.store.terminal.environment = config.store.terminal.environment || {}
this.reloadEnvironment()
this.configSubscription = config.changed$.subscribe(() => this.reloadEnvironment())
} }
async ngOnInit () { async ngOnInit () {
this.shells = (await Promise.all(this.config.enabledServices(this.shellProviders).map(x => x.provide()))).reduce((a, b) => a.concat(b)) this.shells = (await Promise.all(this.config.enabledServices(this.shellProviders).map(x => x.provide()))).reduce((a, b) => a.concat(b))
} }
ngOnDestroy () {
this.configSubscription.unsubscribe()
}
pickWorkingDirectory () { pickWorkingDirectory () {
let shell = this.shells.find(x => x.id === this.config.store.terminal.shell) let shell = this.shells.find(x => x.id === this.config.store.terminal.shell)
console.log(shell) console.log(shell)
@ -33,4 +45,24 @@ export class ShellSettingsTabComponent {
this.config.store.terminal.workingDirectory = paths[0] this.config.store.terminal.workingDirectory = paths[0]
} }
} }
reloadEnvironment () {
this.environmentVars = Object.entries(this.config.store.terminal.environment).map(([k, v]) => ({ key: k, value: v as string }))
}
saveEnvironment () {
this.config.store.terminal.environment = {}
for (let pair of this.environmentVars) {
this.config.store.terminal.environment[pair.key] = pair.value
}
}
addEnvironmentVar () {
this.environmentVars.push({ key: '', value: '' })
}
removeEnvironmentVar (key: string) {
this.environmentVars = this.environmentVars.filter(x => x.key !== key)
this.saveEnvironment()
}
} }

View File

@ -48,7 +48,8 @@ export class TerminalConfigProvider extends ConfigProvider {
'#ffffff', '#ffffff',
] ]
}, },
customColorSchemes: [] customColorSchemes: [],
environment: {},
}, },
} }

View File

@ -52,7 +52,7 @@ export class TerminalService {
let shells = await this.shells$.toPromise() let shells = await this.shells$.toPromise()
shell = shells.find(x => x.id === this.config.store.terminal.shell) || shells[0] shell = shells.find(x => x.id === this.config.store.terminal.shell) || shells[0]
} }
let env: any = Object.assign({}, process.env, shell.env || {}) let env: any = Object.assign({}, process.env, shell.env || {}, this.config.store.terminal.environment || {})
this.logger.log(`Starting shell ${shell.name}`, shell) this.logger.log(`Starting shell ${shell.name}`, shell)
let sessionOptions = await this.sessions.prepareNewSession({ let sessionOptions = await this.sessions.prepareNewSession({