Skip to content

Commit

Permalink
Upgrade config file to yaml on init
Browse files Browse the repository at this point in the history
  • Loading branch information
saheljalal committed Oct 4, 2019
1 parent c3e2dba commit 7eaf407
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 32 deletions.
22 changes: 16 additions & 6 deletions config/config.go
Expand Up @@ -24,7 +24,7 @@ const (
// to a manifest
type Config struct {
path string
Manifest *model.Manifest
manifest *model.Manifest
}

// NewConfig returns a new nostromo config
Expand Down Expand Up @@ -63,23 +63,33 @@ func Parse(path string) (*Config, error) {
return NewConfig(path, m), nil
}

// Path of the config
func (c *Config) Path() string {
return c.path
}

// Manifest associated with this config
func (c *Config) Manifest() *model.Manifest {
return c.manifest
}

// Save nostromo config to file
func (c *Config) Save() error {
if len(c.path) == 0 {
return fmt.Errorf("invalid path to save")
}

if c.Manifest == nil {
if c.manifest == nil {
return fmt.Errorf("manifest is nil")
}

var b []byte
var err error
ext := filepath.Ext(c.path)
if ext == ".json" {
b, err = json.Marshal(c.Manifest)
b, err = json.Marshal(c.manifest)
} else if ext == ".yaml" {
b, err = yaml.Marshal(c.Manifest)
b, err = yaml.Marshal(c.manifest)
}

if err != nil {
Expand Down Expand Up @@ -121,7 +131,7 @@ func (c *Config) Exists() bool {
func (c *Config) Get(key string) string {
switch key {
case "verbose":
return strconv.FormatBool(c.Manifest.Config.Verbose)
return strconv.FormatBool(c.manifest.Config.Verbose)
}
return "key not found"
}
Expand All @@ -134,7 +144,7 @@ func (c *Config) Set(key, value string) error {
if err != nil {
return err
}
c.Manifest.Config.Verbose = verbose
c.manifest.Config.Verbose = verbose
return nil
}
return fmt.Errorf("key not found")
Expand Down
8 changes: 4 additions & 4 deletions config/config_test.go
Expand Up @@ -28,7 +28,7 @@ func TestParse(t *testing.T) {
} else if !test.expErr {
if err != nil {
t.Errorf("expected no error but got %s", err)
} else if c.Manifest == nil {
} else if c.manifest == nil {
t.Errorf("manifest is nil")
}
}
Expand Down Expand Up @@ -128,7 +128,7 @@ func TestGet(t *testing.T) {
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
c := NewConfig("path", fakeManifest())
c.Manifest.Config.Verbose = true
c.Manifest().Config.Verbose = true
if actual := c.Get(test.key); actual != test.expected {
t.Errorf("expected: %s, actual: %s", test.expected, actual)
}
Expand Down Expand Up @@ -179,7 +179,7 @@ func TestKeys(t *testing.T) {

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
if actual := test.config.Manifest.Config.Keys(); !reflect.DeepEqual(actual, test.expected) {
if actual := test.config.Manifest().Config.Keys(); !reflect.DeepEqual(actual, test.expected) {
t.Errorf("expected: %s, actual: %s", test.expected, actual)
}
})
Expand All @@ -203,7 +203,7 @@ func TestFields(t *testing.T) {

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
if actual := test.config.Manifest.Config.Fields(); !reflect.DeepEqual(actual, test.expected) {
if actual := test.config.Manifest().Config.Fields(); !reflect.DeepEqual(actual, test.expected) {
pretty.Println(actual)
t.Errorf("expected: %s, actual: %s", test.expected, actual)
}
Expand Down
77 changes: 55 additions & 22 deletions task/task.go
@@ -1,6 +1,8 @@
package task

import (
"os"
"path/filepath"
"strings"

"github.com/pokanop/nostromo/config"
Expand All @@ -21,6 +23,7 @@ func SetVersion(v *version.Info) {
// InitConfig of nostromo config file if not already initialized
func InitConfig() {
cfg := checkConfigQuiet()

if cfg == nil {
cfg = config.NewConfig(config.ConfigPath, model.NewManifest())
err := pathutil.EnsurePath("~/.nostromo")
Expand All @@ -30,6 +33,25 @@ func InitConfig() {
}

log.Highlight("nostromo config created")
} else if filepath.Ext(cfg.Path()) == ".json" {
// Deprecated config file treated as new config
// Need to update to yaml
p := cfg.Path()
m := cfg.Manifest()
cfg = config.NewConfig(config.ConfigPath, m)
err := pathutil.EnsurePath("~/.nostromo")
if err != nil {
log.Error(err)
return
}

err = os.Remove(pathutil.Abs(p))
if err != nil {
log.Error(err)
return
}

log.Highlight("nostromo config exists, upgraded")
} else {
log.Highlight("nostromo config exists, updating")
}
Expand Down Expand Up @@ -67,13 +89,15 @@ func ShowConfig(rawJSON bool, rawYAML bool) {
return
}

m := cfg.Manifest()

if rawJSON || rawYAML {
log.Highlight("[manifest]")
if rawJSON {
log.Regular(cfg.Manifest.AsJSON())
log.Regular(m.AsJSON())
log.Regular()
} else if rawYAML {
log.Regular(cfg.Manifest.AsYAML())
log.Regular(m.AsYAML())
}

lines, err := shell.InitFileLines()
Expand All @@ -85,17 +109,17 @@ func ShowConfig(rawJSON bool, rawYAML bool) {
log.Regular(strings.TrimSpace(lines))
} else {
log.Regular("[manifest]")
log.Fields(cfg.Manifest)
log.Fields(m)

log.Regular("\n[config]")
log.Fields(cfg.Manifest.Config)
log.Fields(m.Config)

if len(cfg.Manifest.Commands) > 0 {
if len(m.Commands) > 0 {
log.Regular("\n[commands]")
for _, cmd := range cfg.Manifest.Commands {
for _, cmd := range m.Commands {
cmd.Walk(func(c *model.Command, s *bool) {
log.Fields(c)
if cfg.Manifest.Config.Verbose {
if m.Config.Verbose {
log.Regular()
}
})
Expand Down Expand Up @@ -140,13 +164,15 @@ func AddCommand(keyPath, command, description, code, language string) {
return
}

err := cfg.Manifest.AddCommand(keyPath, command, description)
m := cfg.Manifest()

err := m.AddCommand(keyPath, command, description)
if err != nil {
log.Error(err)
return
}

cmd := cfg.Manifest.Find(keyPath)
cmd := m.Find(keyPath)
if cmd == nil {
log.Error("unable to find newly created command")
return
Expand All @@ -172,7 +198,7 @@ func RemoveCommand(keyPath string) {
return
}

err := cfg.Manifest.RemoveCommand(keyPath)
err := cfg.Manifest().RemoveCommand(keyPath)
if err != nil {
log.Error(err)
return
Expand All @@ -191,7 +217,9 @@ func AddSubstitution(keyPath, name, alias string) {
return
}

err := cfg.Manifest.AddSubstitution(keyPath, name, alias)
m := cfg.Manifest()

err := m.AddSubstitution(keyPath, name, alias)
if err != nil {
log.Error(err)
}
Expand All @@ -201,7 +229,7 @@ func AddSubstitution(keyPath, name, alias string) {
log.Error(err)
}

log.Fields(cfg.Manifest.Find(keyPath))
log.Fields(m.Find(keyPath))
}

// RemoveSubstitution from the manifest
Expand All @@ -211,7 +239,7 @@ func RemoveSubstitution(keyPath, alias string) {
return
}

err := cfg.Manifest.RemoveSubstitution(keyPath, alias)
err := cfg.Manifest().RemoveSubstitution(keyPath, alias)
if err != nil {
log.Error(err)
}
Expand All @@ -229,13 +257,15 @@ func Run(args []string) {
return
}

language, cmd, err := cfg.Manifest.ExecutionString(sanitizeArgs(args))
m := cfg.Manifest()

language, cmd, err := m.ExecutionString(sanitizeArgs(args))
if err != nil {
log.Error(err)
return
}

err = shell.Run(cmd, language, cfg.Manifest.Config.Verbose)
err = shell.Run(cmd, language, m.Config.Verbose)
if err != nil {
log.Error(err)
}
Expand All @@ -248,10 +278,12 @@ func Find(name string) {
return
}

m := cfg.Manifest()

matchingCmds := []*model.Command{}
matchingSubs := []*model.Command{}

for _, cmd := range cfg.Manifest.Commands {
for _, cmd := range m.Commands {
cmd.Walk(func(c *model.Command, s *bool) {
if containsCaseInsensitive(c.Name, name) || containsCaseInsensitive(c.Alias, name) {
matchingCmds = append(matchingCmds, c)
Expand All @@ -272,18 +304,18 @@ func Find(name string) {
log.Regular("[commands]")
for _, cmd := range matchingCmds {
log.Fields(cmd)
if cfg.Manifest.Config.Verbose {
if m.Config.Verbose {
log.Regular()
}
}

if !cfg.Manifest.Config.Verbose {
if !m.Config.Verbose {
log.Regular()
}
log.Regular("[substitutions]")
for _, cmd := range matchingSubs {
log.Fields(cmd)
if cfg.Manifest.Config.Verbose {
if m.Config.Verbose {
log.Regular()
}
}
Expand All @@ -310,20 +342,21 @@ func checkConfigCommon(quiet bool) *config.Config {
return nil
}

log.SetOptions(cfg.Manifest.Config.Verbose)
log.SetOptions(cfg.Manifest().Config.Verbose)

return cfg
}

func saveConfig(cfg *config.Config) error {
cfg.Manifest.Version = ver.SemVer
m := cfg.Manifest()
m.Version = ver.SemVer

err := cfg.Save()
if err != nil {
return err
}

err = shell.Commit(cfg.Manifest)
err = shell.Commit(m)
if err != nil {
return err
}
Expand Down

0 comments on commit 7eaf407

Please sign in to comment.