github.com/scaleway/scaleway-cli@v1.11.1/pkg/config/config.go (about)

     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  // Package config contains helpers to manage '~/.scwrc'
     6  package config
     7  
     8  import (
     9  	"encoding/json"
    10  	"errors"
    11  	"fmt"
    12  	"io/ioutil"
    13  	"os"
    14  	"path/filepath"
    15  	"runtime"
    16  
    17  	"github.com/scaleway/scaleway-cli/pkg/scwversion"
    18  )
    19  
    20  // Config is a Scaleway CLI configuration file
    21  type Config struct {
    22  	// Organization is the identifier of the Scaleway orgnization
    23  	Organization string `json:"organization"`
    24  
    25  	// Token is the authentication token for the Scaleway organization
    26  	Token string `json:"token"`
    27  
    28  	// Version is the actual version of scw
    29  	Version string `json:"version"`
    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  	c.Version = scwversion.VERSION
    44  	err = json.NewEncoder(scwrc).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  	// Don't check permissions on Windows, Go knows nothing about them on this platform
    59  	// User profile is to be assumed safe anyway
    60  	if runtime.GOOS != "windows" {
    61  		stat, errStat := os.Stat(scwrcPath)
    62  		// we don't care if it fails, the user just won't see the warning
    63  		if errStat == nil {
    64  			perm := stat.Mode().Perm()
    65  			if perm&0066 != 0 {
    66  				return nil, fmt.Errorf("permissions %#o for .scwrc are too open", perm)
    67  			}
    68  		}
    69  	}
    70  
    71  	file, err := ioutil.ReadFile(scwrcPath)
    72  	if err != nil {
    73  		return nil, err
    74  	}
    75  	var config Config
    76  
    77  	err = json.Unmarshal(file, &config)
    78  	if err != nil {
    79  		return nil, err
    80  	}
    81  	return &config, nil
    82  }
    83  
    84  // GetConfigFilePath returns the path to the Scaleway CLI config file
    85  func GetConfigFilePath() (string, error) {
    86  	path, err := GetHomeDir()
    87  	if err != nil {
    88  		return "", err
    89  	}
    90  	return filepath.Join(path, ".scwrc"), nil
    91  }
    92  
    93  // GetHomeDir returns the path to your home
    94  func GetHomeDir() (string, error) {
    95  	homeDir := os.Getenv("HOME") // *nix
    96  	if homeDir == "" {           // Windows
    97  		homeDir = os.Getenv("USERPROFILE")
    98  	}
    99  	if homeDir == "" {
   100  		return "", errors.New("user home directory not found")
   101  	}
   102  	return homeDir, nil
   103  }