Skip to content

Commit

Permalink
fix: #46
Browse files Browse the repository at this point in the history
  • Loading branch information
blacktop committed Sep 15, 2023
1 parent d0226ae commit 519ac6c
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 91 deletions.
48 changes: 26 additions & 22 deletions internal/command/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,14 +106,14 @@ func parsePages(root int, parentMapping map[int][]database.Item) (database.Apps,
for _, item := range parentMapping[page.ID] {
switch item.Type {
case database.ApplicationType:
utils.Indent(log.WithField("title", item.App.Title).Info)("found app")
utils.Indent(log.WithField("title", item.App.Title).Info, 2)("found app")
p.Items = append(p.Items, item.App.Title)
// case database.WidgetType:
// utils.Indent(log.WithField("title", item.Widget.Title).Info)("found widget")
// p.Items = append(p.Items, item.Widget.Title)
case database.FolderRootType:

utils.Indent(log.WithField("title", item.Group.Title).Info)("found folder")
utils.Indent(log.WithField("title", item.Group.Title).Info, 2)("found folder")

f := database.AppFolder{Name: item.Group.Title}

Expand All @@ -122,12 +122,12 @@ func parsePages(root int, parentMapping map[int][]database.Item) (database.Apps,
}

for fpIndex, fpage := range parentMapping[item.ID] {
utils.DoubleIndent(log.WithField("number", fpIndex+1).Info)("found folder page")
utils.Indent(log.WithField("number", fpIndex+1).Info, 3)("found folder page")

fp := database.FolderPage{Number: fpIndex + 1}

for _, folder := range parentMapping[fpage.ID] {
utils.TripleIndent(log.WithField("title", folder.App.Title).Info)("found app")
utils.Indent(log.WithField("title", folder.App.Title).Info, 4)("found app")
fp.Items = append(fp.Items, folder.App.Title)
}

Expand All @@ -137,13 +137,13 @@ func parsePages(root int, parentMapping map[int][]database.Item) (database.Apps,
if len(f.Pages) > 0 && len(f.Pages[0].Items) > 0 {
p.Items = append(p.Items, f)
} else {
utils.DoubleIndent(log.WithField("folder", item.Group.Title).Error)("empty folder")
utils.Indent(log.WithField("folder", item.Group.Title).Error, 3)("empty folder")
}

case database.PageType:
utils.Indent(log.WithField("parent_id", item.ParentID).Info)("found page")
utils.Indent(log.WithField("parent_id", item.ParentID).Info, 2)("found page")
default:
utils.Indent(log.WithField("type", item.Type).Error)("found ?")
utils.Indent(log.WithField("type", item.Type).Error, 2)("found ?")
}
}
apps.Pages = append(apps.Pages, p)
Expand All @@ -163,9 +163,9 @@ func DefaultOrg(c *Config) (err error) {
lpad.File = filepath.Join(lpad.Folder, "db")
// lpad.File = "./launchpad.db"
if _, err := os.Stat(lpad.File); os.IsNotExist(err) {
utils.Indent(log.WithError(err).WithField("path", lpad.File).Fatal)("launchpad DB not found")
utils.Indent(log.WithError(err).WithField("path", lpad.File).Fatal, 2)("launchpad DB not found")
}
utils.Indent(log.WithFields(log.Fields{"database": lpad.File}).Info)("found launchpad database")
utils.Indent(log.WithFields(log.Fields{"database": lpad.File}).Info, 2)("found launchpad database")

// start from a clean slate
err = removeOldDatabaseFiles(lpad.Folder)
Expand Down Expand Up @@ -210,7 +210,7 @@ func DefaultOrg(c *Config) (err error) {
// groupID := int(math.Max(float64(lpad.GetMaxAppID()), float64(lpad.GetMaxWidgetID())))
groupID := int(float64(lpad.GetMaxAppID())) // widgets are no longer supported

utils.Indent(log.Info)("creating folders out of app categories")
utils.Indent(log.Info, 2)("creating folders out of app categories")

// Create default config file
var apps []database.App
Expand All @@ -227,12 +227,12 @@ func DefaultOrg(c *Config) (err error) {
folderName := strings.Title(strings.Replace(strings.TrimPrefix(category.UTI, "public.app-category."), "-", " ", 1))
folder := database.AppFolder{Name: folderName}
folderPage := database.FolderPage{Number: 1}
utils.DoubleIndent(log.WithField("folder", folderName).Info)("adding folder")
utils.Indent(log.WithField("folder", folderName).Info, 3)("adding folder")
if err := lpad.DB.Where("category_id = ?", category.ID).Find(&apps).Error; err != nil {
log.WithError(err).Error("categories query failed")
}
for _, app := range apps {
utils.TripleIndent(log.WithField("app", app.Title).Info)("adding app to category folder")
utils.Indent(log.WithField("app", app.Title).Info, 4)("adding app to category folder")
folderPage.Items = utils.AppendIfMissing(folderPage.Items, app.Title)
}
folder.Pages = append(folder.Pages, folderPage)
Expand Down Expand Up @@ -261,7 +261,7 @@ func DefaultOrg(c *Config) (err error) {
return fmt.Errorf("failed to GetMissing=>Apps: %v", err)
}

utils.Indent(log.Info)("creating App folders and adding apps to them")
utils.Indent(log.Info, 2)("creating App folders and adding apps to them")
if err := lpad.ApplyConfig(config.Apps, groupID, 1); err != nil {
return fmt.Errorf("failed to DefaultOrg->ApplyConfig: %w", err)
}
Expand Down Expand Up @@ -298,9 +298,9 @@ func SaveConfig(c *Config) (err error) {
lpad.File = filepath.Join(lpad.Folder, "db")
// lpad.File = "./launchpad.db"
if _, err := os.Stat(lpad.File); os.IsNotExist(err) {
utils.Indent(log.WithError(err).WithField("path", lpad.File).Fatal)("launchpad DB not found")
utils.Indent(log.WithError(err).WithField("path", lpad.File).Fatal, 2)("launchpad DB not found")
}
utils.Indent(log.WithFields(log.Fields{"database": lpad.File}).Info)("found launchpad database")
utils.Indent(log.WithFields(log.Fields{"database": lpad.File}).Info, 2)("found launchpad database")

// open launchpad database
lpad.DB, err = gorm.Open(sqlite.Open(lpad.File), &gorm.Config{
Expand Down Expand Up @@ -448,9 +448,9 @@ func LoadConfig(c *Config) (err error) {
lpad.File = filepath.Join(lpad.Folder, "db")
// lpad.File = "./launchpad-test.db"
if _, err := os.Stat(lpad.File); os.IsNotExist(err) {
utils.Indent(log.WithError(err).WithField("path", lpad.File).Fatal)("launchpad DB not found")
utils.Indent(log.WithError(err).WithField("path", lpad.File).Fatal, 2)("launchpad DB not found")
}
utils.Indent(log.WithFields(log.Fields{"database": lpad.File}).Info)("found launchpad database")
utils.Indent(log.WithFields(log.Fields{"database": lpad.File}).Info, 2)("found launchpad database")

// start from a clean slate
err = removeOldDatabaseFiles(lpad.Folder)
Expand Down Expand Up @@ -515,7 +515,11 @@ func LoadConfig(c *Config) (err error) {
return fmt.Errorf("failed to GetMissing=>Apps: %v", err)
}

utils.Indent(log.Info)("creating App folders and adding apps to them")
if err := lpad.Config.Verify(); err != nil {
return fmt.Errorf("failed to verify conf post removal of missing apps: %v", err)
}

utils.Indent(log.Info, 2)("creating App folders and adding apps to them")
if err := lpad.ApplyConfig(lpad.Config.Apps, groupID, 1); err != nil {
return fmt.Errorf("failed to LoadConfig->ApplyConfig: %w", err)
}
Expand All @@ -534,12 +538,12 @@ func LoadConfig(c *Config) (err error) {
}

if len(lpad.Config.Desktop.Image) > 0 {
utils.Indent(log.WithField("image", lpad.Config.Desktop.Image).Info)("setting desktop background image")
utils.Indent(log.WithField("image", lpad.Config.Desktop.Image).Info, 2)("setting desktop background image")
desktop.SetDesktopImage(lpad.Config.Desktop.Image)
}

if len(lpad.Config.Dock.Apps) > 0 || len(lpad.Config.Dock.Others) > 0 {
utils.Indent(log.Info)("setting dock apps")
utils.Indent(log.Info, 2)("setting dock apps")
dPlist, err := dock.LoadDockPlist()
if err != nil {
return errors.Wrap(err, "unable to load dock plist")
Expand All @@ -548,14 +552,14 @@ func LoadConfig(c *Config) (err error) {
dPlist.PersistentApps = nil // remove all apps from dock
}
for _, app := range lpad.Config.Dock.Apps {
utils.DoubleIndent(log.WithField("app", app).Info)("adding to dock")
utils.Indent(log.WithField("app", app).Info, 3)("adding to dock")
dPlist.AddApp(app)
}
if len(dPlist.PersistentOthers) > 0 {
dPlist.PersistentOthers = nil // remove all folders from dock
}
for _, other := range lpad.Config.Dock.Others {
utils.DoubleIndent(log.WithField("other", other).Info)("adding to dock")
utils.Indent(log.WithField("other", other).Info, 3)("adding to dock")
dPlist.AddOther(other)
}
if lpad.Config.Dock.Settings != nil {
Expand Down
6 changes: 3 additions & 3 deletions internal/command/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ var PorgASCIIArt = `
`

func restartDock() error {
utils.Indent(log.Info)("restarting Dock")
utils.Indent(log.Info, 2)("restarting Dock")
if _, err := utils.RunCommand(context.Background(), "killall", "Dock"); err != nil {
return errors.Wrap(err, "killing Dock process failed")
}
Expand All @@ -89,13 +89,13 @@ func removeOldDatabaseFiles(dbpath string) error {

for _, path := range paths {
if _, err := os.Stat(path); os.IsNotExist(err) {
utils.DoubleIndent(log.WithField("path", path).Warn)("DB file not found")
utils.Indent(log.WithField("path", path).Warn, 3)("DB file not found")
continue
}
if err := os.Remove(path); err != nil {
return errors.Wrap(err, "removing file failed")
}
utils.DoubleIndent(log.WithField("path", path).Info)("removed old DB file")
utils.Indent(log.WithField("path", path).Info, 3)("removed old DB file")
}

return restartDock()
Expand Down
49 changes: 28 additions & 21 deletions internal/database/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,29 @@ func (c Config) GetFolderContainingApp(app string) (string, error) {
return "", fmt.Errorf("unable to find folder containing app %s", app)
}

// Verify that the config is valid
func (c Config) Verify() error {
for _, page := range c.Apps.Pages {
for _, item := range page.Items {
switch item.(type) {
case string:
continue
default:
var folder AppFolder
if err := mapstructure.Decode(item, &folder); err != nil {
return fmt.Errorf("mapstructure unable to decode config folder")
}
if len(folder.Pages) > 0 {
if len(folder.Pages[0].Items) < 2 { // verify that all folders contain more than 1 item
return fmt.Errorf("folder %s must contain more than 1 item to be valid", folder.Name)
}
}
}
}
}
return nil
}

// Apps is the launchpad apps config object
type Apps struct {
Pages []Page `yaml:"pages" json:"pages,omitempty"`
Expand Down Expand Up @@ -128,37 +151,21 @@ type Dock struct {
func LoadConfig(filename string) (Config, error) {
var conf Config

utils.Indent(log.WithField("path", filename).Info)("parsing launchpad config YAML")
utils.Indent(log.WithField("path", filename).Info, 2)("parsing launchpad config YAML")
data, err := os.ReadFile(filename)
if err != nil {
utils.DoubleIndent(log.WithError(err).WithField("path", filename).Fatal)("config file not found")
utils.Indent(log.WithError(err).WithField("path", filename).Fatal, 3)("config file not found")
return conf, err
}

err = yaml.Unmarshal(data, &conf)
if err != nil {
utils.DoubleIndent(log.WithError(err).WithField("path", filename).Fatal)("unmarshalling yaml failed")
utils.Indent(log.WithError(err).WithField("path", filename).Fatal, 3)("unmarshalling yaml failed")
return conf, err
}

// verify that all folders contain more than 1 item
for _, page := range conf.Apps.Pages {
for _, item := range page.Items {
switch item.(type) {
case string:
continue
default:
var folder AppFolder
if err := mapstructure.Decode(item, &folder); err != nil {
return Config{}, errors.Wrap(err, "mapstructure unable to decode config folder")
}
if len(folder.Pages) > 0 {
if len(folder.Pages[0].Items) < 2 {
return Config{}, fmt.Errorf("folder %s must contain more than 1 item to be valid", folder.Name)
}
}
}
}
if err := conf.Verify(); err != nil {
return conf, fmt.Errorf("config verification failed: %v", err)
}

return conf, nil
Expand Down
Loading

0 comments on commit 519ac6c

Please sign in to comment.