Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 23 additions & 16 deletions configs/directories.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,32 @@ package configs

import (
"fmt"
"os"
"os/user"
"runtime"

"github.com/arduino/go-paths-helper"
"github.com/arduino/go-win32-utils"
)

// getUserHomeDir returns user's home directory from $HOME or then from os/user
func getUserHomeDir() string {
home := os.Getenv("HOME")
if home == "" {
usr, err := user.Current()
if err != nil {
panic(fmt.Errorf("retrieving user home dir: %s", err))
}
home = usr.HomeDir
}
return home
}

// getDefaultConfigFilePath returns the default path for arduino-cli.yaml
func getDefaultConfigFilePath() *paths.Path {
usr, err := user.Current()
if err != nil {
panic(fmt.Errorf("retrieving user home dir: %s", err))
}
arduinoDataDir := paths.New(usr.HomeDir)
userHomeDir := getUserHomeDir()

arduinoDataDir := paths.New(userHomeDir)

switch runtime.GOOS {
case "linux":
Expand All @@ -53,11 +65,9 @@ func getDefaultConfigFilePath() *paths.Path {
}

func getDefaultArduinoDataDir() (*paths.Path, error) {
usr, err := user.Current()
if err != nil {
return nil, fmt.Errorf("retrieving user home dir: %s", err)
}
arduinoDataDir := paths.New(usr.HomeDir)
userHomeDir := getUserHomeDir()

arduinoDataDir := paths.New(userHomeDir)

switch runtime.GOOS {
case "linux":
Expand All @@ -77,16 +87,13 @@ func getDefaultArduinoDataDir() (*paths.Path, error) {
}

func getDefaultSketchbookDir() (*paths.Path, error) {
usr, err := user.Current()
if err != nil {
return nil, fmt.Errorf("retrieving home dir: %s", err)
}
userHomeDir := getUserHomeDir()

switch runtime.GOOS {
case "linux":
return paths.New(usr.HomeDir).Join("Arduino"), nil
return paths.New(userHomeDir).Join("Arduino"), nil
case "darwin":
return paths.New(usr.HomeDir).Join("Documents", "Arduino"), nil
return paths.New(userHomeDir).Join("Documents", "Arduino"), nil
case "windows":
documentsPath, err := win32.GetDocumentsFolder()
if err != nil {
Expand Down