From 564417a05b0c6d18e4dda81d6b052a6a2c693144 Mon Sep 17 00:00:00 2001 From: RGBCube Date: Sat, 18 Mar 2023 23:39:17 +0300 Subject: [PATCH] Feat: Fix toml errors & add new command --- openttd.cfg | 0 src/config/configs.v | 17 ++++++++++++++--- src/config/mapping.v | 4 +++- src/main.v | 42 +++++++++++++++++++++++++++++++++++++++--- 4 files changed, 56 insertions(+), 7 deletions(-) create mode 100644 openttd.cfg diff --git a/openttd.cfg b/openttd.cfg new file mode 100644 index 0000000..e69de29 diff --git a/src/config/configs.v b/src/config/configs.v index d401762..d7d55d0 100644 --- a/src/config/configs.v +++ b/src/config/configs.v @@ -2,6 +2,16 @@ module config import strings import toml +import strconv + +fn string_is_int(s string) bool { + strconv.atoi(s) or { return false } + return true +} + +fn string_should_be_quoted(s string) bool { + return s !in ['true', 'false'] && !string_is_int(s) +} fn nested_string_map_to_toml(m map[string]map[string]string) string { mut toml_string := strings.new_builder(12800) @@ -10,7 +20,8 @@ fn nested_string_map_to_toml(m map[string]map[string]string) string { toml_string.writeln('[${section}]') for field, value in fields { - toml_string.writeln('${field} = ${value}') + quote := if string_should_be_quoted(value) { '"' } else { '' } + toml_string.writeln('${field} = ${quote}${value}${quote}') } toml_string.write_rune(`\n`) @@ -81,8 +92,8 @@ pub fn (mc ManageTTDConfig) to_openttd_config() !OpenTTDConfig { } } - return OpenTTDConfig{ + return dump(OpenTTDConfig{ content: config @type: mc.@type - } + }) } diff --git a/src/config/mapping.v b/src/config/mapping.v index da0d484..8bc8416 100644 --- a/src/config/mapping.v +++ b/src/config/mapping.v @@ -11,7 +11,9 @@ pub fn (m Mapping) str() string { mapping_string.writeln('[${section}]') for field, field_value in fields { - mapping_string.writeln('${field} = ${field_value.transformed_default_value()}}') + value := field_value.transformed_default_value() + quote := if string_should_be_quoted(value) { '"' } else { '' } + mapping_string.writeln('${field} = ${quote}${value}${quote}') } mapping_string.write_rune(`\n`) diff --git a/src/main.v b/src/main.v index d4e4412..dba4c22 100644 --- a/src/main.v +++ b/src/main.v @@ -4,15 +4,50 @@ import config import cli import os +fn new_config_command() cli.Command { + return cli.Command{ + name: 'new-config' + description: 'Creates a new ManageTTD configuration file in the current directory.' + execute: fn (command cli.Command) ! { + file := command.args[0] + + @type := match file { + 'openttd.toml' { + config.ConfigType.openttd + } + 'private.toml' { + config.ConfigType.private + } + 'secrets.toml' { + config.ConfigType.secrets + } + else { + return error('Unknown configuration file type, cannot create. +Accepted file types are openttd.toml, private.toml and secrets.toml.') + } + } + + if os.exists(file) { + return error('File ${file} already exists.') + } + + os.write_file(file, @type.mapping().str())! + + println('Successfully created ${file}.') + } + required_args: 1 + } +} + fn generate_command() cli.Command { return cli.Command{ name: 'convert' usage: 'convert ' description: 'Convert a ManageTTD TOML configuration file to an OpenTTD configuration file.' required_args: 2 - execute: fn (cmd cli.Command) ! { - from := cmd.args[0] - to := cmd.args[1] + execute: fn (command cli.Command) ! { + from := command.args[0] + to := command.args[1] @type := match from { 'openttd.toml' { @@ -45,6 +80,7 @@ fn main() { description: 'OpenTTD server management software.' version: '0.0.1' commands: [ + new_config_command(), generate_command(), ] }