Skip to content

Commit

Permalink
fix: refactor configuration repository
Browse files Browse the repository at this point in the history
  • Loading branch information
eliseomartelli committed Jul 18, 2024
1 parent 1f9e690 commit f14aae2
Showing 1 changed file with 89 additions and 74 deletions.
163 changes: 89 additions & 74 deletions data/configurationRepository.ts
Original file line number Diff line number Diff line change
@@ -1,102 +1,117 @@
import {
parse,
stringify,
xml_document,
parse,
stringify,
xml_document,
} from "https://deno.land/x/[email protected]/mod.ts";
import { Tool, ToolRepository } from "./toolsrepository.ts";
import { TOOL_DIR } from "../env.ts";
import { join } from "$std/path/join.ts";

interface ConfigurationRepository {
update: () => Promise<void>;
update: () => Promise<void>;
}

interface ConfigurationManager {
read: () => Promise<Configuration>;
write: (_: Configuration) => Promise<void>;
}

export const fileConfigurationManager = (
filename: string,
): ConfigurationManager => {
const read = async (): Promise<Configuration> => {
return await parse_xml(filename).catch(
(error) => {
throw error;
},
) as Configuration;
};

const write = async (configuration: Configuration) => {
await Deno.writeTextFile(
filename,
stringify(configuration),
);
};

return {
read,
write,
};
};

export const configurationRepository = (
toolRepository: ToolRepository,
toolRepository: ToolRepository,
configurationManager: ConfigurationManager,
): ConfigurationRepository => {
return {
update: async () => {
const xmldoc = await parse_xml("./tool_conf.xml").catch(
(error) => {
throw error;
},
) as Configuration;
function updateConfiguration(
toolList: Tool[],
configuration: Configuration,
): Configuration {
const customSection: SectionEntity = generateSection(toolList);

if (!configuration.toolbox.section) {
configuration.toolbox.section = [];
}

configuration.toolbox.section = configuration.toolbox.section!.filter((
item,
) => item["@id"] != customSection["@id"]);

configuration.toolbox.section!.push(customSection);
return configuration;
}

const toolList = await toolRepository.list();
const updatedConfig = updateConfiguration(
toolList,
xmldoc,
);
await Deno.writeTextFile(
"./tool_conf.xml",
stringify(updatedConfig),
);
},
};
function generateSection(
toolList: Tool[],
id: string = "customsection",
name: string = "Custom Section",
): SectionEntity {
return {
"@id": id,
"@name": name,
tool: toolList.map((tool) => {
return {
"@file": join(TOOL_DIR, tool.name),
};
}) as [ToolEntry],
};
}
return {
update: async () => {
const xmldoc = await configurationManager.read();
const toolList = await toolRepository.list();
const updatedConfig = updateConfiguration(
toolList,
xmldoc,
);
await configurationManager.write(updatedConfig);
},
};
};

export interface Configuration extends xml_document {
toolbox: Toolbox;
toolbox: Toolbox;
}

export interface Toolbox {
monitor: string;
section?: (SectionEntity)[] | null;
monitor: string;
section?: (SectionEntity)[] | null;
}

async function parse_xml(filename: string): Promise<Configuration> {
const text_file = await Deno.readTextFile(filename).catch((error) => {
throw error;
});
return parse(text_file) as Configuration;
const text_file = await Deno.readTextFile(filename).catch((error) => {
throw error;
});
return parse(text_file) as Configuration;
}

export interface SectionEntity {
"@id": string;
"@name": string;
tool?: ToolEntry[] | null;
label?: Label | null;
"@id": string;
"@name": string;
tool?: ToolEntry[] | null;
}

export interface ToolEntry {
"@file": string;
}

export interface Label {
id: string;
text: string;
}

function updateConfiguration(
toolList: Tool[],
configuration: Configuration,
): Configuration {
const customSection: SectionEntity = generateSection(toolList);

if (!configuration.toolbox.section) {
configuration.toolbox.section = [];
}

configuration.toolbox.section = configuration.toolbox.section!.filter((
item,
) => item["@id"] != customSection["@id"]);

configuration.toolbox.section!.push(customSection);
return configuration;
}

function generateSection(
toolList: Tool[],
id: string = "customsection",
name: string = "Custom Section",
): SectionEntity {
return {
"@id": id,
"@name": name,
tool: toolList.map((tool) => {
return {
"@file": join(TOOL_DIR, tool.name),
};
}) as [ToolEntry],
};
"@file": string;
}

0 comments on commit f14aae2

Please sign in to comment.