This repository has been archived by the owner on Aug 29, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
create.go
79 lines (69 loc) · 1.82 KB
/
create.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
package main
import (
"database/sql"
_ "github.com/mattn/go-sqlite3"
"bufio"
"errors"
"fmt"
"os"
"strings"
)
func ensureUserWantsAutocreation(dbPath string) error {
fmt.Printf(
"$PUNCH_CARD database not yet created\n\t%s\n", dbPath)
fmt.Printf("Should one be automatically started now? [y/N] ")
reader := bufio.NewReader(os.Stdin)
nextLine, e := reader.ReadString('\n')
if e != nil {
return fmt.Errorf("response parsing: %s", e)
}
response := strings.TrimSpace(nextLine)
if len(response) < 1 || strings.ToLower(string(response[0])) != "y" {
return errors.New("auto-creation offer not accepted")
}
return nil
}
func subCmdCreate(dbPath string) error {
if e := ensureUserWantsAutocreation(dbPath); e != nil {
return e
}
db, e := sql.Open("sqlite3", dbPath)
if e != nil {
return fmt.Errorf("error opening sqlite3: %s", e)
}
stmt, e := db.Prepare(`
CREATE TABLE punchcard (
punch INTEGER NOT NULL PRIMARY KEY,
status INTEGER NOT NULL,
project TEXT NOT NULL,
note TEXT
);
`)
if e != nil {
return fmt.Errorf("preparing punchcard table: %s", e)
}
if _, e := stmt.Exec(); e != nil {
return fmt.Errorf("creating punchcard table: %s", e)
}
stmt, e = db.Prepare(`
CREATE TABLE paychecks (
endclusive INTEGER NOT NULL PRIMARY KEY,
startclusive INTEGER NOT NULL,
project TEXT NOT NULL,
note TEXT
);
`)
if e != nil {
return fmt.Errorf("preparing paychecks table: %s", e)
}
if _, e := stmt.Exec(); e != nil {
return fmt.Errorf("creating paychecks table: %s", e)
}
fmt.Print(`Empty tables successfully created.
To start keep records try 'punch' and 'query' commands.
For reminders of their arguments, see '-h'.
For a listing of ALL commands and full docs, see 'help'
For a reminder of what one command does, see 'help [cmd]', eg: 'help punch'
`)
return nil
}