-
Notifications
You must be signed in to change notification settings - Fork 1
/
cli.go
77 lines (63 loc) · 1.34 KB
/
cli.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
package main
import (
"fmt"
"log"
"os"
"strconv"
"strings"
"github.com/codegangsta/cli"
"github.com/ryosan-470/factordb-go/lib"
)
const (
name = "factordb"
usage = "The CLI for factordb.com"
version = "1.0.0"
)
func main() {
app := cli.NewApp()
app.Name = name
app.Usage = usage
app.Version = version
// Global option
app.Flags = []cli.Flag{
// --json
cli.BoolFlag{
Name: "json",
Usage: "Return response formated JSON",
},
}
app.Action = callAction
app.Run(os.Args)
}
func callAction(c *cli.Context) error {
var number = ""
if len(c.Args()) > 0 {
number = c.Args().First()
}
n, err := strconv.Atoi(number)
if err != nil {
fmt.Println("Your input is not number")
os.Exit(1)
}
f := lib.FactorDB{Number: n}
if err := f.Connect(); err != nil {
log.Fatal("Connection Error")
}
factors, _ := f.GetFactorList()
var output string
var isJson = c.GlobalBool("json")
if isJson {
id, _ := f.GetId()
status, _ := f.GetStatus()
var fs []string
for _, f := range factors {
fs = append(fs, fmt.Sprintf("%d", f))
}
facs := fmt.Sprintf("[%s]", strings.Join(fs, ", "))
output = fmt.Sprintf("{\"id\": \"https://factordb.com/?id=%s\", \"status\": \"%s\", \"factors\": %v}", id, status, facs)
} else {
output = strings.Trim(fmt.Sprintf("%v", factors), "[]")
}
fmt.Printf("%s\n", output)
return nil
}