43 lines
1002 B
Go
43 lines
1002 B
Go
package network
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"io/fs"
|
|
"isle/daemon/daecommon"
|
|
"isle/jsonutil"
|
|
"isle/toolkit"
|
|
"path/filepath"
|
|
)
|
|
|
|
// loadStoreConfig writes the given NetworkConfig to the networkStateDir if the
|
|
// config is non-nil. If the config is nil then a config is read from
|
|
// networkStateDir, returning the zero value if no config was previously
|
|
// stored.
|
|
func loadStoreConfig(
|
|
networkStateDir toolkit.Dir, config *daecommon.NetworkConfig,
|
|
) (
|
|
daecommon.NetworkConfig, error,
|
|
) {
|
|
path := filepath.Join(networkStateDir.Path, "config.json")
|
|
|
|
if config == nil {
|
|
config = new(daecommon.NetworkConfig)
|
|
err := jsonutil.LoadFile(&config, path)
|
|
if err != nil && !errors.Is(err, fs.ErrNotExist) {
|
|
return daecommon.NetworkConfig{}, fmt.Errorf(
|
|
"loading %q: %w", path, err,
|
|
)
|
|
}
|
|
return *config, nil
|
|
}
|
|
|
|
if err := jsonutil.WriteFile(*config, path, 0600); err != nil {
|
|
return daecommon.NetworkConfig{}, fmt.Errorf(
|
|
"writing to %q: %w", path, err,
|
|
)
|
|
}
|
|
|
|
return *config, nil
|
|
}
|