Merge pull request #12372 from influxdata/create_directory_onboarding

fix(setup): create dir when influxdb ran as a different user
pull/12399/head
kelwang 2019-03-06 13:21:07 -05:00 committed by GitHub
commit 0ae463f982
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 20 additions and 9 deletions

View File

@ -47,15 +47,19 @@ type Flags struct {
var flags Flags
func defaultTokenPath() string {
func defaultTokenPath() (string, string, error) {
dir, err := fs.InfluxDir()
if err != nil {
return ""
return "", "", err
}
return filepath.Join(dir, "credentials")
return filepath.Join(dir, "credentials"), dir, nil
}
func getTokenFromPath(path string) (string, error) {
func getTokenFromDefaultPath() (string, error) {
path, _, err := defaultTokenPath()
if err != nil {
return "", err
}
b, err := ioutil.ReadFile(path)
if err != nil {
return "", err
@ -63,7 +67,10 @@ func getTokenFromPath(path string) (string, error) {
return string(b), nil
}
func writeTokenToPath(tok string, path string) error {
func writeTokenToPath(tok, path, dir string) error {
if err := os.MkdirAll(dir, os.ModePerm); err != nil {
return err
}
return ioutil.WriteFile(path, []byte(tok), 0600)
}
@ -74,7 +81,7 @@ func init() {
viper.BindEnv("TOKEN")
if h := viper.GetString("TOKEN"); h != "" {
flags.token = h
} else if tok, err := getTokenFromPath(defaultTokenPath()); err == nil {
} else if tok, err := getTokenFromDefaultPath(); err == nil {
flags.token = tok
}

View File

@ -71,12 +71,16 @@ func setupF(cmd *cobra.Command, args []string) error {
if err != nil {
return fmt.Errorf("failed to setup instance: %v", err)
}
err = writeTokenToPath(result.Auth.Token, defaultTokenPath())
dPath, dir, err := defaultTokenPath()
if err != nil {
return fmt.Errorf("failed to write token to path %q: %v", defaultTokenPath(), err)
return err
}
err = writeTokenToPath(result.Auth.Token, dPath, dir)
if err != nil {
return fmt.Errorf("failed to write token to path %q: %v", dPath, err)
}
fmt.Println(promptWithColor("Your token has been stored in "+defaultTokenPath()+".", colorCyan))
fmt.Println(promptWithColor("Your token has been stored in "+dPath+".", colorCyan))
w := internal.NewTabWriter(os.Stdout)
w.WriteHeaders(