func loadThemeClientWithRetry(directory, env string, isRetry bool) (themekit.ThemeClient, error) { environments, err := loadEnvironments(directory) if err != nil { return themekit.ThemeClient{}, err } config, err := environments.GetConfiguration(env) if err != nil && len(environments) > 0 { invalidEnvMsg := fmt.Sprintf("'%s' is not a valid environment. The following environments are available within config.yml:", env) fmt.Println(themekit.RedText(invalidEnvMsg)) for e, _ := range environments { fmt.Println(themekit.RedText(fmt.Sprintf(" - %s", e))) } os.Exit(1) } else 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)) confirmationfn, savefn := commands.PrepareConfigurationMigration(directory) if confirmationfn() && savefn() == nil { 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 } return themekit.NewThemeClient(config), nil }
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 strings.TrimSpace(text) == "y" } saveFn := func() error { return env.Save(environmentLocation) } return confirmationFn, saveFn }
func verifyCommand(command string, args []string) { errors := []string{} if CommandIsInvalid(command) { if len(command) <= 0 { errors = append(errors, " An operation must be provided") } else { errors = append(errors, fmt.Sprintf(" -'%s' is not a valid command", command)) } } if CannotProcessCommandWithoutAdditionalArguments(command, args) { if parser, ok := parserMapping[command]; ok { errors = append(errors, fmt.Sprintf("\t- '%s' cannot run without additional arguments", command)) parser(command, []string{"-h"}) } } if len(errors) > 0 { errorMessage := fmt.Sprintf("Invalid Invocation!\n%s", strings.Join(errors, "\n")) fmt.Println(themekit.RedText(errorMessage)) SetupAndParseArgs([]string{"--help"}) os.Exit(1) } }
func buildInvalidVersionError(feed themekit.Feed, version string) error { buff := bytes.NewBuffer([]byte{}) buff.Write([]byte(themekit.RedText("Invalid Timber Version: " + version))) buff.Write([]byte("\nAvailable Versions Are:")) buff.Write([]byte("\n - master")) buff.Write([]byte("\n - latest")) for _, entry := range feed.Entries { buff.Write([]byte("\n - " + entry.Title)) } return errors.New(buff.String()) }
func handleError(filename string, err error, eventLog chan themekit.ThemeEvent) { if nonFatal, ok := err.(themekit.NonFatalNetworkError); ok { event := basicEvent{ Title: "Non-Fatal Network Error", EventType: nonFatal.Verb, Target: filename, etype: "fsevent", Formatter: func(b basicEvent) string { return fmt.Sprintf( "[%s] Could not complete %s for %s", themekit.RedText(fmt.Sprintf("%d", nonFatal.Code)), themekit.YellowText(b.EventType), themekit.BlueText(b.Target), ) }, } logEvent(event, eventLog) } }