package browserlauncher import ( "fmt" "os/exec" "runtime" "git.weirdcat.su/weirdcat/auto-attendance/internal/config" "git.weirdcat.su/weirdcat/auto-attendance/internal/logger" ) type BrowserLauncher interface { OpenDefault(url string) error OpenCustom(url string) error OpenAuto(url string) error } type browserLauncherImpl struct { config *config.Config log *logger.Logger useCustomCommand bool customCommand string } // OpenAuto implements BrowserLauncher. func (b *browserLauncherImpl) OpenAuto(url string) error { if b.useCustomCommand { return b.OpenCustom(url) } else { return b.OpenDefault(url) } } // OpenCustom implements BrowserLauncher. func (b *browserLauncherImpl) OpenCustom(url string) error { command := fmt.Sprintf(b.customCommand, url) b.log.Debug("opening link with custom command", "command", command, "url", url) err := exec.Command(command).Start() if err != nil { b.log.Error("failed to open link with custom command", "command", command, "url", url, "error", err) } return err } // OpenDefault implements BrowserLauncher. func (b *browserLauncherImpl) OpenDefault(url string) (err error) { b.log.Debug("opening link with default browser", "url", url) switch runtime.GOOS { case "windows": err = exec.Command("rundll32", "url.dll,FileProtocolHandler", url).Start() default: err = exec.Command("xdg-open", url).Start() } if err != nil { b.log.Error("failed to open link with default browser", "url", url, "error", err) } return err } func NewBrowserLauncher(config *config.Config, log *logger.Logger) BrowserLauncher { useCustomCommand := config.App.UseCustomBrowserCommand customCommand := config.App.BrowserOpenCommand return &browserLauncherImpl{ config: config, log: log, useCustomCommand: useCustomCommand, customCommand: customCommand, } }