config package - gitlab.com/blockchain-privacy/gomisc/config - Go Packages

config

package
v0.0.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 28, 2026 License: AGPL-3.0 Imports: 5 Imported by: 0

README

Config

This module supports reading configuration values from a YAML file.

Using this module

Import the module, create a struct containing all the needed configuration options and add YAML flags for them. Call flag.Parse() if the command-line options are wanted.

Example

package main

import (
	"flag"
	"fmt"
	"gitlab.com/blockchain-privacy/gomisc/config"
)

func main() {
	type Config struct {
		SaveDir string `yaml:"saveDir"`
		Logfile string `yaml:"logfile"`
	}

	var defaultConfig = Config{
		SaveDir: "/some/path/to/a/dir",
		Logfile: "/some/path/to/a/file",
	}
	
	const defaultConfigName = "config.yml"
    var filePath string
    var createConfigFile bool
    config.SetConfigFlags(defaultConfigName, &filePath, &createConfigFile)
    flag.Parse()

    if createConfigFile {
        fmt.Println("Generating configuration file ...")

        err := cli.WriteConfig(defaultConfigName, defaultConfig)
        if err != nil {
            fmt.Println(err)
            return
        }

        fmt.Println("config file", defaultConfigName, "successfully created")
        return
    }

    var newConfig Config
    if err := config.ReadConfig(filePath, &newConfig); err != nil {
        fmt.Println(err)
        return
    }
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ReadConfig

func ReadConfig(configFilePath string, config interface{}) error

ReadConfig reads the config file from the given file path

func SetConfigFlags

func SetConfigFlags(defaultConfigName string, filePath *string, createConfigFile *bool)

SetConfigFlags sets the CLI flags for accessing and generating the configuration file

func WriteConfig

func WriteConfig(filePath string, config interface{}) error

WriteConfig writes the config value to the given file path

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL