isle/go/cmd/entrypoint/bootstrap_util.go

48 lines
976 B
Go
Raw Normal View History

2022-10-26 22:23:39 +00:00
package main
import (
"errors"
"fmt"
"io/fs"
"isle/bootstrap"
2022-10-26 22:23:39 +00:00
"os"
"path/filepath"
)
2022-10-26 22:37:03 +00:00
func loadHostBootstrap() (bootstrap.Bootstrap, error) {
2022-10-26 22:23:39 +00:00
2022-10-26 22:37:03 +00:00
dataDirPath := bootstrap.DataDirPath(envDataDirPath)
2022-10-26 22:23:39 +00:00
hostBootstrap, err := bootstrap.FromFile(dataDirPath)
if errors.Is(err, fs.ErrNotExist) {
return bootstrap.Bootstrap{}, fmt.Errorf(
"%q not found, has the daemon ever been run?",
dataDirPath,
)
2022-10-26 22:23:39 +00:00
} else if err != nil {
return bootstrap.Bootstrap{}, fmt.Errorf("loading %q: %w", dataDirPath, err)
}
return hostBootstrap, nil
}
2022-10-26 22:37:03 +00:00
func writeBootstrapToDataDir(hostBootstrap bootstrap.Bootstrap) error {
2022-10-26 22:23:39 +00:00
2022-10-26 22:37:03 +00:00
path := bootstrap.DataDirPath(envDataDirPath)
2022-10-26 22:23:39 +00:00
dirPath := filepath.Dir(path)
if err := os.MkdirAll(dirPath, 0700); err != nil {
return fmt.Errorf("creating directory %q: %w", dirPath, err)
}
f, err := os.Create(path)
if err != nil {
return fmt.Errorf("creating file %q: %w", path, err)
}
defer f.Close()
return hostBootstrap.WriteTo(f)
}