Skip to content

Commit

Permalink
fix: #47
Browse files Browse the repository at this point in the history
  • Loading branch information
blacktop committed Sep 15, 2023
1 parent 519ac6c commit 9df29eb
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 13 deletions.
25 changes: 22 additions & 3 deletions internal/command/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)

Expand All @@ -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)
Expand Down
19 changes: 11 additions & 8 deletions internal/command/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
9 changes: 7 additions & 2 deletions internal/database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,15 +93,20 @@ 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")
} else {
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)
}
Expand Down

0 comments on commit 9df29eb

Please sign in to comment.