Files
jpictl/pkg/cluster/cluster_file.go
T
amery 986db350b4 cluster: make unreachable panic explicit
making revive 1.3.4 happy

Signed-off-by: Alejandro Mery <amery@jpi.io>
2023-10-23 20:55:28 +00:00

51 lines
1.3 KiB
Go

package cluster
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 *Cluster) 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 *Cluster) 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 *Cluster) CreateFile(name string, args ...any) (io.WriteCloser, error) {
return m.openWriter(name, os.O_CREATE, args...)
}
func (m *Cluster) 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
}
if f, ok := f.(io.WriteCloser); ok {
return f, nil
}
panic("unreachable")
}
// ReadFile reads a file from the cluster's config directory
func (m *Cluster) ReadFile(name string, args ...any) ([]byte, error) {
if len(args) > 0 {
name = fmt.Sprintf(name, args...)
}
return fs.ReadFile(m.dir, name)
}