-
Notifications
You must be signed in to change notification settings - Fork 0
/
setwp.go
138 lines (118 loc) · 2.62 KB
/
setwp.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package main
import (
"database/sql"
"errors"
"log"
"os"
"os/exec"
"os/user"
"path/filepath"
"github.com/alexcormier/setwp/args"
"github.com/alexcormier/setwp/pref"
_ "github.com/mattn/go-sqlite3"
)
const (
dbRelativePath = "Library/Application Support/Dock/desktoppicture.db"
clearDBStatement = `
delete from preferences;
delete from data;
`
setPrefDBStatement = `
insert into data
select ?
where not exists (
select value
from data
where value = ?);
insert into preferences
select ?, data.ROWID, pictures.ROWID
from pictures
inner join data
on data.value = ?;
`
)
func main() {
os.Exit(run())
}
func run() int {
log.SetFlags(0)
prefs, err := args.Parse()
if err != nil {
return handleArgumentError(err)
}
home, err := homeDir()
if err != nil {
return handleDbReadError(err)
}
dbPath := filepath.Join(home, dbRelativePath)
if ret := changeWallpaperSettings(dbPath, prefs); ret != 0 {
return ret
}
if ret := restartDock(); ret != 0 {
return ret
}
return 0
}
func changeWallpaperSettings(dbPath string, prefs pref.Prefs) int {
db, err := sql.Open("sqlite3", dbPath)
if err != nil {
return handleDbReadError(err)
}
defer db.Close()
tx, err := db.Begin()
if err != nil {
return handleDbWriteError(err)
}
success := false
defer closeTx(tx, &success)
if err := clearDB(tx); err != nil {
return handleDbWriteError(err)
}
for key, value := range prefs {
if err := setPref(tx, key, value); err != nil {
return handleDbWriteError(err)
}
}
success = true
return 0
}
func restartDock() int {
if err := exec.Command("killall", "Dock").Run(); err != nil {
log.Println("error applying wallpaper, it will be applied on your next login")
return 100
}
return 0
}
// Clears the wallpaper preferences database.
func clearDB(tx *sql.Tx) error {
_, err := tx.Exec(clearDBStatement)
return err
}
// Sets a preference in the database.
func setPref(tx *sql.Tx, key pref.KeyType, value pref.ValueType) error {
_, err := tx.Exec(setPrefDBStatement, value, value, key, value)
return err
}
// Commits or rollbacks the transaction depending on success.
func closeTx(tx *sql.Tx, success *bool) {
if *success {
if err := tx.Commit(); err != nil {
os.Exit(handleDbWriteError(err))
}
} else {
if err := tx.Rollback(); err != nil {
os.Exit(handleDbRollbackError(err))
}
}
}
// Gets the current user's home directory.
func homeDir() (string, error) {
currentUser, err := user.Current()
if err != nil {
return "", errors.New("unknown current user")
}
if currentUser.HomeDir == "" {
return "", errors.New("unknown home directory")
}
return currentUser.HomeDir, nil
}