Skip to content

Commit 5945e0e

Browse files
committed
updated app to support reading and writing config
1 parent ff6e8a9 commit 5945e0e

File tree

1 file changed

+34
-3
lines changed

1 file changed

+34
-3
lines changed

src/app.tsx

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
2-
* This file is part of Cockpit.
2+
* This file is part of cockpit-aiscot
33
*
4-
* Copyright (C) 2017 Red Hat, Inc.
4+
* Copyright Sensors & Signals LLC https://www.snstac.com/
55
*
66
* Cockpit is free software; you can redistribute it and/or modify it
77
* under the terms of the GNU Lesser General Public License as published by
@@ -49,7 +49,6 @@ import { capitalize } from '@patternfly/react-core';
4949

5050
const _ = cockpit.gettext;
5151

52-
5352
export const Application: React.FC = () => {
5453
// Configuration
5554
const SERVICE_NAME = 'aiscot'; // Change this to your service name
@@ -389,6 +388,38 @@ export const Application: React.FC = () => {
389388

390389
const [formErrors, setFormErrors] = useState<Record<string, string>>({});
391390

391+
useEffect(() => {
392+
async function readConfigFileAndPopulateForm() {
393+
try {
394+
const content = await cockpit.file(CONFIG_FILE, { superuser: "try" }).read();
395+
setConfigFileContents(content);
396+
397+
// Parse config file lines
398+
const lines = content.split('\n');
399+
const newForm: Record<string, string> = { ...envVarForm };
400+
for (const line of lines) {
401+
// Ignore comments and empty lines
402+
const trimmed = line.trim();
403+
if (!trimmed || trimmed.startsWith('#')) continue;
404+
const match = trimmed.match(/^([A-Za-z0-9_]+)=(.*)$/);
405+
if (match) {
406+
let [, key, value] = match;
407+
// Remove quotes if present
408+
value = value.replace(/^"(.*)"$/, '$1').replace(/^'(.*)'$/, '$1');
409+
if (key in CONF_PARAMS) {
410+
newForm[key] = value;
411+
}
412+
}
413+
}
414+
setEnvVarForm(newForm);
415+
} catch (err) {
416+
// Ignore error, configFileContents already set by other effect
417+
}
418+
}
419+
readConfigFileAndPopulateForm();
420+
// eslint-disable-next-line react-hooks/exhaustive-deps
421+
}, [configFileContents]);
422+
392423
// Validation helper
393424
function validateField(key: string, value: string): string {
394425
const def = CONF_PARAMS[key];

0 commit comments

Comments
 (0)