Skip to content

Commit

Permalink
chore: refactor dumping to make is more similar
Browse files Browse the repository at this point in the history
  • Loading branch information
mrexox committed May 22, 2023
1 parent b78fef3 commit b4e9c37
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 55 deletions.
2 changes: 1 addition & 1 deletion internal/config/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down
86 changes: 36 additions & 50 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,71 +15,66 @@ 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 {
return version.CheckCovered(c.MinVersion)
}

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
}

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
}
Expand All @@ -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
}
Expand Down
6 changes: 3 additions & 3 deletions internal/config/remote.go
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion internal/config/script.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand Down

0 comments on commit b4e9c37

Please sign in to comment.