Skip to content

Commit

Permalink
Support init from existing config
Browse files Browse the repository at this point in the history
  • Loading branch information
Frostman committed Sep 28, 2023
1 parent f6f1f52 commit eb55992
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 34 deletions.
17 changes: 8 additions & 9 deletions cmd/hhfab/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
Expand Down Expand Up @@ -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"},
Expand All @@ -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",
Expand All @@ -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")
}
Expand Down
88 changes: 63 additions & 25 deletions pkg/fab/cnc/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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")
Expand All @@ -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{},
Expand All @@ -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")
}
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit eb55992

Please sign in to comment.