chore(variables): support integer interpolation for all values

This commit is contained in:
Julien Dessaux 2024-03-11 23:19:13 +01:00
parent acbccb0a93
commit b40723b0b8
Signed by: adyxax
GPG key ID: F92E51B86E07177E
4 changed files with 26 additions and 16 deletions

View file

@ -37,14 +37,9 @@ func (p *Permissions) resolve(filename string) (Status, error) {
} }
} }
} }
m, ok := p.mode.(*IntValue) m, err := p.mode.Int()
if !ok { if err != nil {
if i, err := strconv.Atoi(p.mode.String()); err != nil { return BROKEN, err
return BROKEN, err
} else {
m = &IntValue{i}
p.mode = m
}
} }
u, ok := p.user.(*IntValue) u, ok := p.user.(*IntValue)
if !ok { if !ok {
@ -63,9 +58,9 @@ func (p *Permissions) resolve(filename string) (Status, error) {
if fileInfo, err := os.Lstat(filename); err != nil { if fileInfo, err := os.Lstat(filename); err != nil {
return BROKEN, err return BROKEN, err
} else { } else {
gv := g.Int() gv, _ := g.Int()
mv := fs.FileMode(m.Int()) mv := fs.FileMode(m)
uv := u.Int() uv, _ := u.Int()
if fileInfo.Mode() != mv { if fileInfo.Mode() != mv {
if err := os.Chmod(filename, mv); err != nil { if err := os.Chmod(filename, mv); err != nil {
return BROKEN, err return BROKEN, err
@ -82,8 +77,6 @@ func (p *Permissions) resolve(filename string) (Status, error) {
} else { } else {
return BROKEN, errors.New("Unsupported operating system") return BROKEN, errors.New("Unsupported operating system")
} }
_ = gv
_ = uv
} }
return status, nil return status, nil
} }

View file

@ -3,6 +3,7 @@ package gonf
import ( import (
"bytes" "bytes"
"log/slog" "log/slog"
"strconv"
"text/template" "text/template"
) )
@ -38,7 +39,9 @@ func (t *TemplateValue) Bytes() []byte {
} }
return t.contents return t.contents
} }
func (t TemplateValue) Int() (int, error) {
return strconv.Atoi(t.String())
}
func (t TemplateValue) String() string { func (t TemplateValue) String() string {
return string(t.Bytes()[:]) return string(t.Bytes()[:])
} }

View file

@ -3,11 +3,13 @@ package gonf
import ( import (
"fmt" "fmt"
"log/slog" "log/slog"
"strconv"
"strings" "strings"
) )
type Value interface { type Value interface {
Bytes() []byte Bytes() []byte
Int() (int, error)
String() string String() string
} }
@ -53,6 +55,9 @@ type BytesValue struct {
func (b BytesValue) Bytes() []byte { func (b BytesValue) Bytes() []byte {
return b.value return b.value
} }
func (b BytesValue) Int() (int, error) {
return strconv.Atoi(string(b.value))
}
func (b BytesValue) String() string { func (b BytesValue) String() string {
return string(b.value[:]) return string(b.value[:])
} }
@ -65,8 +70,8 @@ type IntValue struct {
func (i IntValue) Bytes() []byte { func (i IntValue) Bytes() []byte {
return []byte(string(i.value)) return []byte(string(i.value))
} }
func (i IntValue) Int() int { func (i IntValue) Int() (int, error) {
return i.value return i.value, nil
} }
func (i IntValue) String() string { func (i IntValue) String() string {
return string(i.value) return string(i.value)
@ -83,6 +88,9 @@ func (s *StringsListValue) Append(v ...string) {
func (s StringsListValue) Bytes() []byte { func (s StringsListValue) Bytes() []byte {
return []byte(s.String()) return []byte(s.String())
} }
func (s StringsListValue) Int() (int, error) {
return len(s.value), nil
}
func (s StringsListValue) String() string { func (s StringsListValue) String() string {
return strings.Join(s.value, "\n") return strings.Join(s.value, "\n")
} }
@ -95,6 +103,9 @@ type StringValue struct {
func (s StringValue) Bytes() []byte { func (s StringValue) Bytes() []byte {
return []byte(s.value) return []byte(s.value)
} }
func (s StringValue) Int() (int, error) {
return strconv.Atoi(s.value)
}
func (s StringValue) String() string { func (s StringValue) String() string {
return s.value return s.value
} }

View file

@ -70,6 +70,9 @@ type VariablePromise struct {
func (s VariablePromise) Bytes() []byte { func (s VariablePromise) Bytes() []byte {
return s.value.Bytes() return s.value.Bytes()
} }
func (s VariablePromise) Int() (int, error) {
return s.value.Int()
}
func (s VariablePromise) String() string { func (s VariablePromise) String() string {
return s.value.String() return s.value.String()
} }