From b4e9c37b22cb5e01488d51530b4c0f47f97edc5f Mon Sep 17 00:00:00 2001 From: Valentin Kiselev Date: Mon, 22 May 2023 19:14:56 +0300 Subject: [PATCH] chore: refactor dumping to make is more similar --- internal/config/command.go | 2 +- internal/config/config.go | 86 ++++++++++++++++---------------------- internal/config/remote.go | 6 +-- internal/config/script.go | 2 +- 4 files changed, 41 insertions(+), 55 deletions(-) diff --git a/internal/config/command.go b/internal/config/command.go index a131d3b0..bcb86aa5 100644 --- a/internal/config/command.go +++ b/internal/config/command.go @@ -12,7 +12,7 @@ import ( var errFilesIncompatible = errors.New("One of your runners contains incompatible file types") type Command struct { - Run string `mapstructure:"run" yaml:",omitempty" toml:"run,omitempty" json:"run,omitempty"` + Run string `mapstructure:"run"` Skip interface{} `mapstructure:"skip" yaml:",omitempty" toml:"skip,omitempty" json:"skip,omitempty"` Only interface{} `mapstructure:"only" yaml:",omitempty" toml:"only,omitempty,inline" json:"only,omitempty"` diff --git a/internal/config/config.go b/internal/config/config.go index 659d6ddc..6b0ab137 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -15,17 +15,17 @@ import ( const dumpIndent = 2 type Config struct { - Colors interface{} `mapstructure:"colors,omitempty" yaml:"colors,omitempty" toml:"colors,omitempty" json:"colors,omitempty"` - Extends []string `mapstructure:"extends,omitempty" yaml:"extends,omitempty" toml:"extends,omitempty" json:"extends,omitempty"` - Remote *Remote `mapstructure:"remote,omitempty" yaml:"remote,omitempty" toml:"remote,omitempty" json:"remote,omitempty"` - MinVersion string `mapstructure:"min_version,omitempty" yaml:"min_version,omitempty" toml:"min_version,omitempty" json:"min_version,omitempty"` - SkipOutput []string `mapstructure:"skip_output,omitempty" yaml:"skip_output,omitempty" toml:"skip_output,omitempty" json:"skip_output,omitempty"` - SourceDir string `mapstructure:"source_dir" yaml:"source_dir,omitempty" toml:"source_dir,omitempty" json:"source_dir,omitempty"` - SourceDirLocal string `mapstructure:"source_dir_local" yaml:"source_dir_local,omitempty" toml:"source_dir_local,omitempty" json:"source_dir_local,omitempty"` - Rc string `mapstructure:"rc,omitempty" yaml:"rc,omitempty" toml:"rc,omitempty" json:"rc,omitempty"` - NoTTY bool `mapstructure:"no_tty,omitempty" yaml:"no_tty,omitempty" toml:"no_tty,omitempty" json:"no_tty,omitempty"` - - Hooks map[string]*Hook `mapstructure:"-" yaml:",inline" json:"-" toml:"-"` + Colors interface{} `mapstructure:"colors,omitempty"` + Extends []string `mapstructure:"extends,omitempty"` + Remote *Remote `mapstructure:"remote,omitempty" ` + MinVersion string `mapstructure:"min_version,omitempty"` + SkipOutput []string `mapstructure:"skip_output,omitempty"` + SourceDir string `mapstructure:"source_dir"` + SourceDirLocal string `mapstructure:"source_dir_local"` + Rc string `mapstructure:"rc,omitempty"` + NoTTY bool `mapstructure:"no_tty,omitempty"` + + Hooks map[string]*Hook `mapstructure:"-"` } func (c *Config) Validate() error { @@ -33,23 +33,39 @@ func (c *Config) Validate() error { } func (c *Config) Dump(asJSON bool, asTOML bool) error { + res := make(map[string]interface{}) + if err := mapstructure.Decode(c, &res); err != nil { + return err + } + + if c.SourceDir == DefaultSourceDir { + delete(res, "source_dir") + } + if c.SourceDirLocal == DefaultSourceDirLocal { + delete(res, "source_dir_local") + } + + for hookName, hook := range c.Hooks { + res[hookName] = hook + } + if asJSON { - return c.dumpJSON() + return dumpJSON(res) } if asTOML { - return c.dumpTOML() + return dumpTOML(res) } - return c.dumpYAML() + return dumpYAML(res) } -func (c *Config) dumpYAML() error { +func dumpYAML(input map[string]interface{}) error { encoder := yaml.NewEncoder(os.Stdout) encoder.SetIndent(dumpIndent) defer encoder.Close() - err := encoder.Encode(c) + err := encoder.Encode(input) if err != nil { return err } @@ -57,29 +73,8 @@ func (c *Config) dumpYAML() error { return nil } -func (c *Config) dumpJSON() error { - // This hack allows to inline Hooks - type ConfigForMarshalling *Config - res, err := json.Marshal(ConfigForMarshalling(c)) - if err != nil { - return err - } - - var rawMarshalled map[string]json.RawMessage - if err = json.Unmarshal(res, &rawMarshalled); err != nil { - return err - } - - for hook, contents := range c.Hooks { - var hookMarshalled json.RawMessage - hookMarshalled, err = json.Marshal(contents) - if err != nil { - return err - } - rawMarshalled[hook] = hookMarshalled - } - - res, err = json.MarshalIndent(rawMarshalled, "", " ") +func dumpJSON(input map[string]interface{}) error { + res, err := json.MarshalIndent(input, "", " ") if err != nil { return err } @@ -89,18 +84,9 @@ func (c *Config) dumpJSON() error { return nil } -func (c *Config) dumpTOML() error { - res := make(map[string]interface{}) - if err := mapstructure.Decode(c, &res); err != nil { - return err - } - - for hookName, hook := range c.Hooks { - res[hookName] = hook - } - +func dumpTOML(input map[string]interface{}) error { encoder := toml.NewEncoder(os.Stdout) - err := encoder.Encode(res) + err := encoder.Encode(input) if err != nil { return err } diff --git a/internal/config/remote.go b/internal/config/remote.go index 8612bed1..1ffae230 100644 --- a/internal/config/remote.go +++ b/internal/config/remote.go @@ -1,9 +1,9 @@ package config type Remote struct { - GitURL string `mapstructure:"git_url" yaml:"git_url,omitempty" json:"git_url,omitempty" toml:"git_url"` - Ref string `mapstructure:"ref" yaml:",omitempty" json:"ref,omitempty" toml:"ref,omitempty"` - Config string `mapstructure:"config" yaml:",omitempty" json:"config,omitempty" toml:"config,omitempty"` + GitURL string `mapstructure:"git_url" yaml:"git_url" json:"git_url,omitempty" toml:"git_url"` + Ref string `mapstructure:"ref,omitempty" yaml:",omitempty" json:"ref,omitempty" toml:"ref,omitempty"` + Config string `mapstructure:"config,omitempty" yaml:",omitempty" json:"config,omitempty" toml:"config,omitempty"` } func (r *Remote) Configured() bool { diff --git a/internal/config/script.go b/internal/config/script.go index 21fec93f..a1efd3fc 100644 --- a/internal/config/script.go +++ b/internal/config/script.go @@ -10,7 +10,7 @@ import ( ) type Script struct { - Runner string `mapstructure:"runner" yaml:",omitempty" toml:"runner,omitempty" json:"runner,omitempty"` + Runner string `mapstructure:"runner"` Skip interface{} `mapstructure:"skip" yaml:",omitempty" toml:"skip,omitempty" json:"skip,omitempty"` Only interface{} `mapstructure:"only" yaml:",omitempty" toml:"only,omitempty,inline" json:"only,omitempty"`