2019-02-18 18:44:27 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
|
2023-02-18 23:13:23 +00:00
|
|
|
"git.blob42.xyz/gomark/gosuki/modules"
|
|
|
|
"git.blob42.xyz/gomark/gosuki/parsing"
|
2023-09-05 17:45:21 +00:00
|
|
|
"git.blob42.xyz/gomark/gosuki/profiles"
|
2023-02-18 23:13:23 +00:00
|
|
|
"git.blob42.xyz/gomark/gosuki/utils"
|
|
|
|
"git.blob42.xyz/gomark/gosuki/watch"
|
2020-08-12 18:15:17 +00:00
|
|
|
|
2023-02-18 23:13:23 +00:00
|
|
|
"git.blob42.xyz/sp4ke/gum"
|
2019-02-26 18:41:02 +00:00
|
|
|
|
2020-08-12 18:13:01 +00:00
|
|
|
"github.com/urfave/cli/v2"
|
2019-02-18 18:44:27 +00:00
|
|
|
)
|
|
|
|
|
2023-02-18 22:08:12 +00:00
|
|
|
var startDaemonCmd = &cli.Command{
|
|
|
|
Name: "daemon",
|
|
|
|
Aliases: []string{"d"},
|
2019-02-18 18:44:27 +00:00
|
|
|
Usage: "run browser watchers",
|
2023-01-31 21:21:39 +00:00
|
|
|
// Category: "daemon"
|
2023-02-18 22:08:12 +00:00
|
|
|
Action: startDaemon,
|
2019-02-18 18:44:27 +00:00
|
|
|
}
|
|
|
|
|
2023-02-18 22:08:12 +00:00
|
|
|
func startDaemon(c *cli.Context) error {
|
2020-09-09 13:07:29 +00:00
|
|
|
defer utils.CleanFiles()
|
2019-02-18 18:44:27 +00:00
|
|
|
manager := gum.NewManager()
|
|
|
|
manager.ShutdownOn(os.Interrupt)
|
|
|
|
|
|
|
|
api := NewApi()
|
|
|
|
manager.AddUnit(api)
|
|
|
|
|
|
|
|
go manager.Run()
|
|
|
|
|
|
|
|
// Initialize sqlite database available in global `cacheDB` variable
|
|
|
|
initDB()
|
|
|
|
|
2023-01-31 21:21:39 +00:00
|
|
|
registeredBrowsers := modules.GetBrowserModules()
|
2019-02-22 18:50:26 +00:00
|
|
|
|
2022-11-07 19:07:13 +00:00
|
|
|
// instanciate all browsers
|
2022-11-01 13:27:19 +00:00
|
|
|
for _, browserMod := range registeredBrowsers {
|
2022-10-27 22:33:20 +00:00
|
|
|
|
2022-11-01 13:27:19 +00:00
|
|
|
mod := browserMod.ModInfo()
|
|
|
|
|
2023-02-18 22:08:12 +00:00
|
|
|
// Create context
|
|
|
|
modContext := &modules.Context{
|
|
|
|
Cli: c,
|
|
|
|
}
|
|
|
|
|
2023-01-31 21:21:39 +00:00
|
|
|
//Create a browser instance
|
|
|
|
browser, ok := mod.New().(modules.BrowserModule)
|
2022-11-01 13:27:19 +00:00
|
|
|
if !ok {
|
|
|
|
log.Criticalf("module <%s> is not a BrowserModule", mod.ID)
|
|
|
|
}
|
|
|
|
log.Debugf("created browser instance <%s>", browser.Config().Name)
|
|
|
|
|
2022-11-07 19:07:13 +00:00
|
|
|
// shutdown logic
|
2023-01-31 21:21:39 +00:00
|
|
|
s, isShutdowner := browser.(modules.Shutdowner)
|
2022-11-01 13:27:19 +00:00
|
|
|
if isShutdowner {
|
|
|
|
defer handleShutdown(browser.Config().Name, s)
|
|
|
|
}
|
2022-10-27 22:33:20 +00:00
|
|
|
|
2022-11-01 13:27:19 +00:00
|
|
|
log.Debugf("new browser <%s> instance", browser.Config().Name)
|
2023-01-31 21:21:39 +00:00
|
|
|
h, ok := browser.(modules.HookRunner)
|
2022-10-27 22:33:20 +00:00
|
|
|
if ok {
|
2023-02-18 22:08:12 +00:00
|
|
|
//TODO: document hook running on watch events
|
2022-10-27 22:33:20 +00:00
|
|
|
h.RegisterHooks(parsing.ParseTags)
|
|
|
|
}
|
2019-03-01 17:30:50 +00:00
|
|
|
|
2023-09-05 17:45:21 +00:00
|
|
|
//WIP: Handle multiple profiles for modules who announce it - here ?
|
|
|
|
// Check if browser implements ProfileManager
|
|
|
|
//TODO: global flag for watch all
|
|
|
|
// if watch-all then for each profile setup the browser
|
|
|
|
bpm, ok := browser.(profiles.ProfileManager)
|
|
|
|
if ok {
|
|
|
|
//TODO : for each profile spawn a watcher
|
|
|
|
// list profiles
|
|
|
|
profs, err := bpm.GetProfiles()
|
|
|
|
if err != nil {
|
|
|
|
log.Critical("could not get profiles")
|
|
|
|
}
|
|
|
|
for _, p := range profs {
|
|
|
|
log.Debugf("profile: <%s>", p.Name)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
log.Debugf("<%s> does not implement profiles.ProfileManager",
|
|
|
|
browser.Config().Name)
|
|
|
|
}
|
2023-02-18 22:08:12 +00:00
|
|
|
|
|
|
|
|
|
|
|
// calls the setup logic for each browser instance which
|
2022-11-07 19:07:13 +00:00
|
|
|
// includes the browsers.Initializer and browsers.Loader interfaces
|
2023-02-18 22:08:12 +00:00
|
|
|
err := modules.Setup(browser, modContext)
|
2019-03-01 17:30:50 +00:00
|
|
|
if err != nil {
|
2022-11-01 13:27:19 +00:00
|
|
|
log.Errorf("setting up <%s> %v", browser.Config().Name, err)
|
2022-10-27 22:33:20 +00:00
|
|
|
if isShutdowner {
|
2022-11-01 13:27:19 +00:00
|
|
|
handleShutdown(browser.Config().Name, s)
|
2022-10-27 22:33:20 +00:00
|
|
|
}
|
2019-03-01 17:30:50 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-11-01 13:27:19 +00:00
|
|
|
runner, ok := browser.(watch.WatchRunner)
|
|
|
|
if !ok {
|
|
|
|
log.Criticalf("<%s> must implement watch.WatchRunner interface", browser.Config().Name)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2023-02-18 22:08:12 +00:00
|
|
|
log.Infof("start watching <%s>", runner.Watch().ID)
|
2022-11-01 13:27:19 +00:00
|
|
|
watch.SpawnWatcher(runner)
|
2019-02-18 18:44:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
<-manager.Quit
|
2020-08-12 19:11:48 +00:00
|
|
|
|
|
|
|
return nil
|
2019-02-18 18:44:27 +00:00
|
|
|
}
|
2022-11-01 13:27:19 +00:00
|
|
|
|
2023-01-31 21:21:39 +00:00
|
|
|
func handleShutdown(id string, s modules.Shutdowner) {
|
2022-11-01 13:27:19 +00:00
|
|
|
err := s.Shutdown()
|
|
|
|
if err != nil {
|
|
|
|
log.Panicf("could not shutdown browser <%s>", id)
|
|
|
|
}
|
|
|
|
}
|