From 14b3d91191b914571bdf45765a2adf73464ec555 Mon Sep 17 00:00:00 2001 From: Alejandro Mery Date: Sun, 29 Oct 2023 01:04:54 +0000 Subject: [PATCH 1/3] cluster: introduce Cluster.RemoveFile() Signed-off-by: Alejandro Mery --- pkg/cluster/cluster_file.go | 15 +++++++++++++++ pkg/cluster/machine_file.go | 9 +-------- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/pkg/cluster/cluster_file.go b/pkg/cluster/cluster_file.go index e1deb11..c4f5ac7 100644 --- a/pkg/cluster/cluster_file.go +++ b/pkg/cluster/cluster_file.go @@ -40,6 +40,21 @@ func (m *Cluster) openWriter(name string, flags int, args ...any) (io.WriteClose panic("unreachable") } +// RemoveFile deletes a file from the cluster's config directory +func (m *Cluster) RemoveFile(name string, args ...any) error { + if len(args) > 0 { + name = fmt.Sprintf(name, args...) + } + + err := fs.Remove(m.dir, name) + switch { + case os.IsNotExist(err): + return nil + default: + return err + } +} + // ReadFile reads a file from the cluster's config directory func (m *Cluster) ReadFile(name string, args ...any) ([]byte, error) { if len(args) > 0 { diff --git a/pkg/cluster/machine_file.go b/pkg/cluster/machine_file.go index 40dde3a..1b2b3ae 100644 --- a/pkg/cluster/machine_file.go +++ b/pkg/cluster/machine_file.go @@ -43,16 +43,9 @@ func (m *Machine) openWriter(name string, flags int, args ...any) (io.WriteClose // RemoveFile deletes a file from the machine's config directory func (m *Machine) RemoveFile(name string, args ...any) error { - base := m.zone.zones.dir fullName := m.getFilename(name, args...) - err := fs.Remove(base, fullName) - switch { - case os.IsNotExist(err): - return nil - default: - return err - } + return m.zone.zones.RemoveFile(fullName) } // ReadFile reads a file from the machine's config directory From 2fd5947f1b8834387a4a3175699c88159f017340 Mon Sep 17 00:00:00 2001 From: Alejandro Mery Date: Sun, 29 Oct 2023 01:02:19 +0000 Subject: [PATCH 2/3] cluster: introduce Cluster.WriteStringFile() Signed-off-by: Alejandro Mery --- pkg/cluster/cluster_file.go | 14 ++++++++++++++ pkg/cluster/machine_file.go | 11 ++--------- 2 files changed, 16 insertions(+), 9 deletions(-) diff --git a/pkg/cluster/cluster_file.go b/pkg/cluster/cluster_file.go index c4f5ac7..c5277be 100644 --- a/pkg/cluster/cluster_file.go +++ b/pkg/cluster/cluster_file.go @@ -1,6 +1,7 @@ package cluster import ( + "bytes" "fmt" "io" "os" @@ -63,3 +64,16 @@ func (m *Cluster) ReadFile(name string, args ...any) ([]byte, error) { return fs.ReadFile(m.dir, name) } + +// WriteStringFile writes the given content to a file on the machine's config directory +func (m *Cluster) WriteStringFile(value string, name string, args ...any) error { + f, err := m.CreateTruncFile(name, args...) + if err != nil { + return err + } + defer f.Close() + + buf := bytes.NewBufferString(value) + _, err = buf.WriteTo(f) + return err +} diff --git a/pkg/cluster/machine_file.go b/pkg/cluster/machine_file.go index 1b2b3ae..0dff58e 100644 --- a/pkg/cluster/machine_file.go +++ b/pkg/cluster/machine_file.go @@ -1,7 +1,6 @@ package cluster import ( - "bytes" "fmt" "io" "os" @@ -58,15 +57,9 @@ func (m *Machine) ReadFile(name string, args ...any) ([]byte, error) { // WriteStringFile writes the given content to a file on the machine's config directory func (m *Machine) WriteStringFile(value string, name string, args ...any) error { - f, err := m.CreateTruncFile(name, args...) - if err != nil { - return err - } - defer f.Close() + fullName := m.getFilename(name, args...) - buf := bytes.NewBufferString(value) - _, err = buf.WriteTo(f) - return err + return m.zone.zones.WriteStringFile(value, fullName) } func (m *Machine) getFilename(name string, args ...any) string { From fd1c57d37729c2c54859aa2b862a955be861bd74 Mon Sep 17 00:00:00 2001 From: Alejandro Mery Date: Sun, 29 Oct 2023 18:37:53 +0000 Subject: [PATCH 3/3] cluster: introduce Cluster.ReadLines() reading a file, splitting and trimming lines, and allowing # to comment a line out Signed-off-by: Alejandro Mery --- pkg/cluster/cluster_file.go | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/pkg/cluster/cluster_file.go b/pkg/cluster/cluster_file.go index c5277be..0d885e1 100644 --- a/pkg/cluster/cluster_file.go +++ b/pkg/cluster/cluster_file.go @@ -1,10 +1,12 @@ package cluster import ( + "bufio" "bytes" "fmt" "io" "os" + "strings" fs "github.com/hack-pad/hackpadfs" ) @@ -65,6 +67,31 @@ func (m *Cluster) ReadFile(name string, args ...any) ([]byte, error) { return fs.ReadFile(m.dir, name) } +// ReadLines reads a file from the cluster's config directory, +// split by lines, trimmed, and accepting `#` to comment lines out. +func (m *Cluster) ReadLines(name string, args ...any) ([]string, error) { + var out []string + + data, err := m.ReadFile(name, args...) + if err != nil { + return nil, err + } + + sc := bufio.NewScanner(bytes.NewReader(data)) + for sc.Scan() { + s := strings.TrimSpace(sc.Text()) + switch { + case s == "", strings.HasPrefix(s, "#"): + // ignore + default: + // accepted + out = append(out, s) + } + } + + return out, nil +} + // WriteStringFile writes the given content to a file on the machine's config directory func (m *Cluster) WriteStringFile(value string, name string, args ...any) error { f, err := m.CreateTruncFile(name, args...)