diff --git a/cmd/hhfab/main.go b/cmd/hhfab/main.go index 6f166c77..0d86a97e 100644 --- a/cmd/hhfab/main.go +++ b/cmd/hhfab/main.go @@ -63,7 +63,7 @@ func main() { Destination: &brief, } - var basedir, preset, wiring, wiringGenType, wiringGenPreset string + var basedir, fromConfig, preset, wiring, wiringGenType, wiringGenPreset string basedirFlag := &cli.StringFlag{ Name: "basedir", Aliases: []string{"d"}, @@ -111,6 +111,12 @@ func main() { basedirFlag, verboseFlag, briefFlag, + &cli.StringFlag{ + Name: "config", + Aliases: []string{"c"}, + Usage: "start from existing config `FILE`", + Destination: &fromConfig, + }, &cli.StringFlag{ Name: "preset", Aliases: []string{"p"}, @@ -137,13 +143,6 @@ func main() { // Usage: "use wiring diagram sample preset (one of: " + strings.Join(samplePresets, ", ") + ")", // Destination: &wiringGenPreset, // }, - // TODO support loading from existing config - // &cli.StringFlag{ - // Name: "config", - // Aliases: []string{"c"}, - // Usage: "start from existing config `FILE`", - // Destination: &config, - // }, // TODO support reset before init, is it really needed? // &cli.BoolFlag{ // Name: "reset", @@ -155,7 +154,7 @@ func main() { return setupLogger(verbose, brief) }, Action: func(cCtx *cli.Context) error { - err := mngr.Init(basedir, cnc.Preset(preset), wiring, wiringGenType, wiringGenPreset) + err := mngr.Init(basedir, fromConfig, cnc.Preset(preset), wiring, wiringGenType, wiringGenPreset) if err != nil { return errors.Wrap(err, "error initializing") } diff --git a/pkg/fab/cnc/manager.go b/pkg/fab/cnc/manager.go index 454a1834..da71876a 100644 --- a/pkg/fab/cnc/manager.go +++ b/pkg/fab/cnc/manager.go @@ -10,7 +10,6 @@ import ( "regexp" "time" - "github.com/davecgh/go-spew/spew" "github.com/mholt/archiver/v4" "github.com/pkg/errors" "github.com/urfave/cli/v2" @@ -124,13 +123,8 @@ func (mngr *Manager) prepare() error { return nil } -func (mngr *Manager) Init(basedir string, preset Preset, wiringPath string, wiringGenType string, wiringGenPreset string) error { +func (mngr *Manager) Init(basedir string, fromConfig string, preset Preset, wiringPath string, wiringGenType string, wiringGenPreset string) error { mngr.basedir = basedir - mngr.preset = preset - - if !slices.Contains(mngr.presets, preset) { - return fmt.Errorf("unknown preset: %s", preset) - } if wiringGenType == "" { wiringGenType = "collapsedcore" @@ -175,6 +169,24 @@ func (mngr *Manager) Init(basedir string, preset Preset, wiringPath string, wiri return errors.New("wiring path or wiring gen must be specified") } + if fromConfig != "" { + slog.Info("Loading existing config", "from", fromConfig) + err := mngr.loadConfig(fromConfig) + if err != nil { + return errors.Wrapf(err, "error existing loading config") + } + + if mngr.preset != "" && preset != mngr.preset { + return errors.Errorf("loaded config preset mismatch: %s != %s", preset, mngr.preset) + } + } + + mngr.preset = preset + + if !slices.Contains(mngr.presets, preset) { + return fmt.Errorf("unknown preset: %s", preset) + } + err := mngr.prepare() if err != nil { return errors.Wrapf(err, "error preparing") @@ -198,6 +210,25 @@ func (mngr *Manager) Save() error { return errors.Wrapf(err, "error creating basedir %s", mngr.basedir) } + data, err := mngr.configData() + if err != nil { + return errors.Wrapf(err, "error getting config data") + } + + err = os.WriteFile(filepath.Join(mngr.basedir, "config.yaml"), data, 0o644) + if err != nil { + return errors.Wrapf(err, "error writing config") + } + + err = mngr.wiring.SaveTo(filepath.Join(mngr.basedir, "wiring.yaml")) + if err != nil { + return errors.Wrapf(err, "error saving wiring") + } + + return nil +} + +func (mngr *Manager) configData() ([]byte, error) { saver := &ManagerSaver{ Preset: mngr.preset, Config: map[string]any{}, @@ -213,26 +244,14 @@ func (mngr *Manager) Save() error { data, err := yaml.Marshal(saver) if err != nil { - return errors.Wrapf(err, "error marshaling config") - } - - err = os.WriteFile(filepath.Join(mngr.basedir, "config.yaml"), data, 0o644) - if err != nil { - return errors.Wrapf(err, "error writing config") + return nil, errors.Wrapf(err, "error marshaling config") } - err = mngr.wiring.SaveTo(filepath.Join(mngr.basedir, "wiring.yaml")) - if err != nil { - return errors.Wrapf(err, "error saving wiring") - } - - return nil + return data, nil } -func (mngr *Manager) Load(basedir string) error { - mngr.basedir = basedir - - data, err := os.ReadFile(filepath.Join(basedir, "config.yaml")) +func (mngr *Manager) loadConfig(fromConfig string) error { + data, err := os.ReadFile(fromConfig) if err != nil { return errors.Wrapf(err, "error reading config") } @@ -262,6 +281,17 @@ func (mngr *Manager) Load(basedir string) error { } } + return nil +} + +func (mngr *Manager) Load(basedir string) error { + mngr.basedir = basedir + + err := mngr.loadConfig(filepath.Join(basedir, "config.yaml")) + if err != nil { + return errors.Wrapf(err, "error loading config") + } + wiringData, err := wiring.LoadDataFrom(filepath.Join(basedir, "wiring.yaml")) if err != nil { return errors.Wrapf(err, "error loading wiring") @@ -536,9 +566,17 @@ func (adder *opAdder) addRunOp(bundle Bundle, stage Stage, name string, op RunOp } func (mngr *Manager) Dump() error { - slog.Info("Dumping") + slog.Info("Dumping hydrated config") + mngr.wiring = nil - spew.Dump(mngr) + + data, err := mngr.configData() + if err != nil { + return errors.Wrapf(err, "error getting config data") + } + + fmt.Println() + fmt.Println(string(data)) return nil }