Skip to content

Commit ae05d8f

Browse files
Add support for remote configs (#43)
1 parent 1266a46 commit ae05d8f

File tree

3 files changed

+33
-11
lines changed

3 files changed

+33
-11
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
### Added
11+
12+
- Added support for specifying urls for the `--configPath` option in the backend.
13+
1014
## [2.0.0] - 2024-10-17
1115

1216
### Added

packages/backend/src/index.ts

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { AppContext, Repository } from "./types.js";
1010
import { cloneRepository, fetchRepository } from "./git.js";
1111
import { createLogger } from "./logger.js";
1212
import { createRepository, Database, loadDB, updateRepository } from './db.js';
13-
import { measure } from "./utils.js";
13+
import { isRemotePath, measure } from "./utils.js";
1414
import { REINDEX_INTERVAL_MS, RESYNC_CONFIG_INTERVAL_MS } from "./constants.js";
1515
import stripJsonComments from 'strip-json-comments';
1616

@@ -41,10 +41,22 @@ const indexRepository = async (repo: Repository, ctx: AppContext) => {
4141
}
4242

4343
const syncConfig = async (configPath: string, db: Database, signal: AbortSignal, ctx: AppContext) => {
44-
const configContent = await readFile(configPath, {
45-
encoding: 'utf-8',
46-
signal,
47-
});
44+
const configContent = await (async () => {
45+
if (isRemotePath(configPath)) {
46+
const response = await fetch(configPath, {
47+
signal,
48+
});
49+
if (!response.ok) {
50+
throw new Error(`Failed to fetch config file ${configPath}: ${response.statusText}`);
51+
}
52+
return response.text();
53+
} else {
54+
return readFile(configPath, {
55+
encoding: 'utf-8',
56+
signal,
57+
});
58+
}
59+
})();
4860

4961
// @todo: we should validate the configuration file's structure here.
5062
const config = JSON.parse(stripJsonComments(configContent)) as SourcebotConfigurationSchema;
@@ -122,7 +134,7 @@ const syncConfig = async (configPath: string, db: Database, signal: AbortSignal,
122134
});
123135
const args = parser.parse_args() as Arguments;
124136

125-
if (!existsSync(args.configPath)) {
137+
if (!isRemotePath(args.configPath) && !existsSync(args.configPath)) {
126138
console.error(`Config file ${args.configPath} does not exist`);
127139
process.exit(1);
128140
}
@@ -173,11 +185,13 @@ const syncConfig = async (configPath: string, db: Database, signal: AbortSignal,
173185
});
174186
}
175187

176-
// Re-sync on file changes
177-
watch(args.configPath, () => {
178-
logger.info(`Config file ${args.configPath} changed. Re-syncing...`);
179-
_syncConfig();
180-
});
188+
// Re-sync on file changes if the config file is local
189+
if (!isRemotePath(args.configPath)) {
190+
watch(args.configPath, () => {
191+
logger.info(`Config file ${args.configPath} changed. Re-syncing...`);
192+
_syncConfig();
193+
});
194+
}
181195

182196
// Re-sync every 24 hours
183197
setInterval(() => {

packages/backend/src/utils.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,8 @@ export const getTokenFromConfig = (token: string | { env: string }, ctx: AppCont
5555
throw new Error(`The environment variable '${token.env}' was referenced in ${ctx.configPath}, but was not set.`);
5656
}
5757
return tokenValue;
58+
}
59+
60+
export const isRemotePath = (path: string) => {
61+
return path.startsWith('https://') || path.startsWith('http://');
5862
}

0 commit comments

Comments
 (0)