Commit 2fbbafa0 authored by Piotr Henryk Dabrowski's avatar Piotr Henryk Dabrowski Committed by Massimiliano Pippi

Use os.UserHomeDir() for obtaining user's home directory (#239)

parent 17b110bb
...@@ -19,7 +19,7 @@ package configs ...@@ -19,7 +19,7 @@ package configs
import ( import (
"fmt" "fmt"
"os/user" "os"
"runtime" "runtime"
"github.com/arduino/go-paths-helper" "github.com/arduino/go-paths-helper"
...@@ -28,65 +28,44 @@ import ( ...@@ -28,65 +28,44 @@ import (
// getDefaultConfigFilePath returns the default path for arduino-cli.yaml // getDefaultConfigFilePath returns the default path for arduino-cli.yaml
func getDefaultConfigFilePath() *paths.Path { func getDefaultConfigFilePath() *paths.Path {
usr, err := user.Current() arduinoDataDir, err := getDefaultArduinoDataDir()
if err != nil {
panic(fmt.Errorf("retrieving user home dir: %s", err))
}
arduinoDataDir := paths.New(usr.HomeDir)
switch runtime.GOOS {
case "linux":
arduinoDataDir = arduinoDataDir.Join(".arduino15")
case "darwin":
arduinoDataDir = arduinoDataDir.Join("Library", "arduino15")
case "windows":
localAppDataPath, err := win32.GetLocalAppDataFolder()
if err != nil { if err != nil {
panic(err) panic(err)
} }
arduinoDataDir = paths.New(localAppDataPath).Join("Arduino15")
default:
panic(fmt.Errorf("unsupported OS: %s", runtime.GOOS))
}
return arduinoDataDir.Join("arduino-cli.yaml") return arduinoDataDir.Join("arduino-cli.yaml")
} }
func getDefaultArduinoDataDir() (*paths.Path, error) { func getDefaultArduinoDataDir() (*paths.Path, error) {
usr, err := user.Current() userHomeDir, err := os.UserHomeDir()
if err != nil { if err != nil {
return nil, fmt.Errorf("retrieving user home dir: %s", err) return nil, err
} }
arduinoDataDir := paths.New(usr.HomeDir)
switch runtime.GOOS { switch runtime.GOOS {
case "linux": case "linux":
arduinoDataDir = arduinoDataDir.Join(".arduino15") return paths.New(userHomeDir).Join(".arduino15"), nil
case "darwin": case "darwin":
arduinoDataDir = arduinoDataDir.Join("Library", "arduino15") return paths.New(userHomeDir).Join("Library", "arduino15"), nil
case "windows": case "windows":
localAppDataPath, err := win32.GetLocalAppDataFolder() localAppDataPath, err := win32.GetLocalAppDataFolder()
if err != nil { if err != nil {
return nil, fmt.Errorf("getting LocalAppData path: %s", err) return nil, fmt.Errorf("getting LocalAppData path: %s", err)
} }
arduinoDataDir = paths.New(localAppDataPath).Join("Arduino15") return paths.New(localAppDataPath).Join("Arduino15"), nil
default: default:
return nil, fmt.Errorf("unsupported OS: %s", runtime.GOOS) return nil, fmt.Errorf("unsupported OS: %s", runtime.GOOS)
} }
return arduinoDataDir, nil
} }
func getDefaultSketchbookDir() (*paths.Path, error) { func getDefaultSketchbookDir() (*paths.Path, error) {
usr, err := user.Current() userHomeDir, err := os.UserHomeDir()
if err != nil { if err != nil {
return nil, fmt.Errorf("retrieving home dir: %s", err) return nil, err
} }
switch runtime.GOOS { switch runtime.GOOS {
case "linux": case "linux":
return paths.New(usr.HomeDir).Join("Arduino"), nil return paths.New(userHomeDir).Join("Arduino"), nil
case "darwin": case "darwin":
return paths.New(usr.HomeDir).Join("Documents", "Arduino"), nil return paths.New(userHomeDir).Join("Documents", "Arduino"), nil
case "windows": case "windows":
documentsPath, err := win32.GetDocumentsFolder() documentsPath, err := win32.GetDocumentsFolder()
if err != nil { if err != nil {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment