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.
47 lines
1.2 KiB
47 lines
1.2 KiB
12 months ago
|
package cluster
|
||
1 year ago
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"io"
|
||
|
"os"
|
||
|
|
||
|
fs "github.com/hack-pad/hackpadfs"
|
||
|
)
|
||
|
|
||
|
// OpenFile opens a file on the cluster's config directory with the specified flags
|
||
12 months ago
|
func (m *Cluster) OpenFile(name string, flags int, args ...any) (fs.File, error) {
|
||
1 year ago
|
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
|
||
12 months ago
|
func (m *Cluster) CreateTruncFile(name string, args ...any) (io.WriteCloser, error) {
|
||
1 year ago
|
return m.openWriter(name, os.O_CREATE|os.O_TRUNC, args...)
|
||
|
}
|
||
|
|
||
|
// CreateFile creates a file on the cluster's config directory
|
||
12 months ago
|
func (m *Cluster) CreateFile(name string, args ...any) (io.WriteCloser, error) {
|
||
1 year ago
|
return m.openWriter(name, os.O_CREATE, args...)
|
||
|
}
|
||
|
|
||
12 months ago
|
func (m *Cluster) openWriter(name string, flags int, args ...any) (io.WriteCloser, error) {
|
||
1 year ago
|
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
|
||
12 months ago
|
func (m *Cluster) ReadFile(name string, args ...any) ([]byte, error) {
|
||
1 year ago
|
if len(args) > 0 {
|
||
|
name = fmt.Sprintf(name, args...)
|
||
|
}
|
||
|
|
||
|
return fs.ReadFile(m.dir, name)
|
||
|
}
|