config

package
v0.4.3 Latest Latest
Warning

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

Go to latest
Published: Apr 10, 2022 License: Apache-2.0 Imports: 13 Imported by: 1

Documentation

Overview

Package config helps generically manage configuration data in YAML files (and, by extension JSON, which is a YAML subset) using the gopkg.in/yaml.v3 package (v2 is not compatible with encoding/json creating unexpected marshaling errors).

The package provides the high-level functions called by the Bonzai™ branch command of the same name allowing it to be consistently composed into any to any other Bonzai branch. However, composing into the root is generally preferred to avoid configuration name space conflicts and centralize all configuration data for a single Bonzai tree monolith for easy transport.

Rather than provide functions for changing individual values, this package assumes editing of the YAML files directly and provider helpers for system-wide safe concurrent writes and queries using the convention yq/jq syntax. Default directory and file permissions are purposefully more restrictive than the Go default (0700/0600).

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Conf added in v0.4.0

type Conf struct {
	Id   string // usually application name
	Dir  string // usually os.UserConfigDir
	File string // usually config.yaml
}

Conf fulfills bonzai.Configurer

func (Conf) Data added in v0.4.0

func (c Conf) Data() string

Data returns a string buffer containing all of the configuration file data for the given configuration. An empty string is returned and an error logged if any error occurs.

func (Conf) DirPath added in v0.4.0

func (c Conf) DirPath() string

DirPath is the Dir and Id joined.

func (Conf) Edit added in v0.4.0

func (c Conf) Edit() error

Edit opens the given configuration the local editor. See fs/file.Edit for more.

func (Conf) Init added in v0.4.0

func (c Conf) Init() error

Init initializes the configuration directory (Dir) for the current user and given application name (Id) using the standard os.UserConfigDir location. The directory is completely removed and new configuration file(s) are created.

Consider placing a confirmation prompt before calling this function when term.IsInteractive is true. Since Init uses fs/{dir,file}.Create you can set the file.DefaultPerms and dir.DefaultPerms if you prefer a different default for your permissions.

Permissions in the fs package are restrictive (0700/0600) by default to allow tokens to be stored within configuration files (as other applications are known to do). Still, saving of critical secrets is not encouraged within any flat configuration file. But anything that a web browser would need to cache in order to operate is appropriate (cookies, session tokens, etc.).

func (Conf) OverWrite added in v0.4.0

func (c Conf) OverWrite(newconf any) error

OverWrite marshals any Go type and overwrites the configuration File in a way that is safe for all callers of OverWrite in this current system for any operating system using go-internal/lockedfile (taken from the to internal project itself, https://github.com/golang/go/issues/33974) but applying the file.DefaultPerms instead of the 0666 Go default.

Example
package main

import (
	"fmt"
	"os"

	config "github.com/rwxrob/config/pkg"
	"github.com/rwxrob/fs/dir"
)

func main() {

	c := config.Conf{Id: `foo`, Dir: `testdata`, File: `config.yaml`}

	thing := struct {
		Some  string
		Other string
	}{"some", "other"}

	if err := c.OverWrite(thing); err != nil {
		fmt.Println(err)
	}

	dir.Create(`testdata/foo`)
	defer os.RemoveAll(`testdata/foo`)

	if err := c.OverWrite(thing); err != nil {
		fmt.Println(err)
	}
	c.Print()

}
Output:

some: some
other: other

func (Conf) Path added in v0.4.0

func (c Conf) Path() string

Path returns the combined Dir and File.

func (Conf) Print added in v0.4.0

func (c Conf) Print()

Print prints the Data to standard output with an additional line return.

func (Conf) Query added in v0.4.0

func (c Conf) Query(q string) string

Query returns a YAML string matching the given yq query for the given configuration and strips any initial or trailing white space (usually just the single new line at then added by yq). Currently, this function is implemented by calling rwxrob/yq. Will log and return empty string if error. Note that the yqlib usually adds a line to the end.

func (Conf) QueryPrint added in v0.4.0

func (c Conf) QueryPrint(q string)

QueryPrint prints the output of Query.

Jump to

Keyboard shortcuts

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