func CmdSet(path string, settings *models.Settings) error { fullPath, err := homedir.Expand(path) if err != nil { return err } // make sure both files exist _, err = os.Stat(fullPath + ".pub") if err != nil { if os.IsNotExist(err) { return fmt.Errorf("Public key file '%s' does not exist.", fullPath+".pub") } return err } _, err = os.Stat(fullPath) if err != nil { if os.IsNotExist(err) { return fmt.Errorf("Private key file '%s' does not exist.", fullPath) } return err } settings.PrivateKeyPath = fullPath settings.SessionToken = "" a := auth.New(settings, prompts.New()) user, err := a.Signin() if err != nil { return err } logrus.Printf("Successfully added key and signed in as %s.", user.Email) return nil }
// setGivenEnv takes the given env name and finds it in the env list // in the given settings object. It then populates the EnvironmentID and // ServiceID on the settings object with appropriate values. func setGivenEnv(envName string, settings *models.Settings) { for eName, e := range settings.Environments { if eName == envName { settings.EnvironmentID = e.EnvironmentID settings.ServiceID = e.ServiceID settings.Pod = e.Pod settings.EnvironmentName = envName settings.OrgID = e.OrgID break } } }
func CmdClear(privateKey, session, environments, defaultEnv, pods bool, settings *models.Settings) error { if defaultEnv { logrus.Warnln("The \"--default\" flag has been deprecated! It will be removed in a future version.") } if privateKey { settings.PrivateKeyPath = "" } if session { settings.SessionToken = "" settings.UsersID = "" } if environments { settings.Environments = map[string]models.AssociatedEnv{} } if defaultEnv { settings.Default = "" } if pods { settings.Pods = &[]models.Pod{} } config.SaveSettings(settings) if !privateKey && !session && !environments && !defaultEnv && !pods { logrus.Println("No settings were specified. To see available options, run \"catalyze clear --help\"") } else { logrus.Println("All specified settings have been cleared") } return nil }
// GetSettings returns a Settings object for the current context func (s FileSettingsRetriever) GetSettings(envName, svcName, accountsHost, authHost, ignoreAuthHostVersion, paasHost, ignorePaasHostVersion, username, password string) *models.Settings { HomeDir, err := homedir.Dir() if err != nil { logrus.Println(err.Error()) os.Exit(1) } file, err := os.Open(filepath.Join(HomeDir, SettingsFile)) if os.IsNotExist(err) { file, err = os.Create(filepath.Join(HomeDir, SettingsFile)) } defer file.Close() if err != nil { logrus.Println(err.Error()) os.Exit(1) } var settings models.Settings json.NewDecoder(file).Decode(&settings) // would be best to default this to an initialized map rather than nil if settings.Environments == nil { settings.Environments = make(map[string]models.AssociatedEnv) } // try and set the given env first, if it exists if envName != "" { setGivenEnv(envName, &settings) if settings.EnvironmentID == "" || settings.ServiceID == "" { logrus.Fatalf("No environment named \"%s\" has been associated. Run \"catalyze associated\" to see what environments have been associated or run \"catalyze associate\" from a local git repo to create a new association", envName) } } // if not given, try default. this is deprecated and will be removed soon if settings.EnvironmentID == "" || settings.ServiceID == "" { setGivenEnv(settings.Default, &settings) } settings.AccountsHost = accountsHost settings.AuthHost = authHost settings.PaasHost = paasHost settings.Username = username settings.Password = password authHostVersion := os.Getenv(AuthHostVersionEnvVar) if authHostVersion == "" { authHostVersion = AuthHostVersion } settings.AuthHostVersion = authHostVersion paasHostVersion := os.Getenv(PaasHostVersionEnvVar) if paasHostVersion == "" { paasHostVersion = PaasHostVersion } settings.PaasHostVersion = paasHostVersion logrus.Debugf("Accounts Host: %s", accountsHost) logrus.Debugf("Auth Host: %s", authHost) logrus.Debugf("Paas Host: %s", paasHost) logrus.Debugf("Auth Host Version: %s", authHostVersion) logrus.Debugf("Paas Host Version: %s", paasHostVersion) logrus.Debugf("Environment ID: %s", settings.EnvironmentID) logrus.Debugf("Environment Name: %s", settings.EnvironmentName) logrus.Debugf("Pod: %s", settings.Pod) logrus.Debugf("Service ID: %s", settings.ServiceID) logrus.Debugf("Org ID: %s", settings.OrgID) settings.Version = VERSION return &settings }
func InitGlobalOpts(app *cli.Cli, settings *models.Settings) { accountsHost := os.Getenv(config.AccountsHostEnvVar) if accountsHost == "" { accountsHost = config.AccountsHost } authHost := os.Getenv(config.AuthHostEnvVar) if authHost == "" { authHost = config.AuthHost } paasHost := os.Getenv(config.PaasHostEnvVar) if paasHost == "" { paasHost = config.PaasHost } username := app.String(cli.StringOpt{ Name: "U username", Desc: "Catalyze Username", EnvVar: config.CatalyzeUsernameEnvVar, HideValue: true, }) password := app.String(cli.StringOpt{ Name: "P password", Desc: "Catalyze Password", EnvVar: config.CatalyzePasswordEnvVar, HideValue: true, }) givenEnvName := app.String(cli.StringOpt{ Name: "E env", Desc: "The local alias of the environment in which this command will be run", EnvVar: config.CatalyzeEnvironmentEnvVar, HideValue: true, }) if loggingLevel := os.Getenv(config.LogLevelEnvVar); loggingLevel != "" { if lvl, err := logrus.ParseLevel(loggingLevel); err == nil { logrus.SetLevel(lvl) } } app.Before = func() { r := config.FileSettingsRetriever{} *settings = *r.GetSettings(*givenEnvName, "", accountsHost, authHost, "", paasHost, "", *username, *password) logrus.Debugf("%+v", settings) if settings.Pods == nil || len(*settings.Pods) == 0 || settings.PodCheck < time.Now().Unix() { settings.PodCheck = time.Now().Unix() + 86400 p := pods.New(settings) pods, err := p.List() if err == nil { settings.Pods = pods logrus.Debugf("%+v", settings.Pods) } else { settings.Pods = &[]models.Pod{} logrus.Debugf("Error listing pods: %s", err.Error()) } } } app.After = func() { config.SaveSettings(settings) } versionString := fmt.Sprintf("version %s %s", config.VERSION, config.ArchString()) logrus.Debugln(versionString) app.Version("v version", versionString) }