Skip to content

Commit

Permalink
😞
Browse files Browse the repository at this point in the history
  • Loading branch information
blacktop committed Dec 26, 2017
1 parent d4d3c30 commit 74e259e
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 5 deletions.
6 changes: 5 additions & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@
"request": "launch",
"mode": "debug",
"showLog": false,
"program": "${workspaceRoot}"
"program": "${workspaceRoot}",
"args": [
"-V",
"default"
]
}
]
}
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
---

> **NOTE:** Tested on High Sierra
## Getting Started

```sh
Expand Down Expand Up @@ -130,3 +132,8 @@ Run 'lporg COMMAND --help' for more information on a command.
}
}
```

## TODO

* [ ] swith to Apex log
* [ ] figure out how to write to DB and not just read :disappointed:
67 changes: 63 additions & 4 deletions launch.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (

var (
// ctx *log.Entry

// Version stores the plugin's version
Version string
// BuildTime stores the plugin's build time
Expand Down Expand Up @@ -59,7 +60,7 @@ type Item struct {
Type int `gorm:"column:type"`
// ParentID Group `db:"parent_id"`
Group Group `gorm:"ForeignKey:ParentID"`
ParentID int `gorm:"not null;unique;column:parent_id"`
ParentID int `gorm:"not null;column:parent_id"`
Ordering int `gorm:"column:ordering"`
}

Expand All @@ -69,6 +70,58 @@ func checkError(err error) {
}
}

func createNewGroup(db *gorm.DB, title string) (Group, error) {

group := Group{Title: title}

success := db.NewRecord(group) // => returns `true` as primary key is blank
log.WithFields(log.Fields{"success": success}).Debug("create new group record")

err := db.Create(&group).Error
if err != nil {
return group, errors.Wrap(err, "create new entry failed")
}

emptyGroup := Group{}
success = db.NewRecord(emptyGroup)
log.WithFields(log.Fields{"success": success}).Debug("create new empty group record")

err = db.Create(&emptyGroup).Error
if err != nil {
return group, errors.Wrap(err, "create new EMPTY entry failed")
}

return group, err
}

func addAppToGroup(db *gorm.DB, appName, groupName string) error {

var (
app App
item Item
group Group
)

if err := db.Where("title = ?", appName).First(&app).Error; err != nil {
log.Error(err)
}
if err := db.Where("rowid = ?", app.ItemID).First(&item).Error; err != nil {
log.Error(err)
}
if err := db.Where("title = ?", groupName).First(&group).Error; err != nil {
log.Error(err)
}
return updateItemGroup(db, group.ID+1, &item)
}

// CREATE TRIGGER update_item_parent AFTER UPDATE OF parent_id ON items WHEN 0 == (SELECT value FROM dbinfo WHERE key='ignore_items_update_triggers') BEGIN UPDATE dbinfo SET value=1 WHERE key='ignore_items_update_triggers'; UPDATE items SET ordering = (SELECT ifnull(MAX(ordering),0)+1 FROM items WHERE parent_id=new.parent_id AND ROWID!=old.rowid) WHERE ROWID=old.rowid; UPDATE items SET ordering = ordering - 1 WHERE parent_id = old.parent_id and ordering > old.ordering; UPDATE dbinfo SET value=0 WHERE key='ignore_items_update_triggers'; END
func updateItemGroup(db *gorm.DB, groupID int, item *Item) error {
item.ParentID = groupID
// item.Ordering = 0
return db.Save(&item).Error
// return db.Model(&item).Update("parent_id", groupID).Error
}

// CmdDefaultOrg will organize your launchpad by the app default categories
func CmdDefaultOrg(verbose bool) error {

Expand Down Expand Up @@ -107,6 +160,12 @@ func CmdDefaultOrg(verbose bool) error {
db.LogMode(true)
}

// grp, err := createNewGroup(db, "Porg")
// checkError(err)
// checkError(addAppToGroup(db, "Atom", grp.Title))
// checkError(addAppToGroup(db, "Brave", grp.Title))
// checkError(addAppToGroup(db, "iTerm", grp.Title))

if err := db.Where("type = ?", "4").Find(&items).Error; err != nil {
log.Error(err)
}
Expand All @@ -117,7 +176,8 @@ func CmdDefaultOrg(verbose bool) error {
db.Model(&item.App).Related(&item.App.Category)
log.Debugf("App: %s, item.ParentID=%d\n", item.App.Title, item.ParentID-1)
if err := db.First(&group, item.ParentID-1).Error; err != nil {
return err
log.WithFields(log.Fields{"ParentID": item.ParentID - 1}).Debug(errors.Wrap(err, "find group with item.ParentID-1 failed"))
continue
}
item.Group = group
log.Debugf("%+v\n", group)
Expand Down Expand Up @@ -197,9 +257,8 @@ func main() {
}

if c.Args().Present() {

// user supplied launchpad config YAML
log.Infoln("IMPLIMENT HERE <=================")
log.Infoln("IMPLIMENT LOADING FROM CONFIG YAML HERE <=================")
fmt.Println(porg)
} else {
cli.ShowAppHelp(c)
Expand Down
3 changes: 3 additions & 0 deletions scripts/copy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,8 @@
echo "===> Removing old DB..."
rm launchpad.db*

echo "===> Showing DB files..."
tree $TMPDIR../0/com.apple.dock.launchpad

echo "===> Copying new DB..."
cp $TMPDIR../0/com.apple.dock.launchpad/db/db ./launchpad.db

0 comments on commit 74e259e

Please sign in to comment.