-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
171 lines (155 loc) · 3.1 KB
/
main.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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
package main
import (
"bufio"
"bytes"
"encoding/json"
"fmt"
"io"
"os"
"path/filepath"
"strings"
)
const Version = "0.1.0"
func main() {
wipfile := GetWIPFile()
wiplog := NewWIPLog(wipfile)
args := os.Args
command := "show"
if len(args) > 1 {
command = args[1]
}
switch command {
case "push":
if len(args) < 3 {
fmt.Fprintf(os.Stderr, "usage: wip push <item>\n")
os.Exit(1)
}
item := strings.Join(args[2:], " ")
err := wiplog.Push(item)
if err != nil {
fmt.Fprintf(os.Stderr, `error pushing item "%s": %s\n`, item, err.Error())
os.Exit(1)
}
case "pop":
err := wiplog.Pop()
if err != nil {
fmt.Fprintf(os.Stderr, `error poping item: %s\n`, err.Error())
os.Exit(1)
}
case "show":
var stack []string
err := wiplog.Each(func(pushedItem string) {
stack = append(stack, pushedItem)
}, func() {
if len(stack) == 0 {
return
}
stack = stack[:len(stack)-1]
})
if err != nil {
fmt.Fprintf(os.Stderr, "error parsing wipfile: %s\n", err.Error())
os.Exit(1)
}
for i := 0; i < len(stack); i++ {
fmt.Printf("%d: %s\n", i, stack[len(stack)-1-i])
}
case "version":
fmt.Println(Version)
default:
fmt.Fprintf(os.Stderr, "unknown command %s\n", args[1])
os.Exit(1)
}
}
func GetWIPFile() string {
wipfile := os.Getenv("WIPFILE")
if wipfile != "" {
var err error
wipfile, err = filepath.Abs(wipfile)
if err != nil {
panic(err)
}
return wipfile
}
return DefaultWIPFile()
}
func DefaultWIPFile() string {
home, err := os.UserHomeDir()
if err != nil {
panic(err)
}
return filepath.Join(home, ".wip")
}
type WIPLog struct {
wipfile string
}
func (wl *WIPLog) Each(onPush func(string), onPop func()) error {
fh, err := wl.openReadable()
if err != nil {
return err
}
defer fh.Close()
lines := bufio.NewScanner(fh)
lines.Split(bufio.ScanLines)
for lines.Scan() {
line := lines.Bytes()
var op Op
err = json.Unmarshal(line, &op)
if err != nil {
return err
}
if op.Push != nil {
onPush(op.Push.Item)
}
if op.Pop != nil {
onPop()
}
}
return lines.Err()
}
func (wl *WIPLog) Push(item string) error {
fh, err := wl.openWritable()
if err != nil {
return err
}
defer fh.Close()
op := &Op{Push: &PushOp{Item: item}}
buf := bytes.NewBuffer(nil)
err = json.NewEncoder(buf).Encode(op)
if err != nil {
return err
}
_, err = io.Copy(fh, buf)
return err
}
func (wl *WIPLog) Pop() error {
fh, err := wl.openWritable()
if err != nil {
return err
}
defer fh.Close()
op := &Op{Pop: &PopOp{}}
buf := bytes.NewBuffer(nil)
err = json.NewEncoder(buf).Encode(op)
if err != nil {
return err
}
_, err = io.Copy(fh, buf)
return err
}
type Op struct {
Push *PushOp `json:"push,omitempty"`
Pop *PopOp `json:"pop,omitempty"`
}
type PushOp struct {
Item string `json:"item"`
}
type PopOp struct{}
func (wl *WIPLog) openWritable() (*os.File, error) {
return os.OpenFile(wl.wipfile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0600)
}
func (wl *WIPLog) openReadable() (*os.File, error) {
return os.OpenFile(wl.wipfile, os.O_CREATE|os.O_RDONLY, 0600)
}
func NewWIPLog(wipfile string) *WIPLog {
return &WIPLog{wipfile: wipfile}
}