Browse Source

Merge pull request 'cluster: add top level ReadLines(), WriteStringFile() and RemoveFile() helpers' (#36)

Reviewed-on: #36
pull/33/head
Alejandro Mery 1 year ago
parent
commit
0ff17abd59
  1. 56
      pkg/cluster/cluster_file.go
  2. 20
      pkg/cluster/machine_file.go

56
pkg/cluster/cluster_file.go

@ -1,9 +1,12 @@
package cluster package cluster
import ( import (
"bufio"
"bytes"
"fmt" "fmt"
"io" "io"
"os" "os"
"strings"
fs "github.com/hack-pad/hackpadfs" fs "github.com/hack-pad/hackpadfs"
) )
@ -40,6 +43,21 @@ func (m *Cluster) openWriter(name string, flags int, args ...any) (io.WriteClose
panic("unreachable") 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 // ReadFile reads a file from the cluster's config directory
func (m *Cluster) ReadFile(name string, args ...any) ([]byte, error) { func (m *Cluster) ReadFile(name string, args ...any) ([]byte, error) {
if len(args) > 0 { if len(args) > 0 {
@ -48,3 +66,41 @@ func (m *Cluster) ReadFile(name string, args ...any) ([]byte, error) {
return fs.ReadFile(m.dir, name) 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...)
if err != nil {
return err
}
defer f.Close()
buf := bytes.NewBufferString(value)
_, err = buf.WriteTo(f)
return err
}

20
pkg/cluster/machine_file.go

@ -1,7 +1,6 @@
package cluster package cluster
import ( import (
"bytes"
"fmt" "fmt"
"io" "io"
"os" "os"
@ -43,16 +42,9 @@ func (m *Machine) openWriter(name string, flags int, args ...any) (io.WriteClose
// RemoveFile deletes a file from the machine's config directory // RemoveFile deletes a file from the machine's config directory
func (m *Machine) RemoveFile(name string, args ...any) error { func (m *Machine) RemoveFile(name string, args ...any) error {
base := m.zone.zones.dir
fullName := m.getFilename(name, args...) fullName := m.getFilename(name, args...)
err := fs.Remove(base, fullName)
switch { return m.zone.zones.RemoveFile(fullName)
case os.IsNotExist(err):
return nil
default:
return err
}
} }
// ReadFile reads a file from the machine's config directory // ReadFile reads a file from the machine's config directory
@ -65,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 // 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 { func (m *Machine) WriteStringFile(value string, name string, args ...any) error {
f, err := m.CreateTruncFile(name, args...) fullName := m.getFilename(name, args...)
if err != nil {
return err
}
defer f.Close()
buf := bytes.NewBufferString(value) return m.zone.zones.WriteStringFile(value, fullName)
_, err = buf.WriteTo(f)
return err
} }
func (m *Machine) getFilename(name string, args ...any) string { func (m *Machine) getFilename(name string, args ...any) string {

Loading…
Cancel
Save