This repository has been archived by the owner on Jun 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
68 lines (56 loc) · 1.35 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
package main
import (
"flag"
"fmt"
"io/ioutil"
"log"
"os"
"text/template"
"time"
"github.com/rakyll/statik/fs"
_ "github.com/shouth/studio-aquatan-golang-hands-on/statik"
)
var flagVersion bool
var flagVerbose bool
func main() {
rootCmd := flag.NewFlagSet("Root", flag.ContinueOnError)
rootCmd.BoolVar(&flagVersion, "version", false, "print version")
rootCmd.BoolVar(&flagVersion, "v", false, "print version")
rootCmd.BoolVar(&flagVerbose, "verbose", false, "print log")
err := rootCmd.Parse(os.Args[1:])
if err != nil {
if err == flag.ErrHelp {
os.Exit(0)
}
log.Fatal(err)
}
if flagVersion {
fmt.Println("golang hands-on 0.0.1")
}
addCmd := flag.NewFlagSet("add", flag.ContinueOnError)
var fileName string
addCmd.StringVar(&fileName, "name", time.Now().Format("2006-01-02")+".md", "filename")
args := rootCmd.Args()
if len(args) > 0 {
switch args[0] {
case "add":
_ = addCmd.Parse(args[1:])
handleAddCmd(fileName)
}
}
}
func handleAddCmd(filename string) error {
statikFs, _ := fs.New()
tplFile, _ := statikFs.Open("/report.md.tmpl")
btpl, _ := ioutil.ReadAll(tplFile)
stpl := string(btpl)
tpl := template.Must(template.New("report").Parse(stpl))
rptFile, _ := os.Create(filename)
rptData := struct {
Today string
}{
Today: time.Now().Format("2006-01-02"),
}
_ = tpl.Execute(rptFile, rptData)
return nil
}