Go to file
Sean Hickey 5cc5aa419e Make surrounding quotes optional 2022-09-27 22:17:45 -07:00
.gitignore Minor doc tweaks. v0.0.2 2022-02-27 02:12:55 -08:00
License.md Minor doc tweaks. v0.0.2 2022-02-27 02:12:55 -08:00
Makefile Add ability to override values with cli flags 2022-06-18 12:44:10 -07:00
Readme.md Make surrounding quotes optional 2022-09-27 22:17:45 -07:00
go.mod Rename to gosimpleconf 2022-02-21 22:47:36 -08:00
gosimpleconf.go Make surrounding quotes optional 2022-09-27 22:17:45 -07:00
gosimpleconf_test.go Make surrounding quotes optional 2022-09-27 22:17:45 -07:00

Readme.md

gosimpleconf

go get gitea.wisellama.rocks/Wisellama/gosimpleconf@v0.0.4

This is a small library for parsing super simple configuration files.

It expects a file with the following format:

  • Lines with key-values pairs separated by = (e.g. foo = bar)
  • Lines that start with # are ignored (used for comments)
  • Empty and whitespace-only lines are ignore
  • Lines with data that don't have an = will cause an error.

The key-values pairs are simply parsed as strings and plopped into a map for you to do whatever your program wants with them. If you need a value to not be a string, you will have to convert the value from a string as needed for your program (e.g. using the strconv library).

Example

Here's an example config file file.conf

url = www.wisellama.rocks
number_of_widgets = 1337
pi = 3.141592653589793238462643383
environment = production
# Quotes are optional
name = Dude guy
description = "Just some dude it was"

Then to load that config file into a map:

configMap, err := gosimpleconf.Load("file.conf")

Then everything is a string. You can parse it as needed.

var piStr string = configMap["pi"]
pi, err := strconv.ParseFloat(value, 64)

To override values with cli flags, after parsing the conf file you can use these functions to also parse the flags:

flagMap := SetupFlagOverrides(configMap)
configMap = ParseFlags(configMap, flagmap)

Then you could, for example, override the url value without modifying the config file:

./program --url=newthing.wisellama.rocks

Why?

I've always seen configuration files similar to this show up in Unix-like systems and programs, but it doesn't seem to be fully standardized under a real name other than just "conf files". The closest I've seen are INI files, but those don't seem to fit the style I was looking for (they don't use # for comments and they support sections in square brackets [section]).

Instead, I just wanted the bare minimum configuration format of foo = bar on each line with # for comments. So that's what this library does.

This project looks abandoned

This library is so simple that I almost never expect to need to update it unless Go fundamentally breaks the language in the future. So while the project may eventually look like it has been abandoned, it is rather simply "complete". (gasp I know, such a rare thing in the software world).

License

This project is licensed under the 2-Clause BSD License (a permissive open source license). See License.md for the full text.