示例#1
0
文件: main.go 项目: pkgr/scaleway-cli
// getScalewayAPI returns a ScalewayAPI using the user config file
func getScalewayAPI() (*api.ScalewayAPI, error) {
	// We already get config globally, but whis way we can get explicit error when trying to create a ScalewayAPI object
	config, err := getConfig()
	if err != nil {
		return nil, err
	}
	return api.NewScalewayAPI(os.Getenv("scaleway_api_endpoint"), config.Organization, config.Token)
}
示例#2
0
func runLogin(cmd *types.Command, args []string) {
	if loginHelp {
		cmd.PrintUsage()
	}
	if len(args) != 0 {
		cmd.PrintShortUsage()
	}

	if len(organization) == 0 {
		fmt.Println("You can get your credentials on https://cloud.scaleway.com/#/credentials")
		promptUser("Organization (access key): ", &organization, true)
	}
	if len(token) == 0 {
		promptUser("Token: ", &token, false)
	}

	cfg := &api.Config{
		APIEndPoint:  "https://account.scaleway.com/",
		Organization: strings.Trim(organization, "\n"),
		Token:        strings.Trim(token, "\n"),
	}

	api, err := api.NewScalewayAPI(cfg.APIEndPoint, cfg.Organization, cfg.Token)
	if err != nil {
		log.Fatalf("Unable to create ScalewayAPI: %s", err)
	}
	err = api.CheckCredentials()
	if err != nil {
		log.Fatalf("Unable to contact ScalewayAPI: %s", err)
	}

	scwrcPath, err := utils.GetConfigFilePath()
	if err != nil {
		log.Fatalf("Unable to get scwrc config file path: %s", err)
	}
	scwrc, err := os.OpenFile(scwrcPath, os.O_CREATE|os.O_TRUNC|os.O_RDWR, 0600)
	if err != nil {
		log.Fatalf("Unable to create scwrc config file: %s", err)
	}
	defer scwrc.Close()
	encoder := json.NewEncoder(scwrc)
	cfg.APIEndPoint = "https://api.scaleway.com/"
	err = encoder.Encode(cfg)
	if err != nil {
		log.Fatalf("Unable to encode scw config file: %s", err)
	}
}