// Configuration package firefox import ( "fmt" "os" "path/filepath" "git.blob42.xyz/gomark/gosuki/config" "git.blob42.xyz/gomark/gosuki/database" "git.blob42.xyz/gomark/gosuki/modules" "git.blob42.xyz/gomark/gosuki/mozilla" "git.blob42.xyz/gomark/gosuki/parsing" "git.blob42.xyz/gomark/gosuki/tree" "git.blob42.xyz/gomark/gosuki/utils" "github.com/fatih/structs" "github.com/mitchellh/mapstructure" "github.com/urfave/cli/v2" ) const ( BrowserName = "firefox" FirefoxConfigDir = "$HOME/.mozilla/firefox" DefaultProfile = "default" ) var ( // firefox global config state. FFConfig = &FirefoxConfig{ BrowserConfig: &modules.BrowserConfig{ Name: BrowserName, Type: modules.TFirefox, BkDir: "", BkFile: mozilla.PlacesFile, WatchedPaths: []string{}, NodeTree: &tree.Node{ Name: mozilla.RootName, Parent: nil, Type: tree.RootNode, }, Stats: &parsing.Stats{}, UseFileWatcher: true, }, // Default data source name query options for `places.sqlite` db PlacesDSN: database.DsnOptions{ "_journal_mode": "WAL", }, // default profile to use, can be selected as cli argument Profile: DefaultProfile, } ffProfileLoader = &mozilla.INIProfileLoader{ //BasePath to be set at runtime in init ProfilesFile: mozilla.ProfilesFile, } FirefoxProfileManager = &mozilla.MozProfileManager{ BrowserName: BrowserName, PathGetter: ffProfileLoader, } ) func init() { config.RegisterConfigurator(BrowserName, FFConfig) //BUG: initFirefoxConfig is is called too early config.RegisterConfReadyHooks(initFirefoxConfig) } // FirefoxConfig implements the Configurator interface // which allows it to register and set field through the Configurator. // // It is also used alongside cli_flags.go to dynamically register cli flags // that can change this config (struct fields) from command line at runtime. // // The struct schema defines the parameters to pass on to firefox that can be // overriden by users. Options defined here will automatically supported in the // config.toml file as well as the command line flags. New command line flags or // config file options will only be accepted if they are defined here. type FirefoxConfig struct { // Default data source name query options for `places.sqlite` db PlacesDSN database.DsnOptions Profile string modules.BrowserProfilePrefs `toml:"profile_options"` //TEST: ignore this field in config.Configurator interface // Embed base browser config *modules.BrowserConfig `toml:"-"` } func (fc *FirefoxConfig) Set(opt string, v interface{}) error { // log.Debugf("setting option %s = %v", opt, v) s := structs.New(fc) f, ok := s.FieldOk(opt) if !ok { return fmt.Errorf("%s option not defined", opt) } return f.Set(v) } func (fc *FirefoxConfig) Get(opt string) (interface{}, error) { s := structs.New(fc) f, ok := s.FieldOk(opt) if !ok { return nil, fmt.Errorf("%s option not defined", opt) } return f.Value(), nil } func (fc *FirefoxConfig) Dump() map[string]interface{} { s := structs.New(fc) return s.Map() } func (fc *FirefoxConfig) String() string { s := structs.New(fc) return fmt.Sprintf("%v", s.Map()) } func (fc *FirefoxConfig) MapFrom(src interface{}) error { return mapstructure.Decode(src, fc) } //REFACT: // Hook called when the config is ready func initFirefoxConfig(c *cli.Context) error { log.Debugf(" initializing config") // The following code is executed before the cli context is ready // so we cannot use cli flags here pm := FirefoxProfileManager // expand path to config dir pm.ConfigDir = filepath.Join(os.ExpandEnv(FirefoxConfigDir)) // Check if base folder exists configFolderExists, err := utils.CheckDirExists(pm.ConfigDir) if !configFolderExists { log.Criticalf("the base firefox folder <%s> does not exist", pm.ConfigDir) } if err != nil { log.Fatal(err) return err } // The next part prepares the default profile using the profile manager ffProfileLoader.BasePath = pm.ConfigDir //FIX: bookmarkDir is used in created instance of FF before it is setup in config // use default profile bookmarkDir, err := FirefoxProfileManager.GetProfilePath(FFConfig.Profile) if err != nil { log.Fatal(err) } // update bookmark dir in firefox config //TEST: verify that bookmark dir is set before browser is started FFConfig.BkDir = bookmarkDir log.Debugf("Using profile %s", bookmarkDir) return nil }