Skip to content

Commit

Permalink
set default api endpoint to actual endpoint and lazily initialize con…
Browse files Browse the repository at this point in the history
…f file if it cannot be found
  • Loading branch information
advdv committed Mar 10, 2017
1 parent 4bd95b5 commit 9ce9e00
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
31 changes: 30 additions & 1 deletion command/login.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package command

import (
"io/ioutil"
"os"
"path/filepath"

"github.com/jessevdk/go-flags"
"github.com/mitchellh/cli"
homedir "github.com/mitchellh/go-homedir"
"github.com/nerdalize/nerd/nerd/client"
"github.com/nerdalize/nerd/nerd/conf"
"github.com/pkg/errors"
Expand Down Expand Up @@ -66,7 +69,33 @@ func (cmd *Login) DoRun(args []string) error {
}
config, err := conf.Read()
if err != nil {
return errors.Wrap(err, "failed to read nerd config file")
if os.IsNotExist(errors.Cause(err)) {
//@TODO move this to a library, make permissions sensible!
hdir, err := homedir.Dir()
if err != nil {
return errors.Wrap(err, "failed to get home directory")
}

cdir := filepath.Join(hdir, ".nerd")
err = os.MkdirAll(cdir, 0777)
if err != nil {
return errors.Wrap(err, "failed to create config dir")
}

fpath := filepath.Join(cdir, "config.json")
err = ioutil.WriteFile(fpath, []byte(`{}`), 0777)
if err != nil {
return errors.Wrap(err, "failed to initialize config file")
}

config, err = conf.Read()
if err != nil {
return errors.Wrap(err, "failed to re-read after creating conf file")
}

} else {
return errors.Wrap(err, "failed to read nerd config file")
}
}
cl := client.NewAuthAPI(config.Auth.APIEndpoint)
token, err := cl.GetToken(user, pass)
Expand Down
2 changes: 1 addition & 1 deletion nerd/conf/conf.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ OWbQHMK+vvUXieCJvCc9Vj084ABwLBgX
-----END PUBLIC KEY-----`,
},
CurrentProject: "6de308f4-face-11e6-bc64-92361f002671",
NerdAPIEndpoint: "https://platform.nerdalize.net",
NerdAPIEndpoint: "https://batch.nerdalize.com/v1",
}
}

Expand Down

0 comments on commit 9ce9e00

Please sign in to comment.