diff --git a/.vscode/launch.json b/.vscode/launch.json index 3c351e5..e6c798b 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -13,9 +13,10 @@ "program": "${workspaceRoot}", "args": [ "-V", - "--yes", "load", - "launchpad.yaml" + "--yes", + "--config", + "${userHome}/Library/Application Support/lporg/config.yml" ] }, { @@ -44,29 +45,5 @@ "default" ] }, - { - "name": "Decode", - "type": "go", - "request": "launch", - "mode": "debug", - "showLog": false, - "program": "${workspaceRoot}/test" - }, - { - "name": "Dock", - "type": "go", - "request": "launch", - "mode": "debug", - "showLog": false, - "program": "${workspaceRoot}/dock" - }, - { - "name": "Desktop", - "type": "go", - "request": "launch", - "mode": "debug", - "showLog": false, - "program": "${workspaceRoot}/desktop" - } ] } \ No newline at end of file diff --git a/internal/command/command.go b/internal/command/command.go index 96aa265..b6318cc 100644 --- a/internal/command/command.go +++ b/internal/command/command.go @@ -572,14 +572,14 @@ func LoadConfig(c *Config) error { return errors.Wrap(err, "unable to load dock plist") } if len(dPlist.PersistentApps) > 0 { - clear(dPlist.PersistentApps) // remove all apps from dock + dPlist.PersistentApps = nil // remove all apps from dock } for _, app := range config.Dock.Apps { utils.DoubleIndent(log.WithField("app", app).Info)("adding to dock") dPlist.AddApp(app) } if len(dPlist.PersistentOthers) > 0 { - clear(dPlist.PersistentOthers) // remove all folders from dock + dPlist.PersistentOthers = nil // remove all folders from dock } for _, other := range config.Dock.Others { utils.DoubleIndent(log.WithField("other", other).Info)("adding to dock") diff --git a/internal/dock/dock.go b/internal/dock/dock.go index 372acc5..b794fce 100644 --- a/internal/dock/dock.go +++ b/internal/dock/dock.go @@ -11,6 +11,7 @@ import ( "strings" "time" + "github.com/apex/log" "github.com/blacktop/lporg/internal/utils" "howett.net/plist" ) @@ -152,7 +153,7 @@ func (p *Plist) AddApp(appPath string) error { FileType: 41, FileData: FileData{ URLString: strings.Replace(fmt.Sprintf("file://%s/", appPath), " ", "%20", -1), - URLStringType: 15, + URLStringType: 0, }, FileLabel: fileNameWithoutExtTrimSuffix(appPath), FileModDate: time.Now().Unix(), @@ -176,7 +177,7 @@ func (p *Plist) AddOther(otherPath string) error { Arrangement: index + 1, FileData: FileData{ URLString: strings.Replace(fmt.Sprintf("file://%s/", otherPath), " ", "%20", -1), - URLStringType: 15, + URLStringType: 0, }, FileLabel: fileNameWithoutExtTrimSuffix(otherPath), FileModDate: time.Now().Unix(), @@ -196,39 +197,55 @@ func (p *Plist) Save() error { p.ModCount++ + // backup previous plist home, err := os.UserHomeDir() if err != nil { return fmt.Errorf("failed to get user home directory: %v", err) } - - // read users dock plist pfile, err := os.Open(filepath.Join(home, dockPlistPath)) if err != nil { return fmt.Errorf("failed to open plist: %w", err) } defer pfile.Close() - bak, err := os.Create(filepath.Join(home, dockPlistPath) + ".bak") if err != nil { return fmt.Errorf("failed to create backup plist: %w", err) } defer bak.Close() - - if _, err := io.Copy(bak, pfile); err != nil { // backup previous plist + if _, err := io.Copy(bak, pfile); err != nil { return fmt.Errorf("failed to backup plist: %w", err) } - if _, err := pfile.Seek(0, 0); err != nil { // reset file pointer - return fmt.Errorf("failed to reset plist file pointer: %w", err) - } - if err := plist.NewDecoder(pfile).Decode(p); err != nil { + // write dock plist to temp file + tmp, err := os.CreateTemp("", "dock.plist") + if err != nil { + return fmt.Errorf("failed to create temp file: %v", err) + } + defer os.Remove(tmp.Name()) + if err := plist.NewBinaryEncoder(tmp).Encode(p); err != nil { return fmt.Errorf("failed to decode plist: %w", err) } + tmp.Close() + // import plist and restart dock + if err := p.importPlist(tmp.Name()); err != nil { + return fmt.Errorf("failed to import plist: %w", err) + } return p.kickstart() } +func (p *Plist) importPlist(path string) error { + utils.DoubleIndent(log.Info)("importing dock plist") + out, err := utils.RunCommand(context.Background(), "/usr/bin/defaults", "import", "com.apple.dock", path) + if err != nil { + return fmt.Errorf("failed to defaults import dock plist '%s': %v", path, err) + } + fmt.Println(out) + return nil +} + func (p *Plist) kickstart() error { + utils.DoubleIndent(log.Info)("restarting com.apple.Dock.agent service") out, err := utils.RunCommand(context.Background(), "/bin/launchctl", "kickstart", "-k", fmt.Sprintf("gui/%d/com.apple.Dock.agent", os.Getuid())) if err != nil { return fmt.Errorf("failed to kickstart dock: %v", err)