Compare commits

...

2 Commits

Author SHA1 Message Date
amery 78f7c31c9a cluster: introduce Cluster.WriteStringFile()
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-10-29 01:13:45 +00:00
amery 67d06084b5 cluster: introduce Cluster.RemoveFile()
Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-10-29 01:13:45 +00:00
2 changed files with 32 additions and 17 deletions
+29
View File
@@ -1,6 +1,7 @@
package cluster
import (
"bytes"
"fmt"
"io"
"os"
@@ -40,6 +41,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 {
@@ -49,6 +65,19 @@ 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
}
// MkdirAll creates directories relative to the cluster's config directory
func (m *Cluster) MkdirAll(name string, args ...any) error {
if len(args) > 0 {
+3 -17
View File
@@ -1,7 +1,6 @@
package cluster
import (
"bytes"
"fmt"
"io"
"os"
@@ -42,16 +41,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
@@ -63,15 +55,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)
}
// MkdirAll creates directories relative to the machine's config directory