Browse Source

Merge pull request 'zones: add methods to work with files at the root of `m/`' (#6)

Reviewed-on: #6
pull/7/head v0.4.17
Alejandro Mery 1 year ago
parent
commit
6f5ca3f235
  1. 46
      pkg/zones/zones_file.go

46
pkg/zones/zones_file.go

@ -0,0 +1,46 @@
package zones
import (
"fmt"
"io"
"os"
fs "github.com/hack-pad/hackpadfs"
)
// OpenFile opens a file on the cluster's config directory with the specified flags
func (m *Zones) OpenFile(name string, flags int, args ...any) (fs.File, error) {
if len(args) > 0 {
name = fmt.Sprintf(name, args...)
}
return fs.OpenFile(m.dir, name, flags, 0644)
}
// CreateTruncFile creates or truncates a file on the cluster's config directory
func (m *Zones) 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 cluster's config directory
func (m *Zones) CreateFile(name string, args ...any) (io.WriteCloser, error) {
return m.openWriter(name, os.O_CREATE, args...)
}
func (m *Zones) 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
}
// ReadFile reads a file from the cluster's config directory
func (m *Zones) ReadFile(name string, args ...any) ([]byte, error) {
if len(args) > 0 {
name = fmt.Sprintf(name, args...)
}
return fs.ReadFile(m.dir, name)
}
Loading…
Cancel
Save