88 "os"
99 "path/filepath"
1010
11+ log "github.com/sirupsen/logrus"
12+
1113 "github.com/arduino/arduino-create-agent/icon"
1214 "github.com/getlantern/systray"
1315 "github.com/go-ini/ini"
@@ -41,6 +43,10 @@ func (s *Systray) start() {
4143 mUrl := systray .AddMenuItem ("Go to Arduino Create" , "Arduino Create" )
4244 mDebug := systray .AddMenuItem ("Open Debug Console" , "Debug console" )
4345
46+ // Remove crash-reports
47+ mRmCrashes := systray .AddMenuItem ("Remove crash reports" , "" )
48+ s .updateMenuItem (mRmCrashes , s .CrashesIsEmpty ())
49+
4450 // Add pause/quit
4551 mPause := systray .AddMenuItem ("Pause Plugin" , "" )
4652 systray .AddSeparator ()
@@ -57,6 +63,9 @@ func (s *Systray) start() {
5763 _ = open .Start ("https://create.arduino.cc" )
5864 case <- mDebug .ClickedCh :
5965 _ = open .Start (s .DebugURL ())
66+ case <- mRmCrashes .ClickedCh :
67+ s .RemoveCrashes ()
68+ s .updateMenuItem (mRmCrashes , s .CrashesIsEmpty ())
6069 case <- mPause .ClickedCh :
6170 s .Pause ()
6271 case <- mQuit .ClickedCh :
@@ -66,6 +75,43 @@ func (s *Systray) start() {
6675 }()
6776}
6877
78+ // updateMenuItem will enable or disable an item in the tray icon menu id disable is true
79+ func (s * Systray ) updateMenuItem (item * systray.MenuItem , disable bool ) {
80+ if disable {
81+ item .Disable ()
82+ } else {
83+ item .Enable ()
84+ }
85+ }
86+
87+ // CrashesIsEmpty checks if the folder containing crash-reports is empty
88+ func (s * Systray ) CrashesIsEmpty () bool {
89+ currDir , err := osext .ExecutableFolder ()
90+ if err != nil {
91+ log .Error ("Cannot determine executable path: " , err )
92+ }
93+ logsDir := filepath .Join (currDir , "logs" )
94+ if _ , err := os .Stat (string (logsDir )); os .IsNotExist (err ) {
95+ return true
96+ }
97+ return false
98+ }
99+
100+ // RemoveCrashes removes the crash-reports from `logs` folder
101+ func (s * Systray ) RemoveCrashes () {
102+ currDir , err := osext .ExecutableFolder ()
103+ if err != nil {
104+ log .Error ("Cannot determine executable path: " , err )
105+ }
106+ logsDir := filepath .Join (currDir , "logs" )
107+ pathErr := os .RemoveAll (logsDir )
108+ if pathErr != nil {
109+ log .Error ("Cannot remove crashreports: " , pathErr )
110+ } else {
111+ log .Info ("Removed crashreports inside: " , logsDir )
112+ }
113+ }
114+
69115// starthibernate creates a systray icon with menu options to resume/quit the agent
70116func (s * Systray ) startHibernate () {
71117 systray .SetIcon (icon .GetIconHiber ())
0 commit comments