diff --git a/lib/app/app.go b/lib/app/app.go index b9ca6fa..3ccfc51 100644 --- a/lib/app/app.go +++ b/lib/app/app.go @@ -7,6 +7,7 @@ import ( "os" "os/exec" "os/signal" + "path" "sync" "syscall" @@ -94,6 +95,7 @@ func (a *Application) init() { a.Workflow = a.loadWorkflowFile(a.ConfigFilename) a.JsEngine = CreateNewJavascriptEngine() a.cronEngine = cron.New(cron.WithChain(cron.SkipIfStillRunning(cron.DiscardLogger))) + a.DownloadApplicationIcon() } func (a *Application) hookSignals() { @@ -352,6 +354,26 @@ func (a *Application) handleFlagOptions() { } } +func (a *Application) GetConfigurationPath() string { + pathname, _ := utils.EnsureConfigDirExists("stackup") + + return pathname +} + +func (a *Application) DownloadApplicationIcon() { + filename := a.GetApplicationIconPath() + + if utils.FileExists(filename) && utils.IsFile(filename) { + return + } + + utils.SaveUrlToFile("https://raw.githubusercontent.com/permafrost-dev/stackup/main/assets/stackup-app-512px.png", filename) +} + +func (a *Application) GetApplicationIconPath() string { + return path.Join(a.GetConfigurationPath(), "/stackup-icon.png") +} + func (a *Application) Run() { godotenv.Load() a.init() diff --git a/lib/app/scriptNotifications.go b/lib/app/scriptNotifications.go index 6961642..6dac554 100644 --- a/lib/app/scriptNotifications.go +++ b/lib/app/scriptNotifications.go @@ -60,7 +60,7 @@ func (dn *DesktopNotification) create() *DesktopNotification { } func (dn *DesktopNotification) Send() bool { - result := notifications.NewDesktopNotification(). + result := notifications.NewDesktopNotification(App.GetApplicationIconPath()). Send(dn.state.title, dn.state.message) dn.resetState() @@ -72,10 +72,14 @@ func (dn *DesktopNotification) resetState() { dn.state.message = "" } -func (dn *DesktopNotification) Message(message string) *DesktopNotification { +func (dn *DesktopNotification) Message(message string, title ...string) *DesktopNotification { dn.state.message = message dn.state.title = "notification" + if len(title) > 0 { + dn.state.title = title[0] + } + return dn } diff --git a/lib/notifications/desktop.go b/lib/notifications/desktop.go index a1a5ee2..bba11ad 100644 --- a/lib/notifications/desktop.go +++ b/lib/notifications/desktop.go @@ -5,14 +5,17 @@ import ( ) type DesktopNotification struct { + IconPath string } -// NewDesktopNotification creates a new instance of the DesktopNotification -// struct. -func NewDesktopNotification() *DesktopNotification { - return &DesktopNotification{} +func NewDesktopNotification(iconPath string) *DesktopNotification { + return &DesktopNotification{ + IconPath: iconPath, + } } -func (tn *DesktopNotification) Send(title, message string) error { - return beeep.Notify(title, message, "") +func (dn *DesktopNotification) Send(title, message string) error { + beeep.DefaultDuration = 8 + + return beeep.Notify(title, message, dn.IconPath) } diff --git a/lib/utils/utils.go b/lib/utils/utils.go index 7b1faa8..ec106db 100644 --- a/lib/utils/utils.go +++ b/lib/utils/utils.go @@ -213,6 +213,13 @@ func IsDir(filename string) bool { return info.IsDir() } +func FileExists(filename string) bool { + if _, err := os.Stat(filename); err == nil { + return true + } + return false +} + func MatchPattern(s string, pattern string) []string { regex := regexp.MustCompile(pattern) if !regex.MatchString(s) { @@ -356,3 +363,21 @@ func GetUniqueStrings(items []string) []string { return uniqueItems } + +func EnsureConfigDirExists(appName string) (string, error) { + homeDir, err := os.UserHomeDir() + if err != nil { + return "", err + } + + // Append the directory name to the home directory + configDir := filepath.Join(homeDir, "."+appName) + + // Ensure the directory exists + err = os.MkdirAll(configDir, 0744) + if err != nil { + return "", err + } + + return configDir, nil +}