@@ -22,11 +22,13 @@ package systray
2222import (
2323 "fmt"
2424 "os"
25+ "os/user"
2526 "path/filepath"
2627
2728 log "github.com/sirupsen/logrus"
2829
2930 "github.com/arduino/arduino-create-agent/icon"
31+ "github.com/arduino/go-paths-helper"
3032 "github.com/getlantern/systray"
3133 "github.com/go-ini/ini"
3234 "github.com/skratchdot/open-golang/open"
@@ -101,32 +103,29 @@ func (s *Systray) updateMenuItem(item *systray.MenuItem, disable bool) {
101103
102104// CrashesIsEmpty checks if the folder containing crash-reports is empty
103105func (s * Systray ) CrashesIsEmpty () bool {
104- currDir , err := os .Getwd ()
105- if err != nil {
106- log .Error ("Cannot determine executable path: " , err )
107- }
108- logsDir := filepath .Join (currDir , "logs" )
109- if _ , err := os .Stat (string (logsDir )); os .IsNotExist (err ) {
110- return true
111- }
112- return false
106+ logsDir := getLogsDir ()
107+ return logsDir .NotExist () // if the logs directory is empty we assume there are no crashreports
113108}
114109
115110// RemoveCrashes removes the crash-reports from `logs` folder
116111func (s * Systray ) RemoveCrashes () {
117- currDir , err := os .Getwd ()
118- if err != nil {
119- log .Error ("Cannot determine executable path: " , err )
120- }
121- logsDir := filepath .Join (currDir , "logs" )
122- pathErr := os .RemoveAll (logsDir )
112+ logsDir := getLogsDir ()
113+ pathErr := logsDir .RemoveAll ()
123114 if pathErr != nil {
124- log .Error ("Cannot remove crashreports: " , pathErr )
115+ log .Errorf ("Cannot remove crashreports: %s " , pathErr )
125116 } else {
126- log .Info ("Removed crashreports inside: " , logsDir )
117+ log .Infof ("Removed crashreports inside: %s " , logsDir )
127118 }
128119}
129120
121+ // getLogsDir simply returns the folder containing the logs
122+ func getLogsDir () * paths.Path {
123+ usr , _ := user .Current ()
124+ usrDir := paths .New (usr .HomeDir ) // The user folder, on linux/macos /home/<usr>/
125+ agentDir := usrDir .Join (".arduino-create" )
126+ return agentDir .Join ("logs" )
127+ }
128+
130129// starthibernate creates a systray icon with menu options to resume/quit the agent
131130func (s * Systray ) startHibernate () {
132131 systray .SetIcon (icon .GetIconHiber ())
@@ -189,7 +188,7 @@ type configIni struct {
189188// getconfigs parses all config files in the executable folder
190189func getConfigs () []configIni {
191190 // config.ini must be there, so call it Default
192- src , _ := os .Executable ()
191+ src , _ := os .Executable () // TODO change path
193192 dest := filepath .Dir (src )
194193
195194 var configs []configIni
0 commit comments