Skip to content

Commit 640587a

Browse files
committed
Use more appropriate directory folders for config.ini
1 parent fd31f87 commit 640587a

File tree

2 files changed

+53
-4
lines changed

2 files changed

+53
-4
lines changed

config.go

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
// Copyright 2023 Arduino SA
2+
//
3+
// This program is free software: you can redistribute it and/or modify
4+
// it under the terms of the GNU Affero General Public License as published
5+
// by the Free Software Foundation, either version 3 of the License, or
6+
// (at your option) any later version.
7+
//
8+
// This program is distributed in the hope that it will be useful,
9+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
// GNU Affero General Public License for more details.
12+
//
13+
// You should have received a copy of the GNU Affero General Public License
14+
// along with this program. If not, see <https://www.gnu.org/licenses/>.
15+
16+
package main
17+
18+
import (
19+
"fmt"
20+
"os"
21+
22+
"github.com/arduino/go-paths-helper"
23+
)
24+
25+
// getDefaultArduinoCreateConfigDir returns the full path to the default arduino create agent data directory
26+
func getDefaultArduinoCreateConfigDir() (*paths.Path, error) {
27+
// UserConfigDir returns the default root directory to use
28+
// for user-specific configuration data. Users should create
29+
// their own application-specific subdirectory within this
30+
// one and use that.
31+
//
32+
// On Unix systems, it returns $XDG_CONFIG_HOME as specified by
33+
// https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html
34+
// if non-empty, else $HOME/.config.
35+
//
36+
// On Darwin, it returns $HOME/Library/Application Support.
37+
// On Windows, it returns %AppData%.
38+
// On Plan 9, it returns $home/lib.
39+
//
40+
// If the location cannot be determined (for example, $HOME
41+
// is not defined), then it will return an error.
42+
configDir, err := os.UserConfigDir()
43+
if err != nil {
44+
return nil, err
45+
}
46+
47+
agentConfigDir := paths.New(configDir, "ArduinoCreateAgent")
48+
if err := agentConfigDir.MkdirAll(); err != nil {
49+
return nil, fmt.Errorf("cannot create config dir: %s", err)
50+
}
51+
return agentConfigDir, nil
52+
}

main.go

+1-4
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import (
2424
"flag"
2525
"io/ioutil"
2626
"os"
27-
"os/user"
2827
"runtime"
2928
"runtime/debug"
3029
"strconv"
@@ -192,9 +191,7 @@ func loop() {
192191
src, _ := os.Executable()
193192
srcPath := paths.New(src) // The path of the agent's binary
194193
srcDir := srcPath.Parent() // The directory of the agent's binary
195-
usr, _ := user.Current()
196-
usrDir := paths.New(usr.HomeDir) // The user folder, on linux/macos /home/<usr>/
197-
agentDir := usrDir.Join(".arduino-create")
194+
agentDir, err := getDefaultArduinoCreateConfigDir()
198195

199196
// Instantiate Tools
200197
Tools = tools.Tools{

0 commit comments

Comments
 (0)