From b40723b0b8d482140c5152b22233c1b62ec7838d Mon Sep 17 00:00:00 2001 From: Julien Dessaux Date: Mon, 11 Mar 2024 23:19:13 +0100 Subject: chore(variables): support integer interpolation for all values --- pkg/permissions.go | 19 ++++++------------- pkg/templates.go | 5 ++++- pkg/values.go | 15 +++++++++++++-- pkg/variables.go | 3 +++ 4 files changed, 26 insertions(+), 16 deletions(-) diff --git a/pkg/permissions.go b/pkg/permissions.go index 2bd73a9..3710e68 100644 --- a/pkg/permissions.go +++ b/pkg/permissions.go @@ -37,14 +37,9 @@ func (p *Permissions) resolve(filename string) (Status, error) { } } } - m, ok := p.mode.(*IntValue) - if !ok { - if i, err := strconv.Atoi(p.mode.String()); err != nil { - return BROKEN, err - } else { - m = &IntValue{i} - p.mode = m - } + m, err := p.mode.Int() + if err != nil { + return BROKEN, err } u, ok := p.user.(*IntValue) if !ok { @@ -63,9 +58,9 @@ func (p *Permissions) resolve(filename string) (Status, error) { if fileInfo, err := os.Lstat(filename); err != nil { return BROKEN, err } else { - gv := g.Int() - mv := fs.FileMode(m.Int()) - uv := u.Int() + gv, _ := g.Int() + mv := fs.FileMode(m) + uv, _ := u.Int() if fileInfo.Mode() != mv { if err := os.Chmod(filename, mv); err != nil { return BROKEN, err @@ -82,8 +77,6 @@ func (p *Permissions) resolve(filename string) (Status, error) { } else { return BROKEN, errors.New("Unsupported operating system") } - _ = gv - _ = uv } return status, nil } diff --git a/pkg/templates.go b/pkg/templates.go index 0b7e11b..736bbe0 100644 --- a/pkg/templates.go +++ b/pkg/templates.go @@ -3,6 +3,7 @@ package gonf import ( "bytes" "log/slog" + "strconv" "text/template" ) @@ -38,7 +39,9 @@ func (t *TemplateValue) Bytes() []byte { } return t.contents } - +func (t TemplateValue) Int() (int, error) { + return strconv.Atoi(t.String()) +} func (t TemplateValue) String() string { return string(t.Bytes()[:]) } diff --git a/pkg/values.go b/pkg/values.go index 509b6bf..bb82787 100644 --- a/pkg/values.go +++ b/pkg/values.go @@ -3,11 +3,13 @@ package gonf import ( "fmt" "log/slog" + "strconv" "strings" ) type Value interface { Bytes() []byte + Int() (int, error) String() string } @@ -53,6 +55,9 @@ type BytesValue struct { func (b BytesValue) Bytes() []byte { return b.value } +func (b BytesValue) Int() (int, error) { + return strconv.Atoi(string(b.value)) +} func (b BytesValue) String() string { return string(b.value[:]) } @@ -65,8 +70,8 @@ type IntValue struct { func (i IntValue) Bytes() []byte { return []byte(string(i.value)) } -func (i IntValue) Int() int { - return i.value +func (i IntValue) Int() (int, error) { + return i.value, nil } func (i IntValue) String() string { return string(i.value) @@ -83,6 +88,9 @@ func (s *StringsListValue) Append(v ...string) { func (s StringsListValue) Bytes() []byte { return []byte(s.String()) } +func (s StringsListValue) Int() (int, error) { + return len(s.value), nil +} func (s StringsListValue) String() string { return strings.Join(s.value, "\n") } @@ -95,6 +103,9 @@ type StringValue struct { func (s StringValue) Bytes() []byte { return []byte(s.value) } +func (s StringValue) Int() (int, error) { + return strconv.Atoi(s.value) +} func (s StringValue) String() string { return s.value } diff --git a/pkg/variables.go b/pkg/variables.go index ed09d31..bb3096d 100644 --- a/pkg/variables.go +++ b/pkg/variables.go @@ -70,6 +70,9 @@ type VariablePromise struct { func (s VariablePromise) Bytes() []byte { return s.value.Bytes() } +func (s VariablePromise) Int() (int, error) { + return s.value.Int() +} func (s VariablePromise) String() string { return s.value.String() } -- cgit v1.2.3