|
| 1 | +// Copyright (C) 2015 Scaleway. All rights reserved. |
| 2 | +// Use of this source code is governed by a MIT-style |
| 3 | +// license that can be found in the LICENSE.md file. |
| 4 | + |
| 5 | +// ~/.scwrc management |
| 6 | +package config |
| 7 | + |
| 8 | +import ( |
| 9 | + "encoding/json" |
| 10 | + "errors" |
| 11 | + "fmt" |
| 12 | + "io/ioutil" |
| 13 | + "os" |
| 14 | + "path/filepath" |
| 15 | +) |
| 16 | + |
| 17 | +// Config is a Scaleway CLI configuration file |
| 18 | +type Config struct { |
| 19 | + // ComputeAPI is the endpoint to the Scaleway API |
| 20 | + ComputeAPI string `json:"api_endpoint"` |
| 21 | + |
| 22 | + // AccountAPI is the endpoint to the Scaleway Account API |
| 23 | + AccountAPI string `json:"account_endpoint"` |
| 24 | + |
| 25 | + // Organization is the identifier of the Scaleway orgnization |
| 26 | + Organization string `json:"organization"` |
| 27 | + |
| 28 | + // Token is the authentication token for the Scaleway organization |
| 29 | + Token string `json:"token"` |
| 30 | +} |
| 31 | + |
| 32 | +// Save write the config file |
| 33 | +func (c *Config) Save() error { |
| 34 | + scwrcPath, err := GetConfigFilePath() |
| 35 | + if err != nil { |
| 36 | + return fmt.Errorf("Unable to get scwrc config file path: %s", err) |
| 37 | + } |
| 38 | + scwrc, err := os.OpenFile(scwrcPath, os.O_CREATE|os.O_TRUNC|os.O_RDWR, 0600) |
| 39 | + if err != nil { |
| 40 | + return fmt.Errorf("Unable to create scwrc config file: %s", err) |
| 41 | + } |
| 42 | + defer scwrc.Close() |
| 43 | + encoder := json.NewEncoder(scwrc) |
| 44 | + err = encoder.Encode(c) |
| 45 | + if err != nil { |
| 46 | + return fmt.Errorf("Unable to encode scw config file: %s", err) |
| 47 | + } |
| 48 | + return nil |
| 49 | +} |
| 50 | + |
| 51 | +// GetConfig returns the Scaleway CLI config file for the current user |
| 52 | +func GetConfig() (*Config, error) { |
| 53 | + scwrcPath, err := GetConfigFilePath() |
| 54 | + if err != nil { |
| 55 | + return nil, err |
| 56 | + } |
| 57 | + |
| 58 | + stat, err := os.Stat(scwrcPath) |
| 59 | + // we don't care if it fails, the user just won't see the warning |
| 60 | + if err == nil { |
| 61 | + mode := stat.Mode() |
| 62 | + if mode&0066 != 0 { |
| 63 | + return nil, fmt.Errorf("permissions %#o for .scwrc are too open.", mode) |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + file, err := ioutil.ReadFile(scwrcPath) |
| 68 | + if err != nil { |
| 69 | + return nil, err |
| 70 | + } |
| 71 | + var config Config |
| 72 | + err = json.Unmarshal(file, &config) |
| 73 | + if err != nil { |
| 74 | + return nil, err |
| 75 | + } |
| 76 | + // check if he has an old scwrc version |
| 77 | + if config.AccountAPI == "" { |
| 78 | + config.AccountAPI = "https://account.scaleway.com" |
| 79 | + config.Save() |
| 80 | + } |
| 81 | + if os.Getenv("scaleway_api_endpoint") == "" { |
| 82 | + os.Setenv("scaleway_api_endpoint", config.ComputeAPI) |
| 83 | + } |
| 84 | + return &config, nil |
| 85 | +} |
| 86 | + |
| 87 | +// GetConfigFilePath returns the path to the Scaleway CLI config file |
| 88 | +func GetConfigFilePath() (string, error) { |
| 89 | + path, err := GetHomeDir() |
| 90 | + if err != nil { |
| 91 | + return "", err |
| 92 | + } |
| 93 | + return filepath.Join(path, ".scwrc"), nil |
| 94 | +} |
| 95 | + |
| 96 | +// GetHomeDir returns the path to your home |
| 97 | +func GetHomeDir() (string, error) { |
| 98 | + homeDir := os.Getenv("HOME") // *nix |
| 99 | + if homeDir == "" { // Windows |
| 100 | + homeDir = os.Getenv("USERPROFILE") |
| 101 | + } |
| 102 | + if homeDir == "" { |
| 103 | + return "", errors.New("user home directory not found") |
| 104 | + } |
| 105 | + return homeDir, nil |
| 106 | +} |
0 commit comments