/
format.go
53 lines (45 loc) · 1.03 KB
/
format.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
package main
import (
"bufio"
"encoding/json"
"fmt"
"os"
"github.com/danhunsaker/calends"
"github.com/urfave/cli"
)
func init() {
commands = append(commands, []cli.Command{
cli.Command{
Name: "format",
Usage: "format a timestamp in a given calendar system and format",
ArgsUsage: "<to-calendar> <to-format> [<stamp>]",
Action: func(c *cli.Context) error {
if c.NArg() < 2 || c.NArg() > 3 {
return errArgMismatch
}
out_cal := c.Args()[0]
out_form := c.Args()[1]
stamp := ""
if c.NArg() == 3 {
stamp = c.Args()[2]
} else {
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
stamp = fmt.Sprintf("%s%s", stamp, scanner.Text())
}
}
var moment calends.Calends
err := json.Unmarshal([]byte(stamp), &moment)
if err != nil {
return cli.NewExitError(err, 2)
}
output, err := moment.Date(out_cal, out_form)
if err != nil {
return cli.NewExitError(err, 2)
}
fmt.Println(output)
return nil
},
},
}...)
}