mirror of
https://github.com/RGBCube/ManageTTD
synced 2025-07-28 09:27:45 +00:00
Feat: Fix toml errors & add new command
This commit is contained in:
parent
185789c9d5
commit
564417a05b
4 changed files with 56 additions and 7 deletions
0
openttd.cfg
Normal file
0
openttd.cfg
Normal file
|
@ -2,6 +2,16 @@ module config
|
||||||
|
|
||||||
import strings
|
import strings
|
||||||
import toml
|
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 {
|
fn nested_string_map_to_toml(m map[string]map[string]string) string {
|
||||||
mut toml_string := strings.new_builder(12800)
|
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}]')
|
toml_string.writeln('[${section}]')
|
||||||
|
|
||||||
for field, value in fields {
|
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`)
|
toml_string.write_rune(`\n`)
|
||||||
|
@ -81,8 +92,8 @@ pub fn (mc ManageTTDConfig) to_openttd_config() !OpenTTDConfig {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return OpenTTDConfig{
|
return dump(OpenTTDConfig{
|
||||||
content: config
|
content: config
|
||||||
@type: mc.@type
|
@type: mc.@type
|
||||||
}
|
})
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,7 +11,9 @@ pub fn (m Mapping) str() string {
|
||||||
mapping_string.writeln('[${section}]')
|
mapping_string.writeln('[${section}]')
|
||||||
|
|
||||||
for field, field_value in fields {
|
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`)
|
mapping_string.write_rune(`\n`)
|
||||||
|
|
42
src/main.v
42
src/main.v
|
@ -4,15 +4,50 @@ import config
|
||||||
import cli
|
import cli
|
||||||
import os
|
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 {
|
fn generate_command() cli.Command {
|
||||||
return cli.Command{
|
return cli.Command{
|
||||||
name: 'convert'
|
name: 'convert'
|
||||||
usage: 'convert <from.toml> <to.cfg>'
|
usage: 'convert <from.toml> <to.cfg>'
|
||||||
description: 'Convert a ManageTTD TOML configuration file to an OpenTTD configuration file.'
|
description: 'Convert a ManageTTD TOML configuration file to an OpenTTD configuration file.'
|
||||||
required_args: 2
|
required_args: 2
|
||||||
execute: fn (cmd cli.Command) ! {
|
execute: fn (command cli.Command) ! {
|
||||||
from := cmd.args[0]
|
from := command.args[0]
|
||||||
to := cmd.args[1]
|
to := command.args[1]
|
||||||
|
|
||||||
@type := match from {
|
@type := match from {
|
||||||
'openttd.toml' {
|
'openttd.toml' {
|
||||||
|
@ -45,6 +80,7 @@ fn main() {
|
||||||
description: 'OpenTTD server management software.'
|
description: 'OpenTTD server management software.'
|
||||||
version: '0.0.1'
|
version: '0.0.1'
|
||||||
commands: [
|
commands: [
|
||||||
|
new_config_command(),
|
||||||
generate_command(),
|
generate_command(),
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue