-
-
Notifications
You must be signed in to change notification settings - Fork 150
/
Copy pathconfig.go
110 lines (97 loc) · 3.69 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// Copyright 2023 Arduino SA
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package config
import (
// we need this for the config ini in this package
_ "embed"
"os"
"github.com/arduino/go-paths-helper"
log "github.com/sirupsen/logrus"
)
// GetCertificatesDir return the directory where SSL certificates are saved
func GetCertificatesDir() *paths.Path {
return GetDataDir()
}
// CertsExist checks if the certs have already been generated
func CertsExist() bool {
certFile := GetCertificatesDir().Join("cert.pem")
return certFile.Exist() //if the certFile is not present we assume there are no certs
}
// GetDataDir returns the full path to the default Arduino Create Agent data directory.
func GetDataDir() *paths.Path {
userDir, err := os.UserHomeDir()
if err != nil {
log.Panicf("Could not get user dir: %s", err)
}
dataDir := paths.New(userDir, ".arduino-create")
if err := dataDir.MkdirAll(); err != nil {
log.Panicf("Could not create data dir: %s", err)
}
return dataDir
}
// GetLogsDir return the directory where logs are saved
func GetLogsDir() *paths.Path {
logsDir := GetDataDir().Join("logs")
if err := logsDir.MkdirAll(); err != nil {
log.Panicf("Can't create logs dir: %s", err)
}
return logsDir
}
// LogsIsEmpty checks if the folder containing crash-reports is empty
func LogsIsEmpty() bool {
return GetLogsDir().NotExist() // if the logs directory is empty we assume there are no crashreports
}
// GetDefaultConfigDir returns the full path to the default Arduino Create Agent configuration directory.
func GetDefaultConfigDir() *paths.Path {
// UserConfigDir returns the default root directory to use
// for user-specific configuration data. Users should create
// their own application-specific subdirectory within this
// one and use that.
//
// On Unix systems, it returns $XDG_CONFIG_HOME as specified by
// https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html
// if non-empty, else $HOME/.config.
//
// On Darwin, it returns $HOME/Library/Application Support.
// On Windows, it returns %AppData%.
// On Plan 9, it returns $home/lib.
//
// If the location cannot be determined (for example, $HOME
// is not defined), then it will return an error.
configDir, err := os.UserConfigDir()
if err != nil {
log.Panicf("Can't get user home dir: %s", err)
}
agentConfigDir := paths.New(configDir, "ArduinoCreateAgent")
if err := agentConfigDir.MkdirAll(); err != nil {
log.Panicf("Can't create config dir: %s", err)
}
return agentConfigDir
}
//go:embed config.ini
var configContent []byte
// GenerateConfig function will take a directory path as an input
// and will write the default config,ini file to that directory,
// it will panic if something goes wrong
func GenerateConfig(destDir *paths.Path) *paths.Path {
configPath := destDir.Join("config.ini")
// generate the config.ini file directly in destDir
if err := configPath.WriteFile(configContent); err != nil {
// if we do not have a config there's nothing else we can do
panic("cannot generate config: " + err.Error())
}
log.Infof("generated config in %s", configPath)
return configPath
}