48 lines
976 B
Go
48 lines
976 B
Go
package main
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"io/fs"
|
|
"isle/bootstrap"
|
|
"os"
|
|
"path/filepath"
|
|
)
|
|
|
|
func loadHostBootstrap() (bootstrap.Bootstrap, error) {
|
|
|
|
dataDirPath := bootstrap.DataDirPath(envDataDirPath)
|
|
|
|
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,
|
|
)
|
|
|
|
} else if err != nil {
|
|
return bootstrap.Bootstrap{}, fmt.Errorf("loading %q: %w", dataDirPath, err)
|
|
}
|
|
|
|
return hostBootstrap, nil
|
|
}
|
|
|
|
func writeBootstrapToDataDir(hostBootstrap bootstrap.Bootstrap) error {
|
|
|
|
path := bootstrap.DataDirPath(envDataDirPath)
|
|
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)
|
|
}
|