Skip to content

Commit

Permalink
load shell automatically from env for GUI
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelneale committed Jan 30, 2025
1 parent 6e9423b commit 9325fcf
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 13 deletions.
4 changes: 2 additions & 2 deletions ui/desktop/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import started from 'electron-squirrel-startup';
import path from 'node:path';
import { startGoosed } from './goosed';
import { getBinaryPath } from './utils/binaryPath';
import { loadZshEnv } from './utils/loadEnv';
import { loadShellEnv } from './utils/loadEnv';
import log from './utils/logger';
import { addRecentDir, loadRecentDirs } from './utils/recentDirs';
import {
Expand Down Expand Up @@ -69,7 +69,7 @@ const parseArgs = () => {
};

const getGooseProvider = () => {
loadZshEnv(app.isPackaged);
loadShellEnv(app.isPackaged);
//{env-macro-start}//
//needed when goose is bundled for a specific provider
//{env-macro-end}//
Expand Down
16 changes: 5 additions & 11 deletions ui/desktop/src/utils/loadEnv.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { execSync } from 'child_process';
import path from 'path';
import log from './logger';
import fs from 'node:fs';

export function loadZshEnv(isProduction: boolean = false): void {
export function loadShellEnv(isProduction: boolean = false): void {
// Only proceed if running on macOS and in production mode
if (process.platform !== 'darwin' || !isProduction) {
log.info(
Expand All @@ -15,16 +13,11 @@ export function loadZshEnv(isProduction: boolean = false): void {
}

try {
// Execute zsh and source the zshrc file, then export all environment variables
const zshrcPath = path.join(process.env.HOME || '', '.zshrc');
log.info('LOADING ENV');

// if no file then return
if (!fs.existsSync(zshrcPath)) {
console.log('No zshrc file found');
return;
}
const shell = process.env.SHELL || '/bin/bash'; // Detect user's shell

const envStr = execSync(`/bin/zsh -c 'source ${zshrcPath} && env'`, {
const envStr = execSync(`${shell} -l -i -c 'env'`, {
encoding: 'utf-8',
});

Expand All @@ -33,6 +26,7 @@ export function loadZshEnv(isProduction: boolean = false): void {
const matches = line.match(/^([^=]+)=(.*)$/);
if (matches) {
const [, key, value] = matches;
log.info(`Setting ${key}`);
process.env[key] = value;
}
});
Expand Down

0 comments on commit 9325fcf

Please sign in to comment.