~pkal/dirconf

5067334a87eb9bb5c80398557f0c9d7c2b1f8636 — Philip K 2 years ago c1de357
fixed compilation issues
1 files changed, 31 insertions(+), 19 deletions(-)

M dirconf.go
M dirconf.go => dirconf.go +31 -19
@@ 27,7 27,7 @@ var (

// Option is any value that
type Option interface {
	Set(c *Conf, r io.Reader) error
	Set(c *Conf, file string, r io.Reader) error
}

// PathOption is a Option that can offer a relative file that it's value


@@ 38,6 38,7 @@ type PathOption interface {
	Path() []string
}

// Conf is a list of Options, each which will be processed when parsed
type Conf []Option

func (c *Conf) WriteSchema(dir string) error {


@@ 48,7 49,7 @@ func (c *Conf) WriteSchema(dir string) error {
	}
	defer file.Close()

	for _, opt := range c {
	for _, opt := range *c {
		po, ok := opt.(PathOption)
		if !ok {
			continue


@@ 74,7 75,7 @@ func (c *Conf) Parse() error {
		dir = filepath.Join(me.HomeDir, ".config")
	}

	return c.ParseIn(filepath.Joinc(dir, name))
	return c.ParseIn(filepath.Join(dir, name))
}

func (c *Conf) ParseIn(dir string) error {


@@ 88,18 89,19 @@ func (c *Conf) ParseIn(dir string) error {
		return err
	}

	for _, opt := range c {
		if po, ok := opt.(PathOption); ok {
			fpath := append([]string{dir}, po.Path()...)
			file, err := os.Open(filepath.Join(fpath...))
	for _, opt := range *c {
		po, ok := opt.(PathOption)
		if ok {
			fpath := filepath.Join(append([]string{dir}, po.Path()...)...)
			file, err := os.Open(fpath)
			if err != nil {
				opt.Set(c, nil)
				opt.Set(c, fpath, nil)
			} else {
				opt.Set(c, file)
				opt.Set(c, fpath, file)
				file.Close()
			}
		} else {
			opt.Set(c, nil)
			opt.Set(c, "", nil)
		}
	}



@@ 126,7 128,7 @@ func (o *Int) Name() string { return "int" }
func (o *Int) Path() []string { return strings.Split(o.Key, "/") }

// Set is called when the value of a Int is processed
func (o *Int) Set(c *Conf, r io.Reader) error {
func (o *Int) Set(c *Conf, file string, r io.Reader) error {
	var val int
	if r == nil {
		if o.Must {


@@ 175,7 177,7 @@ func (o *Float) Name() string { return "float" }
func (o *Float) Path() []string { return strings.Split(o.Key, "/") }

// Set is called when the value of a Int is processed
func (o *Float) Set(c *Conf, r io.Reader) error {
func (o *Float) Set(c *Conf, file string, r io.Reader) error {
	var val float64
	if r == nil {
		if o.Must {


@@ 222,7 224,7 @@ func (o *String) Name() string { return "string" }
func (o *String) Path() []string { return strings.Split(o.Key, "/") }

// Set is called when the value of a Int is processed
func (o *String) Set(c *Conf, r io.Reader) error {
func (o *String) Set(c *Conf, file string, r io.Reader) error {
	var val string
	if r == nil {
		if o.Must {


@@ 255,6 257,7 @@ type List struct {
	Default []string
	Must    bool
	OnSet   func([]string)
	Key     string
}

// Name returns the name of the option type


@@ 264,7 267,7 @@ func (o *List) Name() string { return "list" }
func (o *List) Path() []string { return strings.Split(o.Key, "/") }

// Set is called when the value of a Int is processed
func (o *List) Set(c *Conf, r io.Reader) error {
func (o *List) Set(c *Conf, file string, r io.Reader) error {
	var val []string
	if r == nil {
		if o.Must {


@@ 310,7 313,7 @@ func (o *Bool) Name() string { return "boolean" }
func (o *Bool) Path() []string { return strings.Split(o.Key, "/") }

// Set is called when the value of a Int is processed
func (o *Bool) Set(c *Conf, r io.Reader) error {
func (o *Bool) Set(c *Conf, file string, r io.Reader) error {
	if o.Ptr != nil {
		*o.Ptr = r == nil
	}


@@ 337,7 340,7 @@ func (f *Func) Name() string { return "file" }
func (f *Func) Path() []string { return strings.Split(f.Key, "/") }

// Set is called when the value of a Int is processed
func (f *Func) Set(c *Conf, r io.Reader) error {
func (f *Func) Set(c *Conf, file string, r io.Reader) error {
	return f.Func(r)
}



@@ 348,14 351,23 @@ func (f *Func) Set(c *Conf, r io.Reader) error {
// Path is an option for a to get the path
type Path struct {
	Ptr   *string
	OnSet func([]string)
	OnSet func(string)
	Key   string
}

// Path returns the relative file of this option
func (o *Path) Path() []string { return strings.Split(o.Key, "/") }

// Set is called when the value of a Int is processed
func (o *Path) Set(c *Conf, r io.Reader) error {
	path := append([]string{c.path}, strings.Split(o.Key, "/")...)
func (o *Path) Set(c *Conf, file string, r io.Reader) error {
	path := append([]string{file}, strings.Split(o.Key, "/")...)
	val := filepath.Join(path...)
	if o.Ptr != nil {
		*o.Ptr = val
	}
	if o.OnSet != nil {
		o.OnSet(val)
	}
	return nil

}