go.undefinedlabs.com/scopeagent@v0.4.2/agent/config.go (about)

     1  package agent
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"io/ioutil"
     7  	"os"
     8  	"runtime"
     9  
    10  	"github.com/mitchellh/go-homedir"
    11  )
    12  
    13  type Config struct {
    14  	CurrentProfile string             `json:"currentProfile"`
    15  	Profiles       map[string]Profile `json:"profiles"`
    16  }
    17  
    18  type Profile struct {
    19  	ApiEndpoint string `json:"apiEndpoint"`
    20  	ApiKey      string `json:"apiKey"`
    21  	OAuthToken  string `json:"oauthToken"`
    22  }
    23  
    24  func GetConfig() *Config {
    25  	homeDir, err := homedir.Dir()
    26  	if err != nil {
    27  		return nil
    28  	}
    29  	var filePath string
    30  	if runtime.GOOS == "windows" {
    31  		filePath = fmt.Sprintf("%s/AppData/Roaming/scope/config.json", homeDir)
    32  	} else {
    33  		filePath = fmt.Sprintf("%s/.scope/config.json", homeDir)
    34  	}
    35  	file, err := os.Open(filePath)
    36  	if err != nil {
    37  		return nil
    38  	}
    39  	defer file.Close()
    40  	fileBytes, _ := ioutil.ReadAll(file)
    41  	var config Config
    42  	if err = json.Unmarshal(fileBytes, &config); err != nil {
    43  		return nil
    44  	}
    45  	return &config
    46  }
    47  
    48  func GetConfigCurrentProfile() *Profile {
    49  	if config := GetConfig(); config != nil && config.Profiles != nil && config.CurrentProfile != "" {
    50  		profile := config.Profiles[config.CurrentProfile]
    51  		return &profile
    52  	}
    53  	return nil
    54  }