chrome profile manager, refactored firefox config
- BUG: firefox watch all cmd flag not available in daemon.go due to using a new instance - Updated/fixed saving & loading to toml config file - wip module cmd flag
This commit is contained in:
parent
8a233ca9a6
commit
c825972cfd
@ -41,10 +41,11 @@ import (
|
||||
|
||||
"github.com/buger/jsonparser"
|
||||
|
||||
"git.blob42.xyz/gosuki/gosuki/internal/database"
|
||||
"git.blob42.xyz/gosuki/gosuki/hooks"
|
||||
"git.blob42.xyz/gosuki/gosuki/internal/database"
|
||||
"git.blob42.xyz/gosuki/gosuki/internal/logging"
|
||||
"git.blob42.xyz/gosuki/gosuki/pkg/modules"
|
||||
"git.blob42.xyz/gosuki/gosuki/pkg/profiles"
|
||||
"git.blob42.xyz/gosuki/gosuki/pkg/tree"
|
||||
"git.blob42.xyz/gosuki/gosuki/pkg/watch"
|
||||
)
|
||||
@ -135,11 +136,8 @@ func (ch *Chrome) Init(_ *modules.Context) error {
|
||||
}
|
||||
|
||||
func (ch *Chrome) setupWatchers() error {
|
||||
bookmarkDir, err := ch.BookmarkDir()
|
||||
bookmarkDir := ch.BkDir
|
||||
log.Debugf("Watching path: %s", bookmarkDir)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
bookmarkPath := filepath.Join(bookmarkDir, ch.BkFile)
|
||||
// Setup watcher
|
||||
w := &watch.Watch{
|
||||
@ -455,6 +453,8 @@ func (ch *Chrome) Run() {
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
// Load() will be called right after a browser is initialized
|
||||
func (ch *Chrome) Load() error {
|
||||
go ch.Run()
|
||||
@ -479,3 +479,4 @@ var _ modules.Loader = (*Chrome)(nil)
|
||||
var _ watch.WatchRunner = (*Chrome)(nil)
|
||||
var _ hooks.HookRunner = (*Chrome)(nil)
|
||||
var _ watch.Stats = (*Chrome)(nil)
|
||||
var _ profiles.ProfileManager = (*Chrome)(nil)
|
||||
|
54
browsers/chrome/chrome_test.go
Normal file
54
browsers/chrome/chrome_test.go
Normal file
@ -0,0 +1,54 @@
|
||||
// Copyright (c) 2023 Chakib Ben Ziane <contact@blob42.xyz> and [`gosuki` contributors](https://github.com/blob42/gosuki/graphs/contributors).
|
||||
// All rights reserved.
|
||||
//
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
//
|
||||
// This file is part of GoSuki.
|
||||
//
|
||||
// GoSuki is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Affero General Public License as
|
||||
// published by the Free Software Foundation, either version 3 of the
|
||||
// License, or (at your option) any later version.
|
||||
//
|
||||
// GoSuki is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Affero General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with gosuki. If not, see <http://www.gnu.org/licenses/>.
|
||||
package chrome
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"git.blob42.xyz/gosuki/gosuki/internal/utils"
|
||||
)
|
||||
|
||||
const statePath = "~/.config/google-chrome/Local State"
|
||||
|
||||
func TestLoadLocalState(t *testing.T) {
|
||||
var state *StateData
|
||||
fullPath, err := utils.ExpandPath(statePath)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
state, err = loadLocalState(fullPath)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
fmt.Printf("\nstate:\n%+v\n", state)
|
||||
}
|
||||
|
||||
func TestGetProfiles(t *testing.T) {
|
||||
ch := &Chrome{}
|
||||
profiles, err := ch.GetProfiles(ChromeStable)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, prof := range profiles {
|
||||
fmt.Printf("\nprofiles:\n%#v\n", prof)
|
||||
fmt.Println(prof.AbsolutePath())
|
||||
}
|
||||
}
|
@ -22,31 +22,61 @@
|
||||
package chrome
|
||||
|
||||
import (
|
||||
"git.blob42.xyz/gosuki/gosuki/internal/config"
|
||||
"git.blob42.xyz/gosuki/gosuki/pkg/modules"
|
||||
"git.blob42.xyz/gosuki/gosuki/pkg/parsing"
|
||||
"git.blob42.xyz/gosuki/gosuki/pkg/profiles"
|
||||
"git.blob42.xyz/gosuki/gosuki/pkg/tree"
|
||||
)
|
||||
|
||||
const (
|
||||
BrowserName = "chrome"
|
||||
BrowserName = ChromeStable
|
||||
ChromeBaseDir = "$HOME/.config/google-chrome"
|
||||
DefaultProfile = "Default"
|
||||
RootNodeName = "ROOT"
|
||||
)
|
||||
|
||||
|
||||
type ChromeConfig struct {
|
||||
Profile string
|
||||
*modules.BrowserConfig `toml:"-"`
|
||||
modules.ProfilePrefs `toml:"profile_options"`
|
||||
modules.ProfilePrefs `toml:"profile_options" mapstructure:"profile_options"`
|
||||
}
|
||||
|
||||
var (
|
||||
ChromeCfg = &ChromeConfig{
|
||||
Profile: DefaultProfile,
|
||||
ProfileManager = &ChromeProfileManager{}
|
||||
|
||||
ChromeCfg = NewChromeConfig()
|
||||
)
|
||||
|
||||
func setBookmarkDir(cc *ChromeConfig) {
|
||||
var err error
|
||||
|
||||
// load profile from config
|
||||
var profile *profiles.Profile
|
||||
|
||||
// In chrome we need to identify the profiles by their ID to get the correct
|
||||
// path
|
||||
if profile, err = ProfileManager.GetProfileByID(BrowserName, cc.Profile); err != nil {
|
||||
log.Warning(err)
|
||||
} else {
|
||||
bookmarkDir, err := profile.AbsolutePath()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
cc.BkDir = bookmarkDir
|
||||
log.Debugf("Using profile %s", bookmarkDir)
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func NewChromeConfig() *ChromeConfig {
|
||||
|
||||
config := &ChromeConfig{
|
||||
BrowserConfig: &modules.BrowserConfig{
|
||||
Name: BrowserName,
|
||||
Type: modules.TChrome,
|
||||
BkDir: "$HOME/.config/google-chrome/Default",
|
||||
BkFile: "Bookmarks",
|
||||
NodeTree: &tree.Node{
|
||||
Name: RootNodeName,
|
||||
@ -57,6 +87,16 @@ var (
|
||||
UseFileWatcher: true,
|
||||
UseHooks: []string{"tags_from_name", "notify-send"},
|
||||
},
|
||||
//TODO: profile
|
||||
ProfilePrefs: modules.ProfilePrefs{
|
||||
Profile: DefaultProfile,
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
setBookmarkDir(config)
|
||||
|
||||
return config
|
||||
}
|
||||
|
||||
func init() {
|
||||
config.RegisterConfigurator(BrowserName, config.AsConfigurator(ChromeCfg))
|
||||
}
|
||||
|
167
browsers/chrome/profiles.go
Normal file
167
browsers/chrome/profiles.go
Normal file
@ -0,0 +1,167 @@
|
||||
// Copyright (c) 2023 Chakib Ben Ziane <contact@blob42.xyz> and [`gosuki` contributors](https://github.com/blob42/gosuki/graphs/contributors).
|
||||
// All rights reserved.
|
||||
//
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
//
|
||||
// This file is part of GoSuki.
|
||||
//
|
||||
// GoSuki is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Affero General Public License as
|
||||
// published by the Free Software Foundation, either version 3 of the
|
||||
// License, or (at your option) any later version.
|
||||
//
|
||||
// GoSuki is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Affero General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with gosuki. If not, see <http://www.gnu.org/licenses/>.
|
||||
package chrome
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
|
||||
"git.blob42.xyz/gosuki/gosuki/internal/utils"
|
||||
"git.blob42.xyz/gosuki/gosuki/pkg/profiles"
|
||||
)
|
||||
|
||||
// Chrome state file.
|
||||
// The state file is a json file containing the last used profile and the list
|
||||
// of profiles. Equivalent to the profiles.ini file for mozilla browsers.
|
||||
const StateFile = "Local State"
|
||||
|
||||
// Chrome flavour names
|
||||
const (
|
||||
ChromeStable = "chrome"
|
||||
ChromeUnstable = "chrome-unstable"
|
||||
//TODO:
|
||||
// Chromium = "chromium"
|
||||
)
|
||||
|
||||
|
||||
var (
|
||||
ChromeBrowsers = map[string]profiles.BrowserFlavour{
|
||||
ChromeStable: {ChromeStable, "~/.config/google-chrome" },
|
||||
}
|
||||
)
|
||||
|
||||
// Helper struct to manage chrome profiles
|
||||
// profiles.ProfileManager is implemented at the browser level
|
||||
type ChromeProfileManager struct {}
|
||||
|
||||
|
||||
// Returns all profiles for a given flavour
|
||||
func (*ChromeProfileManager) GetProfiles(flavour string) ([]*profiles.Profile, error) {
|
||||
var result []*profiles.Profile
|
||||
|
||||
f, ok := ChromeBrowsers[flavour]
|
||||
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("unknown flavour <%s>", flavour)
|
||||
}
|
||||
statePath, err := utils.ExpandPath(f.BaseDir, StateFile)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
state, err := loadLocalState(statePath)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for id, profile := range state.Profile.InfoCache {
|
||||
result = append(result, &profiles.Profile{
|
||||
Id: id,
|
||||
Name: profile.Name,
|
||||
Path: id,
|
||||
BaseDir: f.BaseDir,
|
||||
})
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (*Chrome) GetProfiles(flavour string) ([]*profiles.Profile, error) {
|
||||
return ProfileManager.GetProfiles(flavour)
|
||||
}
|
||||
|
||||
// Returns all flavours supported by this browser
|
||||
func (*Chrome) ListFlavours() []profiles.BrowserFlavour {
|
||||
var result []profiles.BrowserFlavour
|
||||
|
||||
// detect local flavours
|
||||
for _, v := range ChromeBrowsers {
|
||||
if v.Detect() {
|
||||
result = append(result, v)
|
||||
}
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
|
||||
|
||||
// If should watch all profiles
|
||||
func (chrome *Chrome) WatchAllProfiles() bool {
|
||||
return chrome.ChromeConfig.WatchAllProfiles
|
||||
}
|
||||
|
||||
|
||||
func (cpm *ChromeProfileManager) GetProfileByID (flavour string, id string) (*profiles.Profile, error) {
|
||||
profiles, err := cpm.GetProfiles(flavour)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for _, p := range profiles {
|
||||
if p.Id == id {
|
||||
return p, nil
|
||||
}
|
||||
}
|
||||
|
||||
return nil, fmt.Errorf("profile %s not found", id)
|
||||
}
|
||||
|
||||
// Notifies the module to use a custom profile
|
||||
//NOTE: this is implemented at the browser Level
|
||||
func (c *Chrome) UseProfile(p profiles.Profile) error {
|
||||
c.Profile = p.Name
|
||||
|
||||
// setup the bookmark dir
|
||||
if bookmarkDir, err := p.AbsolutePath(); err != nil {
|
||||
return err
|
||||
} else {
|
||||
c.BkDir = bookmarkDir
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
type StateData struct {
|
||||
LastUsed string
|
||||
Profile struct {
|
||||
InfoCache map[string]profiles.Profile `json:"info_cache"`
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
func loadLocalState(path string) (*StateData, error) {
|
||||
var state StateData
|
||||
file, err := os.Open(path)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
defer file.Close()
|
||||
|
||||
data, err := io.ReadAll(file)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
err = json.Unmarshal(data, &state)
|
||||
return &state, err
|
||||
}
|
@ -23,7 +23,7 @@
|
||||
package firefox
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/urfave/cli/v2"
|
||||
|
||||
"git.blob42.xyz/gosuki/gosuki/internal/config"
|
||||
"git.blob42.xyz/gosuki/gosuki/internal/database"
|
||||
@ -32,9 +32,6 @@ import (
|
||||
"git.blob42.xyz/gosuki/gosuki/pkg/parsing"
|
||||
"git.blob42.xyz/gosuki/gosuki/pkg/profiles"
|
||||
"git.blob42.xyz/gosuki/gosuki/pkg/tree"
|
||||
|
||||
"github.com/fatih/structs"
|
||||
"github.com/mitchellh/mapstructure"
|
||||
)
|
||||
|
||||
const (
|
||||
@ -44,6 +41,7 @@ const (
|
||||
|
||||
// Default flavour to use
|
||||
BrowserName = mozilla.FirefoxFlavour
|
||||
|
||||
)
|
||||
|
||||
var (
|
||||
@ -73,25 +71,24 @@ var (
|
||||
// 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
|
||||
PlacesDSN database.DsnOptions `toml:"-"`
|
||||
|
||||
modules.ProfilePrefs `toml:"profile_options"`
|
||||
modules.ProfilePrefs `toml:"profile_options" mapstructure:"profile_options"`
|
||||
|
||||
//TEST: ignore this field in config.Configurator interface
|
||||
// Embed base browser config
|
||||
*modules.BrowserConfig `toml:"-"`
|
||||
}
|
||||
|
||||
//REFACT: move logic to modules package and use interface as input
|
||||
func setBookmarkDir(fc *FirefoxConfig) {
|
||||
var err error
|
||||
|
||||
// load the default profile from the one defined in the config
|
||||
// load profile from config
|
||||
var profile *profiles.Profile
|
||||
if profile, err = FirefoxProfileManager.GetProfileByName(BrowserName, fc.Profile); err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
log.Warning(err)
|
||||
} else {
|
||||
bookmarkDir, err := profile.AbsolutePath()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
@ -99,15 +96,16 @@ func setBookmarkDir(fc *FirefoxConfig) {
|
||||
|
||||
fc.BkDir = bookmarkDir
|
||||
log.Debugf("Using profile %s", bookmarkDir)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func NewFirefoxConfig() *FirefoxConfig {
|
||||
|
||||
config := &FirefoxConfig{
|
||||
cfg := &FirefoxConfig{
|
||||
BrowserConfig: &modules.BrowserConfig{
|
||||
Name: BrowserName,
|
||||
Type: modules.TFirefox,
|
||||
BkDir: "",
|
||||
BkFile: mozilla.PlacesFile,
|
||||
NodeTree: &tree.Node{
|
||||
Name: mozilla.RootName,
|
||||
@ -128,49 +126,47 @@ func NewFirefoxConfig() *FirefoxConfig {
|
||||
},
|
||||
|
||||
// default profile to use, can be selected as cli argument
|
||||
|
||||
ProfilePrefs: modules.ProfilePrefs{
|
||||
Profile: DefaultProfile,
|
||||
},
|
||||
}
|
||||
|
||||
setBookmarkDir(config)
|
||||
setBookmarkDir(cfg)
|
||||
|
||||
return config
|
||||
// Set WatchAllProfiles that was set by user in flags
|
||||
// if userConf := config.GetModule(BrowserName); userConf != nil {
|
||||
// watchAll, err := userConf.Get("WatchAllProfiles")
|
||||
// if err != nil {
|
||||
// log.Fatal(err)
|
||||
// } else {
|
||||
// cfg.WatchAllProfiles = watchAll.(bool)
|
||||
// }
|
||||
// }
|
||||
//
|
||||
|
||||
return cfg
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
func init() {
|
||||
config.RegisterConfigurator(BrowserName, FFConfig)
|
||||
config.RegisterConfigurator(BrowserName, config.AsConfigurator(FFConfig))
|
||||
config.RegisterConfReadyHooks(func(*cli.Context) error{
|
||||
// log.Debugf("%#v", config.GetAll().Dump())
|
||||
|
||||
|
||||
//FIX: WatchAllProfiles option not set when instanciating new
|
||||
//browser in daemon.go
|
||||
// Recover Watch All Profiles value
|
||||
if userConf := config.GetModule(BrowserName); userConf != nil {
|
||||
watchAll, err := userConf.Get("WatchAllProfiles")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
} else {
|
||||
FFConfig.WatchAllProfiles = watchAll.(bool)
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
@ -245,6 +245,7 @@ func (f *Firefox) scanModifiedBookmarks(since timestamp) ([]*MozBookmark, error)
|
||||
|
||||
|
||||
func NewFirefox() *Firefox {
|
||||
|
||||
return &Firefox{
|
||||
FirefoxConfig: FFConfig,
|
||||
places: &database.DB{},
|
||||
@ -270,7 +271,7 @@ func (f Firefox) fullId() string {
|
||||
}
|
||||
|
||||
// Implements the profiles.ProfileManager interface
|
||||
func (f *Firefox) GetProfiles(flavour string) ([]*profiles.Profile, error) {
|
||||
func (*Firefox) GetProfiles(flavour string) ([]*profiles.Profile, error) {
|
||||
return FirefoxProfileManager.GetProfiles(flavour)
|
||||
}
|
||||
|
||||
@ -293,18 +294,23 @@ func (f *Firefox) UseProfile(p profiles.Profile) error {
|
||||
}
|
||||
}
|
||||
|
||||
func (f *Firefox) cloneConfig() {
|
||||
f.FirefoxConfig = NewFirefoxConfig()
|
||||
}
|
||||
|
||||
//TODO!: Duplicate logic with initFirefoxConfig (config.go)
|
||||
func (f *Firefox) Init(ctx *modules.Context, p *profiles.Profile) error {
|
||||
if p == nil {
|
||||
// setup profile from config
|
||||
profile, err := FirefoxProfileManager.GetProfileByName(BrowserName, f.Profile)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
bookmarkDir, err := profile.AbsolutePath()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
f.BkDir = bookmarkDir
|
||||
return f.init(ctx)
|
||||
}
|
||||
|
||||
// for new profile use a new config
|
||||
f.cloneConfig()
|
||||
// use a new config for this profile
|
||||
f.FirefoxConfig = NewFirefoxConfig()
|
||||
f.Profile = p.Name
|
||||
|
||||
|
||||
@ -322,11 +328,8 @@ func (f *Firefox) Init(ctx *modules.Context, p *profiles.Profile) error {
|
||||
func (f *Firefox) init(ctx *modules.Context) error {
|
||||
log.Infof("initializing <%s>", f.fullId())
|
||||
|
||||
watchedPath, err := f.BookmarkDir()
|
||||
watchedPath := f.BkDir
|
||||
log.Debugf("Watching path: %s", watchedPath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
// Setup watcher
|
||||
w := &watch.Watch{
|
||||
|
@ -151,6 +151,7 @@ func startDaemon(c *cli.Context) error {
|
||||
log.Criticalf("TODO: module <%s> is not a BrowserModule", mod.ID)
|
||||
}
|
||||
|
||||
//BUG: WatchAllProfiles `module` flag option not working here
|
||||
// if the module is a profile manager and is watching all profiles
|
||||
// call runModule for each profile
|
||||
bpm, ok := browser.(profiles.ProfileManager)
|
||||
|
@ -38,7 +38,7 @@ import (
|
||||
_ "git.blob42.xyz/gosuki/gosuki/browsers/firefox"
|
||||
|
||||
// Load chrome browser module
|
||||
// _ "git.blob42.xyz/gosuki/gosuki/browsers/chrome"
|
||||
_ "git.blob42.xyz/gosuki/gosuki/browsers/chrome"
|
||||
)
|
||||
|
||||
var log = logging.GetLogger("")
|
||||
|
@ -76,7 +76,7 @@ var listProfilesCmd = &cli.Command{
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Printf("%-10s \t %s\n", p.Name, pPath)
|
||||
fmt.Printf("%-10s[id:%s]\t %s\n", p.Name, p.Id, pPath)
|
||||
}
|
||||
}
|
||||
fmt.Println()
|
||||
|
@ -24,11 +24,12 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"os"
|
||||
"fmt"
|
||||
|
||||
"git.blob42.xyz/gosuki/gosuki/internal/logging"
|
||||
|
||||
"github.com/BurntSushi/toml"
|
||||
"github.com/fatih/structs"
|
||||
"github.com/mitchellh/mapstructure"
|
||||
"github.com/urfave/cli/v2"
|
||||
)
|
||||
|
||||
@ -75,50 +76,52 @@ func (c Config) MapFrom(src interface{}) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
type AutoConfigurator struct{
|
||||
c interface{}
|
||||
}
|
||||
|
||||
func (ac AutoConfigurator) Set(opt string, v interface{}) error {
|
||||
// log.Debugf("setting option %s = %v", opt, v)
|
||||
s := structs.New(ac.c)
|
||||
f, ok := s.FieldOk(opt)
|
||||
if !ok {
|
||||
return fmt.Errorf("%s option not defined", opt)
|
||||
}
|
||||
|
||||
return f.Set(v)
|
||||
}
|
||||
|
||||
func (ac AutoConfigurator) Get(opt string) (interface{}, error) {
|
||||
s := structs.New(ac.c)
|
||||
f, ok := s.FieldOk(opt)
|
||||
if !ok {
|
||||
return nil, fmt.Errorf("%s option not defined", opt)
|
||||
}
|
||||
|
||||
return f.Value(), nil
|
||||
}
|
||||
|
||||
func (ac AutoConfigurator) Dump() map[string]interface{} {
|
||||
s := structs.New(ac.c)
|
||||
return s.Map()
|
||||
}
|
||||
|
||||
func (ac AutoConfigurator) MapFrom(src interface{}) error {
|
||||
log.Debugf("mapping from: %#v ", src)
|
||||
log.Debugf("mapping to: %#v ", ac.c)
|
||||
return mapstructure.Decode(src, ac.c)
|
||||
}
|
||||
|
||||
func AsConfigurator(c interface{}) Configurator {
|
||||
return AutoConfigurator{c}
|
||||
}
|
||||
|
||||
// Register a global option ie. under [global] in toml file
|
||||
func RegisterGlobalOption(key string, val interface{}) {
|
||||
log.Debugf("Registring global option %s = %v", key, val)
|
||||
configs[GlobalConfigName].Set(key, val)
|
||||
}
|
||||
|
||||
// Setup cli flag for global options
|
||||
func SetupGlobalFlags() []cli.Flag {
|
||||
log.Debugf("Setting up global flags")
|
||||
flags := []cli.Flag{}
|
||||
for k, v := range configs[GlobalConfigName].Dump() {
|
||||
log.Debugf("Registering global flag %s = %v", k, v)
|
||||
|
||||
// Register the option as a cli flag
|
||||
switch val := v.(type) {
|
||||
case string:
|
||||
flags = append(flags, &cli.StringFlag{
|
||||
Category: "_",
|
||||
Name: k,
|
||||
Value: val,
|
||||
})
|
||||
|
||||
case int:
|
||||
flags = append(flags, &cli.IntFlag{
|
||||
Category: "_",
|
||||
Name: k,
|
||||
Value: val,
|
||||
})
|
||||
|
||||
case bool:
|
||||
flags = append(flags, &cli.BoolFlag{
|
||||
Category: "_",
|
||||
Name: k,
|
||||
Value: val,
|
||||
})
|
||||
|
||||
default:
|
||||
log.Fatalf("unsupported type for global option %s", k)
|
||||
}
|
||||
}
|
||||
|
||||
return flags
|
||||
}
|
||||
|
||||
func RegisterModuleOpt(module string, opt string, val interface{}) error {
|
||||
log.Debugf("Setting option for module <%s>: %s = %v", module, opt, val)
|
||||
dest := configs[module]
|
||||
@ -126,48 +129,27 @@ func RegisterModuleOpt(module string, opt string, val interface{}) error {
|
||||
}
|
||||
|
||||
// Get all configs as a map[string]interface{}
|
||||
// FIX: only print exported fields, parse tags for hidden fields
|
||||
func GetAll() Config {
|
||||
result := make(Config)
|
||||
for k, c := range configs {
|
||||
// if its an AutoConfigurator, use its c field
|
||||
if ac, ok := c.(AutoConfigurator); ok {
|
||||
result[k] = ac.c
|
||||
} else {
|
||||
result[k] = c
|
||||
}
|
||||
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
|
||||
// Create a toml config file
|
||||
func InitConfigFile() error {
|
||||
configFile, err := os.Create(ConfigFile)
|
||||
if err != nil {
|
||||
return err
|
||||
func GetModule(module string) Configurator {
|
||||
if c, ok := configs[module]; ok {
|
||||
return c
|
||||
}
|
||||
|
||||
allConf := GetAll()
|
||||
|
||||
tomlEncoder := toml.NewEncoder(configFile)
|
||||
err = tomlEncoder.Encode(&allConf)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func LoadFromTomlFile() error {
|
||||
dest := make(Config)
|
||||
_, err := toml.DecodeFile(ConfigFile, &dest)
|
||||
|
||||
for k, val := range dest {
|
||||
// send the conf to its own module
|
||||
if _, ok := configs[k]; ok {
|
||||
configs[k].MapFrom(val)
|
||||
}
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
|
||||
// Hooks registered here will be executed after the config package has finished
|
||||
// loading the conf
|
||||
|
62
internal/config/config_file.go
Normal file
62
internal/config/config_file.go
Normal file
@ -0,0 +1,62 @@
|
||||
// Copyright (c) 2023 Chakib Ben Ziane <contact@blob42.xyz> and [`gosuki` contributors](https://github.com/blob42/gosuki/graphs/contributors).
|
||||
// All rights reserved.
|
||||
//
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
//
|
||||
// This file is part of GoSuki.
|
||||
//
|
||||
// GoSuki is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Affero General Public License as
|
||||
// published by the Free Software Foundation, either version 3 of the
|
||||
// License, or (at your option) any later version.
|
||||
//
|
||||
// GoSuki is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Affero General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with gosuki. If not, see <http://www.gnu.org/licenses/>.
|
||||
package config
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
"github.com/BurntSushi/toml"
|
||||
)
|
||||
|
||||
// Create a toml config file
|
||||
func InitConfigFile() error {
|
||||
configFile, err := os.Create(ConfigFile)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
allConf := GetAll()
|
||||
|
||||
tomlEncoder := toml.NewEncoder(configFile)
|
||||
err = tomlEncoder.Encode(&allConf)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func LoadFromTomlFile() error {
|
||||
dest := make(Config)
|
||||
_, err := toml.DecodeFile(ConfigFile, &dest)
|
||||
|
||||
for k, val := range dest {
|
||||
|
||||
// send the conf to its own module
|
||||
if _, ok := configs[k]; ok {
|
||||
configs[k].MapFrom(val)
|
||||
}
|
||||
}
|
||||
|
||||
log.Debugf("loaded firefox config %#v", configs["firefox"])
|
||||
|
||||
return err
|
||||
}
|
||||
|
61
internal/config/flags.go
Normal file
61
internal/config/flags.go
Normal file
@ -0,0 +1,61 @@
|
||||
// Copyright (c) 2023 Chakib Ben Ziane <contact@blob42.xyz> and [`gosuki` contributors](https://github.com/blob42/gosuki/graphs/contributors).
|
||||
// All rights reserved.
|
||||
//
|
||||
// SPDX-License-Identifier: AGPL-3.0-or-later
|
||||
//
|
||||
// This file is part of GoSuki.
|
||||
//
|
||||
// GoSuki is free software: you can redistribute it and/or modify
|
||||
// it under the terms of the GNU Affero General Public License as
|
||||
// published by the Free Software Foundation, either version 3 of the
|
||||
// License, or (at your option) any later version.
|
||||
//
|
||||
// GoSuki is distributed in the hope that it will be useful,
|
||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU Affero General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU Affero General Public License
|
||||
// along with gosuki. If not, see <http://www.gnu.org/licenses/>.
|
||||
package config
|
||||
|
||||
import "github.com/urfave/cli/v2"
|
||||
|
||||
// Setup cli flag for global options
|
||||
func SetupGlobalFlags() []cli.Flag {
|
||||
log.Debugf("Setting up global flags")
|
||||
flags := []cli.Flag{}
|
||||
for k, v := range configs[GlobalConfigName].Dump() {
|
||||
log.Debugf("Registering global flag %s = %v", k, v)
|
||||
|
||||
// Register the option as a cli flag
|
||||
switch val := v.(type) {
|
||||
case string:
|
||||
flags = append(flags, &cli.StringFlag{
|
||||
Category: "_",
|
||||
Name: k,
|
||||
Value: val,
|
||||
})
|
||||
|
||||
case int:
|
||||
flags = append(flags, &cli.IntFlag{
|
||||
Category: "_",
|
||||
Name: k,
|
||||
Value: val,
|
||||
})
|
||||
|
||||
case bool:
|
||||
flags = append(flags, &cli.BoolFlag{
|
||||
Category: "_",
|
||||
Name: k,
|
||||
Value: val,
|
||||
})
|
||||
|
||||
default:
|
||||
log.Fatalf("unsupported type for global option %s", k)
|
||||
}
|
||||
}
|
||||
|
||||
return flags
|
||||
}
|
||||
|
@ -27,6 +27,7 @@ var BadProfile = &profiles.INIProfileLoader{
|
||||
}
|
||||
|
||||
func TestGetProfiles(t *testing.T) {
|
||||
// fake browser flavour
|
||||
MozBrowsers["test"] = profiles.BrowserFlavour{"test", "testdata"}
|
||||
t.Run("OK", func(t *testing.T) {
|
||||
pm := &MozProfileManager{
|
||||
|
@ -67,7 +67,8 @@ type Browser interface {
|
||||
type ProfilePrefs struct {
|
||||
|
||||
// Whether to watch all the profiles for multi-profile modules
|
||||
WatchAllProfiles bool `toml:"watch_all_profiles"`
|
||||
WatchAllProfiles bool `toml:"watch_all_profiles" mapstructure:"watch_all_profiles"`
|
||||
Profile string `toml:"profile" mapstructure:"profile"`
|
||||
}
|
||||
|
||||
// BrowserConfig is the main browser configuration shared by all browser modules.
|
||||
@ -81,9 +82,6 @@ type BrowserConfig struct {
|
||||
// Name of bookmarks file
|
||||
BkFile string
|
||||
|
||||
|
||||
ProfilePrefs
|
||||
|
||||
// In memory sqlite db (named `memcache`).
|
||||
// Used to keep a browser's state of bookmarks across jobs.
|
||||
BufferDB *database.DB
|
||||
@ -108,29 +106,11 @@ type BrowserConfig struct {
|
||||
hooks map[string]hooks.Hook
|
||||
}
|
||||
|
||||
|
||||
func (b *BrowserConfig) GetWatcher() *watch.WatchDescriptor {
|
||||
return b.watcher
|
||||
}
|
||||
|
||||
func (b BrowserConfig) BookmarkDir() (string, error) {
|
||||
var err error
|
||||
bDir, err := utils.ExpandPath(b.BkDir)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
exists, err := utils.CheckDirExists(bDir)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
if !exists {
|
||||
return "", fmt.Errorf("not a bookmark dir: %s ", bDir)
|
||||
}
|
||||
|
||||
return bDir, nil
|
||||
}
|
||||
|
||||
// CallHooks calls all registered hooks for this browser for the given
|
||||
// *tree.Node. The hooks are called in the order they were registered. This is
|
||||
// usually done within the parsing logic of a browser module, typically in the
|
||||
|
@ -23,6 +23,12 @@ package profiles
|
||||
|
||||
import "path/filepath"
|
||||
|
||||
// PathResolver allows custom path resolution for profiles
|
||||
type PathResolver interface {
|
||||
GetPath() string
|
||||
SetBaseDir(string)
|
||||
}
|
||||
|
||||
type INIProfileLoader struct {
|
||||
// The absolute path to the directory where profiles.ini is located
|
||||
BasePath string
|
||||
|
@ -23,6 +23,7 @@
|
||||
package profiles
|
||||
|
||||
import (
|
||||
|
||||
"git.blob42.xyz/gosuki/gosuki/internal/logging"
|
||||
"git.blob42.xyz/gosuki/gosuki/internal/utils"
|
||||
)
|
||||
@ -36,15 +37,6 @@ type ProfileManager interface {
|
||||
// Returns all profiles for a given flavour
|
||||
GetProfiles(flavour string) ([]*Profile, error)
|
||||
|
||||
//TODO!: remove
|
||||
// Returns the default profile if no profile is selected
|
||||
// GetDefaultProfile() (*Profile, error)
|
||||
|
||||
//TODO!: remove
|
||||
//TODO!: fix input to method, should take string ?
|
||||
// Return that absolute path to a profile and follow symlinks
|
||||
// GetProfilePath(Profile) (string)
|
||||
|
||||
// If should watch all profiles
|
||||
WatchAllProfiles() bool
|
||||
|
||||
@ -61,6 +53,7 @@ type Profile struct {
|
||||
Id string
|
||||
|
||||
// Name of the profile
|
||||
// This is usually the name of the directory where the profile is stored
|
||||
Name string
|
||||
|
||||
// relative path to profile from base dir
|
||||
@ -74,12 +67,6 @@ func (f Profile) AbsolutePath() (string, error) {
|
||||
return utils.ExpandPath(f.BaseDir, f.Path)
|
||||
}
|
||||
|
||||
// PathResolver allows custom path resolution for profiles
|
||||
// See the INIProfileLoader for an example
|
||||
type PathResolver interface {
|
||||
GetPath() string
|
||||
SetBaseDir(string)
|
||||
}
|
||||
|
||||
// The BrowserFlavour struct stores the name of the browser and the base
|
||||
// directory where the profiles are stored.
|
||||
|
Loading…
Reference in New Issue
Block a user