Skip to content

Commit

Permalink
remove Marshal() from config.Config
Browse files Browse the repository at this point in the history
  • Loading branch information
phm07 committed Jan 10, 2024
1 parent d89a322 commit 6292032
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 42 deletions.
52 changes: 25 additions & 27 deletions internal/state/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (

type Config interface {
Write() error
Marshal() ([]byte, error)

ActiveContext() *Context
SetActiveContext(*Context)
Expand Down Expand Up @@ -62,7 +61,7 @@ func ReadConfig(path string) (Config, error) {
}

func (cfg *config) Write() error {
data, err := cfg.Marshal()
data, err := cfg.marshal()
if err != nil {
return err
}
Expand All @@ -75,31 +74,6 @@ func (cfg *config) Write() error {
return nil
}

type rawConfig struct {
activeContext string
contexts []rawConfigContext
}

type rawConfigContext struct {
Name string `toml:"name"`
Token string `toml:"token"`
}

func (cfg *config) Marshal() ([]byte, error) {

var raw rawConfig
if cfg.activeContext != nil {
raw.activeContext = cfg.activeContext.Name
}
for _, context := range cfg.contexts {
raw.contexts = append(raw.contexts, rawConfigContext{
Name: context.Name,
Token: context.Token,
})
}
return toml.Marshal(raw)
}

func (cfg *config) ActiveContext() *Context {
return cfg.activeContext
}
Expand Down Expand Up @@ -153,6 +127,30 @@ func (cfg *config) RemoveContext(context *Context) {
}
}

type rawConfig struct {
activeContext string
contexts []rawConfigContext
}

type rawConfigContext struct {
Name string `toml:"name"`
Token string `toml:"token"`
}

func (cfg *config) marshal() ([]byte, error) {
var raw rawConfig
if cfg.activeContext != nil {
raw.activeContext = cfg.activeContext.Name
}
for _, context := range cfg.contexts {
raw.contexts = append(raw.contexts, rawConfigContext{
Name: context.Name,
Token: context.Token,
})
}
return toml.Marshal(raw)
}

func (cfg *config) unmarshal(data []byte) error {
var raw rawConfig
if err := toml.Unmarshal(data, &raw); err != nil {
Expand Down
15 changes: 0 additions & 15 deletions internal/state/config/zz_config_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 6292032

Please sign in to comment.