Skip to content

Commit

Permalink
refactor(config): move global config into core, refactor CLI config l…
Browse files Browse the repository at this point in the history
…oading

Merge pull request #348 from qri-io/config_photo_set
  • Loading branch information
b5 authored Apr 9, 2018
2 parents 01a1f54 + 9310d10 commit cb9f1fe
Show file tree
Hide file tree
Showing 30 changed files with 184 additions and 207 deletions.
3 changes: 3 additions & 0 deletions cmd/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ changes to qri.`,
create a dataset with a metadata and data file:
$ qri add --meta meta.json --data comics.csv me/comic_characters`,
PreRun: func(cmd *cobra.Command, args []string) {
loadConfig()
},
Run: func(cmd *cobra.Command, args []string) {

ingest := (addDsFilepath != "" || addDsMetaFilepath != "" || addDsStructureFilepath != "" || addDsURL != "")
Expand Down
19 changes: 0 additions & 19 deletions cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ import (
"fmt"
"os"
"path/filepath"

"github.com/mitchellh/go-homedir"
)

// ErrExit writes an error to stdout & exits
Expand Down Expand Up @@ -35,23 +33,6 @@ func GetWd() string {
return dir
}

func userHomeDir() string {
dir, err := homedir.Dir()
if err != nil {
panic(err)
}
return dir

// if runtime.GOOS == "windows" {
// home := os.Getenv("HOMEDRIVE") + os.Getenv("HOMEPATH")
// if home == "" {
// home = os.Getenv("USERPROFILE")
// }
// return home
// }
// return os.Getenv("HOME")
}

func loadFileIfPath(path string) (file *os.File, err error) {
if path == "" {
return nil, nil
Expand Down
15 changes: 7 additions & 8 deletions cmd/cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,7 @@ func TestCommandsIntegration(t *testing.T) {
}

path := filepath.Join(os.TempDir(), "qri_test_commands_integration")
// t.Logf("temp path: %s", path)
log.Info(path)
t.Logf("temp path: %s", path)
//clean up if previous cleanup failed
if _, err := os.Stat(path); os.IsNotExist(err) {
os.RemoveAll(path)
Expand Down Expand Up @@ -161,12 +160,12 @@ func TestCommandsIntegration(t *testing.T) {

for i, args := range commands {
func() {
// defer func() {
// if e := recover(); e != nil {
// t.Errorf("case %d unexpected panic executing command\n%s\n%s", i, strings.Join(args, " "), e)
// return
// }
// }()
defer func() {
if e := recover(); e != nil {
t.Errorf("case %d unexpected panic executing command\n%s\n%s", i, strings.Join(args, " "), e)
return
}
}()
_, err := executeCommand(RootCmd, args...)
if err != nil {
t.Errorf("case %d unexpected error executing command\n%s\n%s", i, strings.Join(args, " "), err.Error())
Expand Down
79 changes: 26 additions & 53 deletions cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,28 @@ package cmd
import (
"encoding/json"
"fmt"
"github.com/qri-io/qri/core"
"io/ioutil"
"os"
"path/filepath"

golog "github.com/ipfs/go-log"
"github.com/qri-io/qri/config"
"github.com/spf13/cobra"
"gopkg.in/yaml.v2"
)

var (
// cfg is the global configuration object for the CLI
cfg *config.Config
// setting ignoreCfg to true will prevent loadConfig from doing anything
ignoreCfg bool
func configFilepath() string {
if cfgFile == "" {
return filepath.Join(QriRepoPath, "config.yaml")
}
return cfgFile
}

getConfigFilepath, setConfigFilepath string
)
func loadConfig() {
core.ConfigFilepath = configFilepath()
if err := core.LoadConfig(core.ConfigFilepath); err != nil {
ErrExit(err)
}
}

// configCmd represents commands that read & modify configuration settings
var configCmd = &cobra.Command{
Expand All @@ -47,9 +51,12 @@ runtime via command a line argument.`,
var configGetCommand = &cobra.Command{
Use: "get",
Short: "Show a configuration setting",
PreRun: func(cmd *cobra.Command, args []string) {
loadConfig()
},
Run: func(cmd *cobra.Command, args []string) {
for _, path := range args {
value, err := cfg.Get(path)
value, err := core.Config.Get(path)
ExitIfErr(err)
data, err := yaml.Marshal(value)
ExitIfErr(err)
Expand All @@ -61,6 +68,9 @@ var configGetCommand = &cobra.Command{
var configSetCommand = &cobra.Command{
Use: "set",
Short: "Set a configuration option",
PreRun: func(cmd *cobra.Command, args []string) {
loadConfig()
},
Run: func(cmd *cobra.Command, args []string) {
// var err error
if len(args)%2 != 0 {
Expand All @@ -72,11 +82,11 @@ var configSetCommand = &cobra.Command{
err := yaml.Unmarshal([]byte(args[i+1]), &value)
ExitIfErr(err)

err = cfg.Set(args[i], value)
err = core.Config.Set(args[i], value)
ExitIfErr(err)
}

err := cfg.WriteToFile(configFilepath())
err := core.Config.WriteToFile(core.ConfigFilepath)
ExitIfErr(err)
printSuccess("config updated")
},
Expand All @@ -92,10 +102,14 @@ export makes it easier to share your qri configuration settings.
We've added the --with-private-keys option to include private keys in the export
PLEASE PLEASE PLEASE NEVER SHARE YOUR PRIVATE KEYS WITH ANYONE. EVER.
Anyone with your private keys can impersonate you on qri.`,
PreRun: func(cmd *cobra.Command, args []string) {
loadConfig()
},
Run: func(cmd *cobra.Command, args []string) {
var (
data []byte
err error
cfg = core.Config
)

wpk, err := cmd.Flags().GetBool("with-private-keys")
Expand Down Expand Up @@ -139,49 +153,8 @@ Anyone with your private keys can impersonate you on qri.`,
},
}

func configFilepath() string {
return filepath.Join(QriRepoPath, "config.yaml")
}

func loadConfig() (err error) {
if ignoreCfg {
return nil
}

cfg, err = config.ReadFromFile(configFilepath())

if err == nil && cfg.Profile == nil {
err = fmt.Errorf("missing profile")
}

if err != nil {
str := `couldn't read config file. error
%s
if you've recently updated qri your config file may no longer be valid.
The easiest way to fix this is to delete your repository at:
%s
and start with a fresh qri install by running 'qri setup' again.
Sorry, we know this is not exactly a great experience, from this point forward
we won't be shipping changes that require starting over.
`
err = fmt.Errorf(str, err.Error(), QriRepoPath)
}

// configure logging straight away
if cfg != nil && cfg.Logging != nil {
for name, level := range cfg.Logging.Levels {
golog.SetLogLevel(name, level)
}
}

return err
}

func init() {
configGetCommand.Flags().StringVarP(&getConfigFilepath, "file", "f", "", "file to save YAML config info to")
configCmd.AddCommand(configGetCommand)

configSetCommand.Flags().StringVarP(&setConfigFilepath, "file", "f", "", "filepath to *complete* yaml config info file")
configCmd.AddCommand(configSetCommand)

configExportCmd.Flags().Bool("with-private-keys", false, "include private keys in export")
Expand Down
6 changes: 5 additions & 1 deletion cmd/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"github.com/qri-io/qri/api"
"github.com/qri-io/qri/config"
"github.com/qri-io/qri/core"
"github.com/qri-io/qri/repo"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -37,6 +38,9 @@ things:
When you run connect you are connecting to the distributed web, interacting with
peers & swapping data.`,
PreRun: func(cmd *cobra.Command, args []string) {
loadConfig()
},
Run: func(cmd *cobra.Command, args []string) {
var (
r repo.Repo
Expand All @@ -50,7 +54,7 @@ peers & swapping data.`,
r = getRepo(true)

s, err := api.New(r, func(c *config.Config) {
*c = *cfg
*c = *core.Config

if connectCmdAPIPort != "" {
c.API.Port = connectCmdAPIPort
Expand Down
3 changes: 3 additions & 0 deletions cmd/connections.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ var connectionsCmd = &cobra.Command{
show all IPFS connections:
$ qri connections --ipfs`,
PreRun: func(cmd *cobra.Command, args []string) {
loadConfig()
},
Run: func(cmd *cobra.Command, args []string) {
if len(args) != 0 {
ErrExit(fmt.Errorf("connections accepts no arguments"))
Expand Down
3 changes: 3 additions & 0 deletions cmd/data.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ var dataCmd = &cobra.Command{
Data reads records from a dataset`,
Example: ` show the first 50 rows of a dataset:
$ qri data me/dataset_name`,
PreRun: func(cmd *cobra.Command, args []string) {
loadConfig()
},
Run: func(cmd *cobra.Command, args []string) {
if len(args) == 0 {
fmt.Println("please specify a dataset name to retrieve data")
Expand Down
3 changes: 3 additions & 0 deletions cmd/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ Diff compares two datasets from your repo and prints a represntation
of the differences between them. You can specifify the datasets
either by name or by their hash`,
Example: `todo`,
PreRun: func(cmd *cobra.Command, args []string) {
loadConfig()
},
Run: func(cmd *cobra.Command, args []string) {
for i, arg := range args {
fmt.Printf("%d: %s\n", i, arg)
Expand Down
3 changes: 3 additions & 0 deletions cmd/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ Export gets datasets out of qri. By default it exports only a dataset’s data t
the path [current directory]/[peername]/[dataset name]/[data file].
To export everything about a dataset, use the --dataset flag.`,
PreRun: func(cmd *cobra.Command, args []string) {
loadConfig()
},
Run: func(cmd *cobra.Command, args []string) {
if len(args) == 0 {
fmt.Println("please specify a dataset name to export")
Expand Down
3 changes: 3 additions & 0 deletions cmd/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ var infoCmd = &cobra.Command{
get info for a dataset at a specific version:
$ qri info QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn`,
PreRun: func(cmd *cobra.Command, args []string) {
loadConfig()
},
Run: func(cmd *cobra.Command, args []string) {
if len(args) == 0 {
// ErrExit(fmt.Errorf("please specify a dataset path or name to get the info of"))
Expand Down
3 changes: 3 additions & 0 deletions cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ The default list is the latest version of all datasets you have on your local
qri repository.`,
Example: ` show all of your datasets:
$ qri list`,
PreRun: func(cmd *cobra.Command, args []string) {
loadConfig()
},
Run: func(cmd *cobra.Command, args []string) {
if len(args) < 1 {
// TODO - add limit & offset params
Expand Down
3 changes: 3 additions & 0 deletions cmd/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ details in order of occurrence, starting with the most recent known version,
working backwards in time.`,
Example: ` show log for the dataset b5/precip:
$ qri log b5/precip`,
PreRun: func(cmd *cobra.Command, args []string) {
loadConfig()
},
Run: func(cmd *cobra.Command, args []string) {
if len(args) < 1 {
ErrExit(fmt.Errorf("please specify a dataset reference to log"))
Expand Down
44 changes: 0 additions & 44 deletions cmd/logging.go

This file was deleted.

3 changes: 3 additions & 0 deletions cmd/peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ var peerInfoCmd = &cobra.Command{
Short: `Get info on a qri peer`,
Example: ` show info on a user named "mr0grog":
$ qri peer info mr0grog`,
PreRun: func(cmd *cobra.Command, args []string) {
loadConfig()
},
Run: func(cmd *cobra.Command, args []string) {
if len(args) < 1 {
ErrExit(fmt.Errorf("peer name is required"))
Expand Down
3 changes: 3 additions & 0 deletions cmd/peers.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ var peersCmd = &cobra.Command{
Long: `peers lists the peers your qri node has seen before`,
Example: ` list qri peers:
$ qri peers`,
PreRun: func(cmd *cobra.Command, args []string) {
loadConfig()
},
Run: func(cmd *cobra.Command, args []string) {
outformat := cmd.Flag("format").Value.String()
if outformat != "" {
Expand Down
Loading

0 comments on commit cb9f1fe

Please sign in to comment.