firefox profiles loading, todo CLI
This commit is contained in:
parent
9ed35b4149
commit
0c069a3c39
@ -105,6 +105,7 @@ func (bw *BaseBrowser) GetWatcher() *Watcher {
|
||||
}
|
||||
func (bw *BaseBrowser) Load() {
|
||||
log.Debug("BaseBrowser Load()")
|
||||
|
||||
bw.InitIndex()
|
||||
|
||||
// Check if cache is initialized
|
||||
|
13
commands.go
13
commands.go
@ -63,9 +63,16 @@ func startServer(c *cli.Context) {
|
||||
// Initialize sqlite database available in global `cacheDB` variable
|
||||
initDB()
|
||||
|
||||
browsers := []IBrowser{
|
||||
NewFFBrowser(),
|
||||
NewChromeBrowser(),
|
||||
var browsers []IBrowser
|
||||
|
||||
ff := NewFFBrowser()
|
||||
if ff != nil {
|
||||
browsers = append(browsers, ff)
|
||||
}
|
||||
|
||||
cr := NewChromeBrowser()
|
||||
if cr != nil {
|
||||
browsers = append(browsers, cr)
|
||||
}
|
||||
|
||||
for _, b := range browsers {
|
||||
|
21
firefox.go
21
firefox.go
@ -4,6 +4,7 @@ package main
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"errors"
|
||||
"gomark/database"
|
||||
"gomark/mozilla"
|
||||
"gomark/parsing"
|
||||
@ -51,11 +52,16 @@ const (
|
||||
ORDER BY url`
|
||||
)
|
||||
|
||||
var Firefox = BrowserPaths{
|
||||
var (
|
||||
Firefox = BrowserPaths{
|
||||
|
||||
BookmarkFile: mozilla.BookmarkFile,
|
||||
BookmarkDir: mozilla.BookmarkDir,
|
||||
}
|
||||
|
||||
ErrInitFirefox = errors.New("Could not start Firefox watcher")
|
||||
)
|
||||
|
||||
const (
|
||||
MozMinJobInterval = 500 * time.Millisecond
|
||||
)
|
||||
@ -136,6 +142,19 @@ func NewFFBrowser() IBrowser {
|
||||
// Initialize `places.sqlite`
|
||||
bookmarkPath := path.Join(browser.baseDir, browser.bkFile)
|
||||
|
||||
// Check if BookmarkPath exists
|
||||
exists, err := utils.CheckFileExists(bookmarkPath)
|
||||
if err != nil {
|
||||
log.Critical(err)
|
||||
log.Critical(ErrInitFirefox)
|
||||
return nil
|
||||
}
|
||||
if !exists {
|
||||
log.Criticalf("Bookmark path <%s> does not exist", bookmarkPath)
|
||||
log.Critical(ErrInitFirefox)
|
||||
return nil
|
||||
}
|
||||
|
||||
opts := database.DsnOptions{
|
||||
"_journal_mode": "WAL",
|
||||
}
|
||||
|
@ -4,11 +4,8 @@ import (
|
||||
"gomark/logging"
|
||||
)
|
||||
|
||||
var fflog = logging.GetLogger("FF")
|
||||
|
||||
const (
|
||||
BookmarkFile = "places.sqlite"
|
||||
BookmarkDir = "/home/spike/.mozilla/firefox/7otsk3vs.test_bookmarks"
|
||||
)
|
||||
|
||||
var (
|
||||
@ -16,5 +13,14 @@ var (
|
||||
PlacesDSN = map[string]string{
|
||||
"_jouranl_mode": "WAL",
|
||||
}
|
||||
log = logging.GetLogger("MOZ")
|
||||
|
||||
log = logging.GetLogger("FF")
|
||||
|
||||
// Bookmark directory
|
||||
BookmarkDir string
|
||||
)
|
||||
|
||||
func SetBookmarkDir(dir string) {
|
||||
log.Debugf("setting bookmarks dir to <%s>", dir)
|
||||
BookmarkDir = dir
|
||||
}
|
||||
|
@ -1,6 +1,7 @@
|
||||
package mozilla
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"gomark/profiles"
|
||||
"gomark/utils"
|
||||
"path/filepath"
|
||||
@ -9,6 +10,10 @@ import (
|
||||
ini "gopkg.in/ini.v1"
|
||||
)
|
||||
|
||||
type ProfileManager = profiles.ProfileManager
|
||||
type ProfileGetter = profiles.ProfileGetter
|
||||
type PathGetter = profiles.PathGetter
|
||||
|
||||
const (
|
||||
ProfilesFile = "profiles.ini"
|
||||
)
|
||||
@ -16,14 +21,40 @@ const (
|
||||
var (
|
||||
ConfigFolder = ".mozilla/firefox"
|
||||
ReIniProfiles = regexp.MustCompile(`(?i)profile`)
|
||||
|
||||
firefoxProfile = &ProfileGetter{
|
||||
//BasePath to be set at runtime in init
|
||||
ProfilesFile: ProfilesFile,
|
||||
}
|
||||
|
||||
FirefoxProfileManager = &FFProfileManager{
|
||||
pathGetter: firefoxProfile,
|
||||
}
|
||||
|
||||
ErrProfilesIni = errors.New("Could not parse Firefox profiles.ini file")
|
||||
ErrNoDefaultProfile = errors.New("No default profile found")
|
||||
)
|
||||
|
||||
type FFProfileManager struct {
|
||||
basePath string
|
||||
profilesFile *ini.File
|
||||
pathGetter PathGetter
|
||||
ProfileManager
|
||||
}
|
||||
|
||||
func (pm *FFProfileManager) loadProfile() error {
|
||||
|
||||
log.Debugf("loading profile from <%s>", pm.pathGetter.Get())
|
||||
pFile, err := ini.Load(pm.pathGetter.Get())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
pm.profilesFile = pFile
|
||||
return nil
|
||||
}
|
||||
|
||||
func (pm *FFProfileManager) GetProfiles() ([]*profiles.Profile, error) {
|
||||
pm.loadProfile()
|
||||
sections := pm.profilesFile.Sections()
|
||||
var filtered []*ini.Section
|
||||
var result []*profiles.Profile
|
||||
@ -48,7 +79,32 @@ func (pm *FFProfileManager) GetProfiles() ([]*profiles.Profile, error) {
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func (pm *FFProfileManager) ListProfiles() []string {
|
||||
func (pm *FFProfileManager) GetDefaultProfilePath() (string, error) {
|
||||
log.Debugf("using config dir %s", ConfigFolder)
|
||||
p, err := pm.GetDefaultProfile()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return filepath.Join(ConfigFolder, p.Path), nil
|
||||
}
|
||||
|
||||
func (pm *FFProfileManager) GetDefaultProfile() (*profiles.Profile, error) {
|
||||
profs, err := pm.GetProfiles()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for _, p := range profs {
|
||||
if p.Name == "default" {
|
||||
return p, nil
|
||||
}
|
||||
}
|
||||
|
||||
return nil, ErrNoDefaultProfile
|
||||
}
|
||||
|
||||
func (pm *FFProfileManager) ListProfiles() ([]string, error) {
|
||||
pm.loadProfile()
|
||||
sections := pm.profilesFile.SectionStrings()
|
||||
var result []string
|
||||
for _, s := range sections {
|
||||
@ -57,22 +113,11 @@ func (pm *FFProfileManager) ListProfiles() []string {
|
||||
}
|
||||
}
|
||||
|
||||
return result
|
||||
if len(result) == 0 {
|
||||
return nil, ErrProfilesIni
|
||||
}
|
||||
|
||||
func NewFFProfileManager() (*FFProfileManager, error) {
|
||||
profiles, err := ini.Load(filepath.Join(ConfigFolder, ProfilesFile))
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
pm := &FFProfileManager{
|
||||
basePath: ConfigFolder,
|
||||
profilesFile: profiles,
|
||||
}
|
||||
|
||||
return pm, nil
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func init() {
|
||||
@ -81,12 +126,21 @@ func init() {
|
||||
// Check if base folder exists
|
||||
configFolderExists, err := utils.CheckDirExists(ConfigFolder)
|
||||
if !configFolderExists {
|
||||
fflog.Criticalf("The base firefox folder <%s> does not exist",
|
||||
log.Criticalf("The base firefox folder <%s> does not exist",
|
||||
ConfigFolder)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
fflog.Critical(err)
|
||||
log.Critical(err)
|
||||
}
|
||||
|
||||
firefoxProfile.BasePath = ConfigFolder
|
||||
|
||||
bookmarkDir, err := FirefoxProfileManager.GetDefaultProfilePath()
|
||||
if err != nil {
|
||||
log.Error(err)
|
||||
}
|
||||
|
||||
SetBookmarkDir(bookmarkDir)
|
||||
|
||||
}
|
||||
|
@ -1,37 +1,64 @@
|
||||
package mozilla
|
||||
|
||||
import (
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestNewProfileManager(t *testing.T) {
|
||||
InitialConfigFolder := ConfigFolder
|
||||
ConfigFolder = "toto"
|
||||
_, err := NewFFProfileManager()
|
||||
|
||||
if !os.IsNotExist(err) {
|
||||
t.Error(err)
|
||||
var OkProfile = &ProfileGetter{
|
||||
BasePath: "testdata",
|
||||
ProfilesFile: "profiles_ok.ini",
|
||||
}
|
||||
|
||||
ConfigFolder = InitialConfigFolder
|
||||
var BadProfile = &ProfileGetter{
|
||||
BasePath: "testdata",
|
||||
ProfilesFile: "profiles_bad.ini",
|
||||
}
|
||||
|
||||
func TestListProfiles(t *testing.T) {
|
||||
pm, _ := NewFFProfileManager()
|
||||
//_, filename, _, _ := runtime.Caller(0)
|
||||
//dir, err := filepath.Abs(filepath.Dir(filename))
|
||||
//if err != nil {
|
||||
//t.Error(err)
|
||||
//}
|
||||
//t.Error(dir)
|
||||
t.Run("OK", func(t *testing.T) {
|
||||
pm := &FFProfileManager{
|
||||
pathGetter: OkProfile,
|
||||
}
|
||||
|
||||
t.Log("Listing profiles")
|
||||
profiles := pm.ListProfiles()
|
||||
for _, p := range pm.ListProfiles() {
|
||||
profiles, err := pm.ListProfiles()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
for _, p := range profiles {
|
||||
t.Logf("found profiles: %s", p)
|
||||
}
|
||||
if profiles[0] != "Profile0" {
|
||||
t.Error("Expected at least Profile0")
|
||||
t.Error("Expected Profile0")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("Bad", func(t *testing.T) {
|
||||
pm := &FFProfileManager{
|
||||
pathGetter: BadProfile,
|
||||
}
|
||||
|
||||
_, err := pm.ListProfiles()
|
||||
|
||||
if err != ErrProfilesIni || err == nil {
|
||||
t.Error("Expected error parsing bad profiles file")
|
||||
}
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
func TestGetProfiles(t *testing.T) {
|
||||
pm, _ := NewFFProfileManager()
|
||||
pm := &FFProfileManager{
|
||||
pathGetter: OkProfile,
|
||||
}
|
||||
|
||||
profs, err := pm.GetProfiles()
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
@ -40,4 +67,8 @@ func TestGetProfiles(t *testing.T) {
|
||||
for _, p := range profs {
|
||||
t.Log(p)
|
||||
}
|
||||
|
||||
if profs[0].Name != "default" {
|
||||
t.Error("Expected default profile in profiles.ini")
|
||||
}
|
||||
}
|
||||
|
@ -52,13 +52,13 @@ func UnlockPlaces(dir string) error {
|
||||
// TODO: #multiprocess add CLI to unlock places.sqlite
|
||||
pusers, err := utils.FileProcessUsers(path.Join(BookmarkDir, BookmarkFile))
|
||||
if err != nil {
|
||||
fflog.Error(err)
|
||||
log.Error(err)
|
||||
}
|
||||
|
||||
for pid, p := range pusers {
|
||||
pname, err := p.Name()
|
||||
if err != nil {
|
||||
fflog.Error(err)
|
||||
log.Error(err)
|
||||
}
|
||||
return errors.New(fmt.Sprintf("multiprocess not enabled and %s(%d) is running. Close firefox and disable VFS lock", pname, pid))
|
||||
}
|
||||
|
2
mozilla/testdata/profiles_bad.ini
vendored
Normal file
2
mozilla/testdata/profiles_bad.ini
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
[Test]
|
||||
Name=Does not contain a firefox profile
|
12
mozilla/testdata/profiles_ok.ini
vendored
Normal file
12
mozilla/testdata/profiles_ok.ini
vendored
Normal file
@ -0,0 +1,12 @@
|
||||
[General]
|
||||
StartWithLastProfile=0
|
||||
|
||||
[Profile0]
|
||||
Name=default
|
||||
Path=path.default
|
||||
|
||||
[Profile1]
|
||||
Name=profile1
|
||||
Path=path.profile1
|
||||
|
||||
|
@ -3,14 +3,16 @@
|
||||
|
||||
package profiles
|
||||
|
||||
import "path/filepath"
|
||||
|
||||
const (
|
||||
XDG_HOME = "XDG_CONFIG_HOME"
|
||||
)
|
||||
|
||||
type ProfileManager interface {
|
||||
ListProfiles() []string
|
||||
GetProfiles() []*Profile
|
||||
GetDefaultProfile() Profile
|
||||
ListProfiles() ([]string, error)
|
||||
GetProfiles() ([]*Profile, error)
|
||||
GetDefaultProfile() (*Profile, error)
|
||||
}
|
||||
|
||||
type Profile struct {
|
||||
@ -18,3 +20,20 @@ type Profile struct {
|
||||
Name string
|
||||
Path string
|
||||
}
|
||||
|
||||
func (p *Profile) GetPath() string {
|
||||
return p.Path
|
||||
}
|
||||
|
||||
type PathGetter interface {
|
||||
Get() string
|
||||
}
|
||||
|
||||
type ProfileGetter struct {
|
||||
BasePath string
|
||||
ProfilesFile string
|
||||
}
|
||||
|
||||
func (pg *ProfileGetter) Get() string {
|
||||
return filepath.Join(pg.BasePath, pg.ProfilesFile)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user