summaryrefslogtreecommitdiffstats
path: root/static/config-manager.js
blob: eea6c91c6146ec59879935329003e94e26f3a06f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/* jshint esversion: 2024, module: true */

import { ApiClient } from "./api-client.js";
import { ValidationState } from "./enums.js";

/**
 * Static Configuration Manager for handling config files
 * @class ConfigManager
 */
class ConfigManager {
	/**
	 * Refresh configuration file list
	 * @static
	 * @returns {Promise<Array>} Array of file names
	 */
	static async refreshConfigs() {
		try {
			const data = await ApiClient.getConfigFiles();
			return data.files || [];
		} catch (error) {
			throw new Error(`Failed to list configs: ${error.message}`);
		}
	}

	/**
	 * Load selected configuration file
	 * @static
	 * @param {string} name - File name
	 * @returns {Promise<string>} File content
	 */
	static async loadConfig(name) {
		try {
			return await ApiClient.getConfigFile(name);
		} catch (error) {
			throw new Error(`Failed to load: ${error.message}`);
		}
	}

	/**
	 * Validate current configuration
	 * @static
	 * @param {string} name - File name
	 * @param {string} content - File content
	 * @returns {Promise<Object>} Validation result
	 */
	static async validateConfig(name, content) {
		try {
			return await ApiClient.validateConfig(name, content);
		} catch (error) {
			throw new Error(`Validation failed: ${error.message}`);
		}
	}

	/**
	 * Save current configuration
	 * @static
	 * @param {string} name - File name
	 * @param {string} content - File content
	 * @param {boolean} restart - Restart service
	 * @returns {Promise<Object>} Save result
	 */
	static async saveConfig(name, content, restart) {
		try {
			return await ApiClient.saveConfig(name, content, restart);
		} catch (error) {
			throw new Error(`Save failed: ${error.message}`);
		}
	}

	/**
	 * Get validation state class
	 * @static
	 * @param {Symbol} state - Validation state
	 * @returns {string} CSS class
	 */
	static getValidationClass(state) {
		switch (state) {
			case ValidationState.SUCCESS:
				return "validation-success";
			case ValidationState.ERROR:
				return "validation-error";
			case ValidationState.PENDING:
				return "validation-pending";
			default:
				return "";
		}
	}

	/**
	 * Get validation message
	 * @static
	 * @param {Symbol} state - Validation state
	 * @param {string} [message] - Additional message
	 * @returns {string} Validation message
	 */
	static getValidationMessage(state, message = "") {
		switch (state) {
			case ValidationState.SUCCESS:
				return "✓ Configuration is valid";
			case ValidationState.ERROR:
				return `${message || "Validation failed"}`;
			case ValidationState.PENDING:
				return "Validating...";
			default:
				return "";
		}
	}
}

export { ConfigManager };