-
Notifications
You must be signed in to change notification settings - Fork 156
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #83 from rm-Umar/umar/settings
added global cache
- Loading branch information
Showing
11 changed files
with
274 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/ipinfo/cli/lib/complete" | ||
"github.com/ipinfo/cli/lib/complete/predict" | ||
"github.com/spf13/pflag" | ||
) | ||
|
||
var completionsConfig = &complete.Command{ | ||
Flags: map[string]complete.Predictor{ | ||
"-h": predict.Nothing, | ||
"--help": predict.Nothing, | ||
}, | ||
} | ||
|
||
func printHelpConfig() { | ||
fmt.Printf( | ||
`Usage: %s config [<key>=<value>...] | ||
Description: | ||
Change the configurations. | ||
Examples: | ||
$ %[1]s config cache=disable | ||
$ %[1]s config token=testtoken cache=enable | ||
Options: | ||
--help, -h | ||
show help. | ||
Configurations: | ||
cache=<enable | disable> | ||
Control whether the cache is enabled or disabled. | ||
token=<tok> | ||
Save a token for use when querying the API. | ||
(Token will not be validated). | ||
`, progBase) | ||
} | ||
|
||
func cmdConfig() error { | ||
var fHelp bool | ||
|
||
pflag.BoolVarP(&fHelp, "help", "h", false, "show help.") | ||
pflag.Parse() | ||
|
||
// get args for config and parsing it. | ||
args := pflag.Args()[1:] | ||
if fHelp || len(args) < 1 { | ||
printHelpConfig() | ||
return nil | ||
} | ||
for _, arg := range args { | ||
configStr := strings.Split(arg, "=") | ||
key := strings.ToLower(configStr[0]) | ||
if len(configStr) != 2 { | ||
if key == "cache" || key == "token" { | ||
return fmt.Errorf("err: no value provided for key %s", key) | ||
} | ||
return fmt.Errorf("err: invalid key argument %s", key) | ||
} | ||
switch key { | ||
case "cache": | ||
val := strings.ToLower(configStr[1]) | ||
switch val { | ||
case "enable": | ||
gConfig.CacheEnabled = true | ||
case "disable": | ||
gConfig.CacheEnabled = false | ||
default: | ||
return fmt.Errorf("err: invalid value %s; cache must be 'enabled' or disabled", val) | ||
} | ||
case "token": | ||
gConfig.Token = configStr[1] | ||
default: | ||
return fmt.Errorf("err: invalid key argument %s", configStr[0]) | ||
} | ||
} | ||
|
||
// save config in bulk. | ||
if err := SaveConfig(gConfig); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"io/ioutil" | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/ipinfo/cli/lib" | ||
) | ||
|
||
// global config. | ||
var gConfig Config | ||
|
||
type Config struct { | ||
CacheEnabled bool `json:"cache_enabled"` | ||
Token string `json:"token"` | ||
} | ||
|
||
// gets the global config directory, creating it if necessary. | ||
func getConfigDir() (string, error) { | ||
cdir, err := os.UserConfigDir() | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
confDir := filepath.Join(cdir, "ipinfo") | ||
if err := os.MkdirAll(confDir, 0700); err != nil { | ||
return "", err | ||
} | ||
|
||
return confDir, nil | ||
} | ||
|
||
// returns the path to the config file. | ||
func ConfigPath() (string, error) { | ||
confDir, err := getConfigDir() | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return filepath.Join(confDir, "config.json"), nil | ||
} | ||
|
||
// returns the path to the token file. | ||
// | ||
// might be deleted in future release as `token` file is deprecated. | ||
func TokenPath() (string, error) { | ||
confDir, err := getConfigDir() | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return filepath.Join(confDir, "token"), nil | ||
} | ||
|
||
func InitConfig() error { | ||
configpath, err := ConfigPath() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// create default config if none yet. | ||
if !lib.FileExists(configpath) { | ||
gConfig = NewConfig() | ||
|
||
tokenpath, err := TokenPath() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// if token exists, migrate it to the config file. | ||
if lib.FileExists(tokenpath) { | ||
token, err := ReadTokenFile() | ||
if err != nil { | ||
return err | ||
} | ||
gConfig.Token = token | ||
|
||
// remove the existing token file | ||
if err := os.Remove(tokenpath); err != nil { | ||
return err | ||
} | ||
} | ||
} else { | ||
config, err := ReadConfig() | ||
if err != nil { | ||
return err | ||
} | ||
gConfig = config | ||
} | ||
|
||
if err := SaveConfig(gConfig); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// returns a new, default config. | ||
func NewConfig() Config { | ||
return Config{ | ||
CacheEnabled: true, | ||
Token: "", | ||
} | ||
} | ||
|
||
// reads `token` file for migration of token to config file. | ||
// | ||
// might be deleted in future release as `token` file is deprecated. | ||
func ReadTokenFile() (string, error) { | ||
path, err := TokenPath() | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
token, err := ioutil.ReadFile(path) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return string(token), nil | ||
} | ||
|
||
// save the config to file. | ||
func SaveConfig(config Config) error { | ||
configPath, err := ConfigPath() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
jsonData, err := json.Marshal(config) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if err := ioutil.WriteFile(configPath, jsonData, 0644); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// returns the values of config file. | ||
func ReadConfig() (Config, error) { | ||
configPath, err := ConfigPath() | ||
if err != nil { | ||
return Config{}, err | ||
} | ||
|
||
data, err := ioutil.ReadFile(configPath) | ||
if err != nil { | ||
return Config{}, err | ||
} | ||
|
||
var config Config | ||
if err := json.Unmarshal(data, &config); err != nil { | ||
return Config{}, err | ||
} | ||
|
||
return config, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package main | ||
|
||
import "fmt" | ||
|
||
func init() { | ||
if err := InitConfig(); err != nil { | ||
fmt.Println("warn: error in creating config file.") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.