-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
123 lines (103 loc) · 2.46 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
package main
import (
"flag"
"fmt"
"log"
"os"
"strings"
"time"
"github.com/nexneo/samay/data"
)
var (
cmd,
name,
typ,
newName,
httpPort,
content string
billable bool
duration time.Duration
theIdx,
month,
week int
cmdflags *flag.FlagSet
)
func main() {
parseFlags()
project := data.CreateProject(name)
// fmt.Println("Running...", cmd)
fn := commands[cmd]
if fn != nil {
err := fn(project)
if err != nil {
log.Fatalln(err)
}
} else {
usage()
}
}
func parseFlags() {
if len(os.Args) > 1 {
cmd = os.Args[1]
} else {
cmd = "report"
}
initflags(cmd)
if cmd == "help" {
usage()
os.Exit(0)
}
if len(os.Args) < 3 {
cmdflags.Parse(os.Args)
} else if cmd == "mv" {
name = os.Args[2]
newName = os.Args[3]
cmdflags.Parse(os.Args[4:])
projectNameCantbe(newName)
} else {
cmdflags.Parse(os.Args[2:])
// if -p flag not set, check if second argument is Project name
if name == "" && !strings.HasPrefix(os.Args[2], "-") {
name = os.Args[2]
cmdflags.Parse(os.Args[3:])
}
}
if name == "" && cmd != "report" && cmd != "web" {
log.Print("Error: Project name required.\n\n")
usage()
os.Exit(0)
}
projectNameCantbe(name)
}
func projectNameCantbe(name string) {
if strings.HasPrefix(name, "-") {
log.Printf("Error: Project name can't be \"%s\"", name)
usage()
os.Exit(0)
}
}
// Bind variables with command line flags
func initflags(cmd string) {
cmdflags = flag.NewFlagSet(cmd, flag.ExitOnError)
cmdflags.StringVar(&content, "m", "", "Description for entry")
cmdflags.StringVar(&httpPort, "port", ":8080", "HTTP Port")
cmdflags.IntVar(&month, "r", int(time.Now().Month()), "Report Month")
cmdflags.IntVar(&theIdx, "i", -1, "Entry index(#) in log")
cmdflags.DurationVar(&duration, "d", time.Hour, "Entry Duration")
cmdflags.BoolVar(&billable, "bill", true, "Billable")
}
func usage() {
fmt.Fprintf(os.Stderr,
"Usage: samay [command] [project] [new project] [options]\n\nCommands:\n%s%s%s%s%s%s%s%s%s\n\nOptions:\n",
" start (s) : Start timer\n",
" stop (p) : Stop Timer and entry with -m=(description) or uses $EDITOR\n",
" entry (e) : Direct entry with -d=(duration)\n",
" rm : Remove all entries, or single with -i=(#)\n",
" mv : Move all entries to new project\n",
" log : Show log for project\n",
" show : Show project or entry with -i=(#)\n",
" report : Report for all projects\n",
" help : Help\n")
cmdflags.Parse([]string{})
cmdflags.PrintDefaults()
}