You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
88 lines
2.1 KiB
88 lines
2.1 KiB
12 months ago
|
package cluster
|
||
1 year ago
|
|
||
|
import (
|
||
1 year ago
|
"bytes"
|
||
1 year ago
|
"fmt"
|
||
1 year ago
|
"io"
|
||
1 year ago
|
"os"
|
||
1 year ago
|
"path/filepath"
|
||
1 year ago
|
|
||
|
fs "github.com/hack-pad/hackpadfs"
|
||
1 year ago
|
)
|
||
|
|
||
1 year ago
|
// OpenFile opens a file on the machine's config directory with the specified flags
|
||
|
func (m *Machine) OpenFile(name string, flags int, args ...any) (fs.File, error) {
|
||
|
base := m.zone.zones.dir
|
||
|
fullName := m.getFilename(name, args...)
|
||
|
|
||
|
return fs.OpenFile(base, fullName, flags, 0644)
|
||
|
}
|
||
|
|
||
1 year ago
|
// CreateTruncFile creates or truncates a file on the machine's config directory
|
||
|
func (m *Machine) CreateTruncFile(name string, args ...any) (io.WriteCloser, error) {
|
||
|
return m.openWriter(name, os.O_CREATE|os.O_TRUNC, args...)
|
||
|
}
|
||
|
|
||
|
// CreateFile creates a file on the machine's config directory
|
||
|
func (m *Machine) CreateFile(name string, args ...any) (io.WriteCloser, error) {
|
||
|
return m.openWriter(name, os.O_CREATE, args...)
|
||
|
}
|
||
|
|
||
|
func (m *Machine) openWriter(name string, flags int, args ...any) (io.WriteCloser, error) {
|
||
|
f, err := m.OpenFile(name, os.O_WRONLY|flags, args...)
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
return f.(io.WriteCloser), nil
|
||
|
}
|
||
|
|
||
1 year ago
|
// 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
|
||
|
}
|
||
|
}
|
||
|
|
||
1 year ago
|
// ReadFile reads a file from the machine's config directory
|
||
|
func (m *Machine) ReadFile(name string, args ...any) ([]byte, error) {
|
||
|
base := m.zone.zones.dir
|
||
|
fullName := m.getFilename(name, args...)
|
||
|
|
||
|
return fs.ReadFile(base, fullName)
|
||
|
}
|
||
|
|
||
1 year ago
|
// 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()
|
||
|
|
||
|
buf := bytes.NewBufferString(value)
|
||
|
_, err = buf.WriteTo(f)
|
||
|
return err
|
||
|
}
|
||
|
|
||
1 year ago
|
func (m *Machine) getFilename(name string, args ...any) string {
|
||
|
if len(args) > 0 {
|
||
|
name = fmt.Sprintf(name, args...)
|
||
|
}
|
||
|
|
||
|
s := []string{
|
||
|
m.zone.Name,
|
||
|
m.Name,
|
||
|
name,
|
||
|
}
|
||
|
|
||
|
return filepath.Join(s...)
|
||
|
}
|