From 9df29eb93bdf5c65f6001b13a52b51fc8deede5a Mon Sep 17 00:00:00 2001 From: blacktop Date: Thu, 14 Sep 2023 23:46:24 -0600 Subject: [PATCH] fix: #47 --- internal/command/command.go | 25 ++++++++++++++++++++++--- internal/command/utils.go | 19 +++++++++++-------- internal/database/database.go | 9 +++++++-- 3 files changed, 40 insertions(+), 13 deletions(-) diff --git a/internal/command/command.go b/internal/command/command.go index ec4ef59..2e34e1a 100644 --- a/internal/command/command.go +++ b/internal/command/command.go @@ -193,17 +193,17 @@ func DefaultOrg(c *Config) (err error) { // Clear all items related to groups so we can re-create them if err := lpad.ClearGroups(); err != nil { - return fmt.Errorf("failed to ClearGroups: %w", err) + return fmt.Errorf("failed to ClearGroups: %v", err) } // Disable the update triggers if err := lpad.DisableTriggers(); err != nil { - return fmt.Errorf("failed to DisableTriggers: %w", err) + return fmt.Errorf("failed to DisableTriggers: %v", err) } // Add root and holding pages to items and groups if err := lpad.AddRootsAndHoldingPages(); err != nil { - return fmt.Errorf("failed to AddRootsAndHoldingPagesfailed: %w", err) + return fmt.Errorf("failed to AddRootsAndHoldingPagesfailed: %v", err) } // We will begin our group records using the max ids found (groups always appear after apps and widgets) @@ -238,6 +238,21 @@ func DefaultOrg(c *Config) (err error) { folder.Pages = append(folder.Pages, folderPage) page.Items = append(page.Items, folder) } + if err := lpad.DB.Where("category_id IS NULL").Find(&apps).Error; err != nil { + log.WithError(err).Error("categories query failed") + } + if len(apps) > 0 { + folder := database.AppFolder{Name: "Misc"} + for idx, appPage := range split(apps, 35) { + folderPage := database.FolderPage{Number: idx + 1} + for _, app := range appPage { + utils.Indent(log.WithField("app", app.Title).Info, 4)("adding app to Misc folder") + folderPage.Items = utils.AppendIfMissing(folderPage.Items, app.Title) + } + folder.Pages = append(folder.Pages, folderPage) + } + page.Items = append(page.Items, folder) + } config.Apps.Pages = append(config.Apps.Pages, page) @@ -261,6 +276,10 @@ func DefaultOrg(c *Config) (err error) { return fmt.Errorf("failed to GetMissing=>Apps: %v", err) } + 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(config.Apps, groupID, 1); err != nil { return fmt.Errorf("failed to DefaultOrg->ApplyConfig: %w", err) diff --git a/internal/command/utils.go b/internal/command/utils.go index a09a8ff..5d7e395 100644 --- a/internal/command/utils.go +++ b/internal/command/utils.go @@ -109,15 +109,18 @@ func getiCloudDrivePath() (string, error) { return filepath.Join(home, "Library/Mobile Documents/com~apple~CloudDocs"), nil } -func split(buf []string, lim int) [][]string { - var chunk []string - chunks := make([][]string, 0, len(buf)/lim+1) - for len(buf) >= lim { - chunk, buf = buf[:lim], buf[lim:] - chunks = append(chunks, chunk) +func split[T any](buf []T, lim int) [][]T { + var chunk []T + chunks := make([][]T, 0, lim) + for _, b := range buf { + chunk = append(chunk, b) + if len(chunk) == lim { + chunks = append(chunks, chunk) + chunk = nil + } } - if len(buf) > 0 { - chunks = append(chunks, buf[:]) + if len(chunk) > 0 { + chunks = append(chunks, chunk) } return chunks } diff --git a/internal/database/database.go b/internal/database/database.go index 31deec3..1b34b98 100644 --- a/internal/database/database.go +++ b/internal/database/database.go @@ -93,7 +93,7 @@ func (lp *LaunchPad) GetMissing(apps *Apps, appType int) error { return fmt.Errorf("mapstructure unable to decode config folder: %w", err) } for fpIdx, fpage := range folder.Pages { - ftmp := []any{} + ftmp := []string{} for _, fitem := range fpage.Items { if !slices.Contains(lp.dbApps, fitem) { utils.Indent(log.WithField("app", fitem).Warn, 3)("found app in config that are is not on system") @@ -101,7 +101,12 @@ func (lp *LaunchPad) GetMissing(apps *Apps, appType int) error { ftmp = append(ftmp, fitem) } } - item.(map[string]any)["pages"].([]any)[fpIdx].(map[string]any)["items"] = ftmp + switch item.(type) { + case AppFolder: + item.(AppFolder).Pages[fpIdx].Items = ftmp + default: + item.(map[string]any)["pages"].([]any)[fpIdx].(map[string]any)["items"] = ftmp + } } tmp = append(tmp, item) }