Skip to content

Commit c611395

Browse files
committed
move config.go to it's own package and make functions public.
This way we have a single source of truth.
1 parent ea1be68 commit c611395

File tree

6 files changed

+35
-36
lines changed

6 files changed

+35
-36
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ public/
1010
artifacts*
1111
logs/
1212

13+
config/config.ini
14+
1315
# IDEs config
1416
.idea
1517
.vscode

certificates.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"text/template"
2424
"time"
2525

26+
"github.com/arduino/arduino-create-agent/config"
2627
"github.com/arduino/go-paths-helper"
2728
"github.com/gin-gonic/gin"
2829
log "github.com/sirupsen/logrus"
@@ -271,7 +272,7 @@ func certHandler(c *gin.Context) {
271272
}
272273

273274
func deleteCertHandler(c *gin.Context) {
274-
DeleteCertificates(getCertificatesDir())
275+
DeleteCertificates(config.GetCertificatesDir())
275276
}
276277

277278
// DeleteCertificates will delete the certificates

config.go renamed to config/config.go

+16-13
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
// You should have received a copy of the GNU Affero General Public License
1414
// along with this program. If not, see <https://www.gnu.org/licenses/>.
1515

16-
package main
16+
package config
1717

1818
import (
1919
_ "embed"
@@ -23,13 +23,13 @@ import (
2323
log "github.com/sirupsen/logrus"
2424
)
2525

26-
// getCertificatesDir return the directory where SSL certificates are saved
27-
func getCertificatesDir() *paths.Path {
28-
return getDataDir()
26+
// GetCertificatesDir return the directory where SSL certificates are saved
27+
func GetCertificatesDir() *paths.Path {
28+
return GetDataDir()
2929
}
3030

31-
// getDataDir returns the full path to the default Arduino Create Agent data directory.
32-
func getDataDir() *paths.Path {
31+
// GetDataDir returns the full path to the default Arduino Create Agent data directory.
32+
func GetDataDir() *paths.Path {
3333
userDir, err := os.UserHomeDir()
3434
if err != nil {
3535
log.Panicf("Could not get user dir: %s", err)
@@ -41,17 +41,17 @@ func getDataDir() *paths.Path {
4141
return dataDir
4242
}
4343

44-
// getLogsDir return the directory where logs are saved
45-
func getLogsDir() *paths.Path {
46-
logsDir := getDataDir().Join("logs")
44+
// GetLogsDir return the directory where logs are saved
45+
func GetLogsDir() *paths.Path {
46+
logsDir := GetDataDir().Join("logs")
4747
if err := logsDir.MkdirAll(); err != nil {
4848
log.Panicf("Can't create logs dir: %s", err)
4949
}
5050
return logsDir
5151
}
5252

53-
// getDefaultConfigDir returns the full path to the default Arduino Create Agent configuration directory.
54-
func getDefaultConfigDir() *paths.Path {
53+
// GetDefaultConfigDir returns the full path to the default Arduino Create Agent configuration directory.
54+
func GetDefaultConfigDir() *paths.Path {
5555
// UserConfigDir returns the default root directory to use
5656
// for user-specific configuration data. Users should create
5757
// their own application-specific subdirectory within this
@@ -79,13 +79,16 @@ func getDefaultConfigDir() *paths.Path {
7979
return agentConfigDir
8080
}
8181

82+
// https://github.com/golang/go/issues/46056
83+
//
84+
//go:generate cp -r ../config.ini config.ini
8285
//go:embed config.ini
8386
var configContent []byte
8487

85-
// generateConfig function will take a directory path as an input
88+
// GenerateConfig function will take a directory path as an input
8689
// and will write the default config,ini file to that directory,
8790
// it will panic if something goes wrong
88-
func generateConfig(destDir *paths.Path) *paths.Path {
91+
func GenerateConfig(destDir *paths.Path) *paths.Path {
8992
configPath := destDir.Join("config.ini")
9093

9194
// generate the config.ini file directly in destDir

hub.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"strconv"
2727
"strings"
2828

29+
"github.com/arduino/arduino-create-agent/config"
2930
"github.com/arduino/arduino-create-agent/upload"
3031
log "github.com/sirupsen/logrus"
3132
)
@@ -182,7 +183,7 @@ func checkCmd(m []byte) {
182183
} else if strings.HasPrefix(sl, "downloadtool") {
183184
// Always delete root certificates when we receive a downloadtool command
184185
// Useful if the install procedure was not followed strictly (eg. manually)
185-
DeleteCertificates(getCertificatesDir())
186+
DeleteCertificates(config.GetCertificatesDir())
186187
go func() {
187188
args := strings.Split(s, " ")
188189
var tool, toolVersion, pack, behaviour string

main.go

+10-9
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import (
3131
"time"
3232

3333
cors "github.com/andela/gin-cors"
34+
"github.com/arduino/arduino-create-agent/config"
3435
"github.com/arduino/arduino-create-agent/systray"
3536
"github.com/arduino/arduino-create-agent/tools"
3637
"github.com/arduino/arduino-create-agent/updater"
@@ -127,17 +128,17 @@ func main() {
127128

128129
// Generate certificates
129130
if *genCert {
130-
generateCertificates(getCertificatesDir())
131+
generateCertificates(config.GetCertificatesDir())
131132
os.Exit(0)
132133
}
133134
// Check if certificates made with Agent <=1.2.7 needs to be moved over the new location
134-
migrateCertificatesGeneratedWithOldAgentVersions(getCertificatesDir())
135+
migrateCertificatesGeneratedWithOldAgentVersions(config.GetCertificatesDir())
135136

136137
// Launch main loop in a goroutine
137138
go loop()
138139

139140
// SetupSystray is the main thread
140-
configDir := getDefaultConfigDir()
141+
configDir := config.GetDefaultConfigDir()
141142
Systray = systray.Systray{
142143
Hibernate: *hibernate,
143144
Version: version + "-" + commit,
@@ -167,7 +168,7 @@ func loop() {
167168

168169
// Instantiate Tools
169170
Tools = tools.Tools{
170-
Directory: getDataDir().String(),
171+
Directory: config.GetDataDir().String(),
171172
IndexURL: *indexURL,
172173
Logger: func(msg string) {
173174
mapD := map[string]string{"DownloadStatus": "Pending", "Msg": msg}
@@ -178,7 +179,7 @@ func loop() {
178179
Tools.Init(requiredToolsAPILevel)
179180

180181
// Let's handle the config
181-
configDir := getDefaultConfigDir()
182+
configDir := config.GetDefaultConfigDir()
182183
var configPath *paths.Path
183184

184185
// see if the env var is defined, if it is take the config from there, this will override the default path
@@ -207,7 +208,7 @@ func loop() {
207208
}
208209
}
209210
if configPath == nil {
210-
configPath = generateConfig(configDir)
211+
configPath = config.GenerateConfig(configDir)
211212
}
212213

213214
// Parse the config.ini
@@ -316,7 +317,7 @@ func loop() {
316317
if *crashreport {
317318
logFilename := "crashreport_" + time.Now().Format("20060102150405") + ".log"
318319
// handle logs directory creation
319-
logsDir := getLogsDir()
320+
logsDir := config.GetLogsDir()
320321
logFile, err := os.OpenFile(logsDir.Join(logFilename).String(), os.O_WRONLY|os.O_CREATE|os.O_SYNC|os.O_APPEND, 0644)
321322
if err != nil {
322323
log.Print("Cannot create file used for crash-report")
@@ -376,12 +377,12 @@ func loop() {
376377
r.POST("/update", updateHandler)
377378

378379
// Mount goa handlers
379-
goa := v2.Server(getDataDir().String())
380+
goa := v2.Server(config.GetDataDir().String())
380381
r.Any("/v2/*path", gin.WrapH(goa))
381382

382383
go func() {
383384
// check if certificates exist; if not, use plain http
384-
certsDir := getCertificatesDir()
385+
certsDir := config.GetCertificatesDir()
385386
if certsDir.Join("cert.pem").NotExist() {
386387
log.Error("Could not find HTTPS certificate. Using plain HTTP only.")
387388
return

systray/systray_real.go

+3-12
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,11 @@ package systray
2121

2222
import (
2323
"os"
24-
"os/user"
2524

2625
log "github.com/sirupsen/logrus"
2726

27+
"github.com/arduino/arduino-create-agent/config"
2828
"github.com/arduino/arduino-create-agent/icon"
29-
"github.com/arduino/go-paths-helper"
3029
"github.com/getlantern/systray"
3130
"github.com/go-ini/ini"
3231
"github.com/skratchdot/open-golang/open"
@@ -104,13 +103,13 @@ func (s *Systray) updateMenuItem(item *systray.MenuItem, disable bool) {
104103

105104
// CrashesIsEmpty checks if the folder containing crash-reports is empty
106105
func (s *Systray) CrashesIsEmpty() bool {
107-
logsDir := getLogsDir()
106+
logsDir := config.GetLogsDir()
108107
return logsDir.NotExist() // if the logs directory is empty we assume there are no crashreports
109108
}
110109

111110
// RemoveCrashes removes the crash-reports from `logs` folder
112111
func (s *Systray) RemoveCrashes() {
113-
logsDir := getLogsDir()
112+
logsDir := config.GetLogsDir()
114113
pathErr := logsDir.RemoveAll()
115114
if pathErr != nil {
116115
log.Errorf("Cannot remove crashreports: %s", pathErr)
@@ -119,14 +118,6 @@ func (s *Systray) RemoveCrashes() {
119118
}
120119
}
121120

122-
// getLogsDir simply returns the folder containing the logs
123-
func getLogsDir() *paths.Path {
124-
usr, _ := user.Current()
125-
usrDir := paths.New(usr.HomeDir) // The user folder, on linux/macos /home/<usr>/
126-
agentDir := usrDir.Join(".arduino-create")
127-
return agentDir.Join("logs")
128-
}
129-
130121
// starthibernate creates a systray icon with menu options to resume/quit the agent
131122
func (s *Systray) startHibernate() {
132123
systray.SetIcon(icon.GetIconHiber())

0 commit comments

Comments
 (0)