~pkal/dirconf

fc0f0707c52c3f7a2fbe4f3d13cb95c04fd9883e — Philip K 2 years ago
initial commit
9 files changed, 189 insertions(+), 0 deletions(-)

A dirconf.go
A duration.go
A file.go
A float32.go
A float64.go
A getbool.go
A int32.go
A int64.go
A string.go
A  => dirconf.go +56 -0
@@ 1,56 @@
package dirconf

import (
	"errors"
	"os"
	"os/user"
	"path"
)

var (
	// ErrNoSuchKey is returned when an invalid key is requested
	ErrNoSuchKey = errors.New("No Such Key")

	// ErrWrongType is returned when the content of a value cannot be
	// cast to the requested type
	ErrWrongType = errors.New("Wrong Type")
)

// Conf represents an open directory configuration
type Conf struct {
	path string
	file *os.File
}

// Open a directory configuration object
func Open(path string) (*Conf, error) {
	var (
		conf = Conf{path: path}
		err  error
	)

	conf.file, err = os.Open(path)
	if err != nil {
		return nil, err
	}

	return &conf, nil
}

// OpenConf a directory configuration object while paying attention to
// XDG variables.
//
// See https://specifications.freedesktop.org/basedir-spec/latest/ar01s03.html
func OpenConf(name string) (*Conf, error) {
	dir := os.Getenv("XDG_CONFIG_HOME")
	if dir == "" {
		me, err := user.Current()
		if err != nil {
			return nil, err
		}

		dir = path.Join(me.HomeDir, ".config")
	}

	return Open(path.Join(dir, name))
}

A  => duration.go +17 -0
@@ 1,17 @@
package dirconf

import "time"

// Duration returns an time.Duration object from the current configuration
func (c *Conf) Duration(key string) (time.Duration, error) {
	str, err := c.String(key)
	if err != nil {
		return 0, err
	}

	d, err := time.ParseDuration(str)
	if err != nil {
		return 0, ErrWrongType
	}
	return d, nil
}

A  => file.go +19 -0
@@ 1,19 @@
package dirconf

import (
	"os"
	"path"
)

// File returns an os.File object from the current configuration
func (c *Conf) File(key ...string) (file *os.File, err error) {
	file, err = os.Open(path.Join(append(key, c.path)...))
	if err != nil {
		if os.IsNotExist(err) {
			return nil, ErrNoSuchKey
		}
		return
	}

	return file, nil
}

A  => float32.go +18 -0
@@ 1,18 @@
package dirconf

import "fmt"

// Float32 returns an Float32 value from the current configuration
func (c *Conf) Float32(key ...string) (float32, error) {
	str, err := c.String(key...)
	if err != nil {
		return 0, err
	}

	var f float32
	_, err = fmt.Sscanf(str, "%f", &f)
	if err != nil {
		return 0, ErrWrongType
	}
	return f, nil
}

A  => float64.go +18 -0
@@ 1,18 @@
package dirconf

import "fmt"

// Float64 returns an Float64 value from the current configuration
func (c *Conf) Float64(key ...string) (float64, error) {
	str, err := c.String(key...)
	if err != nil {
		return 0, err
	}

	var f float64
	_, err = fmt.Sscanf(str, "%f", &f)
	if err != nil {
		return 0, ErrWrongType
	}
	return f, nil
}

A  => getbool.go +8 -0
@@ 1,8 @@
package dirconf

// String returns a String from the current configuration
func (c *Conf) Bool(key ...string) (b bool, err error) {
	file, err := c.File(key...)
	file.Close()
	return err != nil, err
}

A  => int32.go +18 -0
@@ 1,18 @@
package dirconf

import "fmt"

// Int32 returns an Int32eger from the current configuration
func (c *Conf) Int32(key ...string) (int32, error) {
	str, err := c.String(key...)
	if err != nil {
		return 0, err
	}

	var i int32
	_, err = fmt.Sscanf(str, "%d", &i)
	if err != nil {
		return 0, ErrWrongType
	}
	return i, nil
}

A  => int64.go +18 -0
@@ 1,18 @@
package dirconf

import "fmt"

// Int64 returns an Int64eger from the current configuration
func (c *Conf) Int64(key ...string) (int64, error) {
	str, err := c.String(key...)
	if err != nil {
		return 0, err
	}

	var i int64
	_, err = fmt.Sscanf(str, "%d", &i)
	if err != nil {
		return 0, ErrWrongType
	}
	return i, nil
}

A  => string.go +17 -0
@@ 1,17 @@
package dirconf

import (
	"io/ioutil"
)

// GetString returns a String from the current configuration
func (c *Conf) String(key ...string) (string, error) {
	file, err := c.File(key...)
	if err != nil {
		return "", err
	}

	byte, err := ioutil.ReadAll(file)
	file.Close()
	return string(byte), nil
}