From 35998120727ed81ddd51dfa57d4a05d7c72aabcb Mon Sep 17 00:00:00 2001 From: Alejandro Mery Date: Mon, 21 Aug 2023 22:53:48 +0000 Subject: [PATCH] zones: introduce Machine.ReadFile() Signed-off-by: Alejandro Mery --- pkg/zones/machine.go | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/pkg/zones/machine.go b/pkg/zones/machine.go index ca863e3..752e6ad 100644 --- a/pkg/zones/machine.go +++ b/pkg/zones/machine.go @@ -1,7 +1,10 @@ package zones import ( + "fmt" + "io/fs" "net/netip" + "path/filepath" "strconv" "strings" "sync" @@ -56,3 +59,25 @@ func (m *Machine) FullName() string { return m.Name } + +// 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) +} + +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...) +}