-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinit.go
44 lines (39 loc) · 897 Bytes
/
init.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
package main
import (
"fmt"
"os"
"sort"
"github.com/tobstarr/wlcli/Godeps/_workspace/src/github.com/BurntSushi/toml"
"github.com/tobstarr/wlcli/Godeps/_workspace/src/github.com/dynport/gocli"
)
type initAction struct {
}
func (r *initAction) Run() error {
path := configFileName
if _, err := os.Stat(path); err == nil {
return fmt.Errorf("config file %s already exists", path)
}
cl, err := loadClient()
if err != nil {
return err
}
lists, err := cl.Lists()
if err != nil {
return err
}
t := gocli.NewTable()
for _, l := range lists {
t.Add(l.Title)
}
t.SortBy = 0
sort.Sort(t)
list := lists[t.Select("select a list")]
l.Printf("initializing list %d %s", list.ID, list.Title)
c := &Config{ListID: list.ID}
f, err := os.OpenFile(path, os.O_CREATE|os.O_EXCL|os.O_RDWR, 0644)
if err != nil {
return err
}
defer f.Close()
return toml.NewEncoder(f).Encode(c)
}