2019-02-18 18:44:27 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2023-09-05 19:11:21 +00:00
|
|
|
"fmt"
|
2019-02-18 18:44:27 +00:00
|
|
|
"os"
|
|
|
|
|
2023-02-18 23:13:23 +00:00
|
|
|
"git.blob42.xyz/gomark/gosuki/modules"
|
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-09-05 19:11:21 +00:00
|
|
|
// Runs the module by calling the setup
|
|
|
|
func RunModule(c *cli.Context,
|
|
|
|
browserMod modules.BrowserModule,
|
2023-09-06 12:25:56 +00:00
|
|
|
p *profiles.Profile) (error) {
|
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)
|
2023-09-06 12:25:56 +00:00
|
|
|
if !isShutdowner {
|
2023-09-05 20:28:26 +00:00
|
|
|
log.Warningf("browser <%s> does not implement modules.Shutdowner", browser.Config().Name)
|
2022-11-01 13:27:19 +00:00
|
|
|
}
|
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-09-05 19:11:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
//TODO!: call with custom profile
|
|
|
|
if p != nil {
|
|
|
|
bpm, ok := browser.(profiles.ProfileManager)
|
|
|
|
if !ok {
|
|
|
|
err := fmt.Errorf("<%s> does not implement profiles.ProfileManager",
|
|
|
|
browser.Config().Name)
|
|
|
|
log.Critical(err)
|
|
|
|
return err
|
2023-09-05 17:45:21 +00:00
|
|
|
}
|
2023-09-05 19:11:21 +00:00
|
|
|
if err := bpm.UseProfile(*p); err != nil {
|
|
|
|
log.Criticalf("could not use profile <%s>", p.Name)
|
|
|
|
return err
|
2023-09-05 17:45:21 +00:00
|
|
|
}
|
|
|
|
}
|
2023-02-18 22:08:12 +00:00
|
|
|
|
|
|
|
|
2023-09-05 20:28:26 +00:00
|
|
|
// calls the setup logic for each browser instance which
|
|
|
|
// 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
|
|
|
}
|
2023-09-05 19:11:21 +00:00
|
|
|
return err
|
2019-03-01 17:30:50 +00:00
|
|
|
}
|
|
|
|
|
2022-11-01 13:27:19 +00:00
|
|
|
runner, ok := browser.(watch.WatchRunner)
|
|
|
|
if !ok {
|
2023-09-05 19:11:21 +00:00
|
|
|
err = fmt.Errorf("<%s> must implement watch.WatchRunner interface", browser.Config().Name)
|
|
|
|
log.Critical(err)
|
|
|
|
return err
|
2022-11-01 13:27:19 +00:00
|
|
|
}
|
|
|
|
|
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)
|
2023-09-05 19:11:21 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func startDaemon(c *cli.Context) error {
|
|
|
|
defer utils.CleanFiles()
|
|
|
|
manager := gum.NewManager()
|
|
|
|
manager.ShutdownOn(os.Interrupt)
|
|
|
|
|
|
|
|
api := NewApi()
|
|
|
|
manager.AddUnit(api)
|
|
|
|
|
|
|
|
go manager.Run()
|
|
|
|
|
|
|
|
// Initialize sqlite database available in global `cacheDB` variable
|
|
|
|
initDB()
|
|
|
|
|
|
|
|
registeredBrowsers := modules.GetBrowserModules()
|
|
|
|
|
|
|
|
// instanciate all browsers
|
|
|
|
for _, browserMod := range registeredBrowsers {
|
|
|
|
|
|
|
|
mod := browserMod.ModInfo()
|
|
|
|
|
|
|
|
//Create a temporary browser instance to check if it implements
|
|
|
|
// the ProfileManager interface
|
|
|
|
browser, ok := mod.New().(modules.BrowserModule)
|
|
|
|
if !ok {
|
|
|
|
log.Criticalf("module <%s> is not a BrowserModule", mod.ID)
|
|
|
|
}
|
|
|
|
|
2023-09-05 20:28:26 +00:00
|
|
|
// if the module is a profile manager and is watching all profiles
|
|
|
|
// call RunModule for each profile
|
2023-09-05 19:11:21 +00:00
|
|
|
bpm, ok := browser.(profiles.ProfileManager)
|
|
|
|
if ok {
|
|
|
|
if bpm.WatchAllProfiles() {
|
|
|
|
profs, err := bpm.GetProfiles()
|
|
|
|
if err != nil {
|
|
|
|
log.Critical("could not get profiles")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
for _, p := range profs {
|
|
|
|
log.Debugf("profile: <%s>", p.Name)
|
|
|
|
err = RunModule(c, browserMod, p)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
err := RunModule(c, browserMod, nil)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
log.Warningf("module <%s> does not implement profiles.ProfileManager",
|
|
|
|
browser.Config().Name)
|
2023-09-05 20:28:26 +00:00
|
|
|
if err := RunModule(c, browserMod, nil); err != nil {
|
|
|
|
continue
|
|
|
|
}
|
2023-09-05 19:11:21 +00:00
|
|
|
}
|
2023-09-06 12:25:56 +00:00
|
|
|
|
|
|
|
// register defer shutdown logic
|
|
|
|
s, isShutdowner := browser.(modules.Shutdowner)
|
|
|
|
if isShutdowner {
|
|
|
|
defer handleShutdown(browser.Config().Name, s)
|
|
|
|
}
|
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 {
|
2023-09-06 12:25:56 +00:00
|
|
|
log.Panicf("<%s> could not shutdown: %s", id, err)
|
2022-11-01 13:27:19 +00:00
|
|
|
}
|
|
|
|
}
|