// DeleteBreadcrumb removes the config file at LocalSettingsPath func DeleteBreadcrumb(alias string, settings *models.Settings) { env := settings.Environments[alias] dir := env.Directory dir = filepath.Join(dir, ".git", LocalSettingsFile) defer os.Remove(dir) delete(settings.Environments, alias) if settings.Default == alias { settings.Default = "" } os.Remove(dir) SaveSettings(settings) }
// SetDefault sets the default environment. This environment must already be // associated. Any commands run outside of a git directory will use the default // environment for context. func SetDefault(alias string, settings *models.Settings) { var found bool for name := range settings.Environments { if name == alias { found = true break } } if !found { fmt.Printf("No environment with an alias of \"%s\" has been associated. Please run \"catalyze associate\" first\n", alias) os.Exit(1) } settings.Default = alias config.SaveSettings(settings) fmt.Printf("%s is now the default environment\n", alias) }
// Associate an environment so that commands can be run against it. This command // no longer adds a git remote. See commands.AddRemote(). func Associate(envLabel string, serviceLabel string, alias string, remote string, defaultEnv bool, settings *models.Settings) { if _, err := os.Stat(".git"); os.IsNotExist(err) { fmt.Println("Not git repo found in the current directory") os.Exit(1) } helpers.SignIn(settings) envs := helpers.ListEnvironments("pod", settings) for _, env := range *envs { if env.Data.Name == envLabel { if env.State == "defined" { fmt.Printf("Your environment is not yet provisioned. Please visit https://dashboard.catalyze.io/environments/update/%s to finish provisioning your environment\n", env.ID) return } // would be nice to have some sort of global filter() function var chosenService models.Service if serviceLabel != "" { labels := []string{} for _, service := range *env.Data.Services { if service.Type == "code" { labels = append(labels, service.Label) if service.Label == serviceLabel { chosenService = service break } } } if chosenService.Type == "" { fmt.Printf("No code service found with name '%s'. Code services found: %s\n", serviceLabel, strings.Join(labels, ", ")) os.Exit(1) } } else { for _, service := range *env.Data.Services { if service.Type == "code" { chosenService = service break } } if chosenService.Type == "" { fmt.Printf("No code service found for \"%s\" environment (ID = %s)\n", envLabel, settings.EnvironmentID) os.Exit(1) } } for _, r := range helpers.ListGitRemote() { if r == remote { helpers.RemoveGitRemote(remote) break } } helpers.AddGitRemote(remote, chosenService.Source) fmt.Printf("\"%s\" remote added.\n", remote) dir, err := filepath.Abs(filepath.Dir(os.Args[0])) if err != nil { panic(err) } name := alias if name == "" { name = envLabel } settings.Environments[name] = models.AssociatedEnv{ EnvironmentID: env.ID, ServiceID: chosenService.ID, Directory: dir, Name: envLabel, } if defaultEnv { settings.Default = name } config.DropBreadcrumb(name, settings) config.SaveSettings(settings) if len(settings.Environments) > 1 && settings.Default == "" { fmt.Printf("You now have %d environments associated. Consider running \"catalyze default ENV_NAME\" to set a default\n", len(settings.Environments)) } return } } fmt.Printf("No environment with label \"%s\" found\n", envLabel) os.Exit(1) }