Skip to content

Commit

Permalink
Merge pull request #54 from csaunders/confirm-when-changing-configura…
Browse files Browse the repository at this point in the history
…tion

Adds a confirmation action when migrating a theme
  • Loading branch information
csaunders committed Jun 2, 2015
2 parents cdf14ea + c540637 commit d143561
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 5 deletions.
9 changes: 5 additions & 4 deletions cmd/theme/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,11 +228,12 @@ func loadThemeClientWithRetry(directory, env string, isRetry bool) (themekit.The
if err != nil && !isRetry {
upgradeMessage := fmt.Sprintf("Looks like your configuration file is out of date. Upgrading to default environment '%s'", themekit.DefaultEnvironment)
fmt.Println(themekit.YellowText(upgradeMessage))
err := commands.MigrateConfiguration(directory)
if err != nil {
return themekit.ThemeClient{}, err
confirmationfn, savefn := commands.PrepareConfigurationMigration(directory)

if confirmationfn() && savefn() == nil {
return loadThemeClientWithRetry(directory, env, true)
}
return loadThemeClientWithRetry(directory, env, true)
return themekit.ThemeClient{}, errors.New("loadThemeClientWithRetry: could not load or migrate the configuration")
} else if err != nil {
return themekit.ThemeClient{}, err
}
Expand Down
30 changes: 29 additions & 1 deletion commands/configure.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package commands

import (
"bufio"
"errors"
"fmt"
"github.com/csaunders/themekit"
"io/ioutil"
"os"
"path/filepath"
"strings"

"github.com/csaunders/themekit"
)

type ConfigurationOptions struct {
Expand Down Expand Up @@ -114,6 +116,32 @@ func MigrateConfigurationCommand(args map[string]interface{}) (done chan bool, l
return
}

func PrepareConfigurationMigration(dir string) (func() bool, func() error) {
environmentLocation := filepath.Join(dir, "config.yml")
env, err := loadOrInitializeEnvironment(environmentLocation)
if err != nil {
themekit.NotifyError(err)
return func() bool { return false }, func() error { return err }
}

confirmationFn := func() bool {
before, _ := ioutil.ReadFile(environmentLocation)
after := env.String()
fmt.Println(themekit.YellowText("Compare changes to configuration:"))
fmt.Println(themekit.YellowText("Before:\n"), themekit.GreenText(string(before)))
fmt.Println(themekit.YellowText("After:\n"), themekit.RedText(after))
reader := bufio.NewReader(os.Stdin)
fmt.Println(themekit.YellowText("Does this look correct? (y/n)"))
text, _ := reader.ReadString('\n')
return text == "y"
}

saveFn := func() error {
return env.Save(environmentLocation)
}
return confirmationFn, saveFn
}

func MigrateConfiguration(dir string) error {
environmentLocation := filepath.Join(dir, "config.yml")
env, err := loadOrInitializeEnvironment(environmentLocation)
Expand Down
8 changes: 8 additions & 0 deletions environments.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,14 @@ func (e Environments) Write(w io.Writer) error {
return err
}

func (e Environments) String() string {
bytes, err := yaml.Marshal(e)
if err != nil {
return "environments: cannot serialize"
}
return string(bytes)
}

func (e Environments) Save(location string) error {
file, err := os.OpenFile(location, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
defer file.Close()
Expand Down

0 comments on commit d143561

Please sign in to comment.