-
Notifications
You must be signed in to change notification settings - Fork 11
/
me.go
126 lines (118 loc) · 3.69 KB
/
me.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
124
125
126
package main
import (
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"time"
"github.com/codegangsta/cli"
"github.com/toorop/govh"
"github.com/toorop/govh/me"
)
// getMeCmds return commands for /me section
func getMeCmds(OVHClient *govh.OVHClient) (cmds []cli.Command) {
meClient, err := me.New(OVHClient)
if err != nil {
return
}
// Ip commands
cmds = []cli.Command{
{
// SUB bill
Name: "bill",
Description: "subcomands for bill",
Subcommands: []cli.Command{
{
// CMD list - list bill ID
Name: "list",
Description: "return bill IDs from dateFrom to dateTo",
Usage: "ovh me bill list [--from TIMESTAMP] [--to TIMESTAMP] [--json]" + NLTAB + "Example: ovh me bill list --from 1420066800 --to 1451602800",
Flags: []cli.Flag{
cli.IntFlag{Name: "from", Value: 0, Usage: "Date from"},
cli.IntFlag{Name: "to", Value: 0, Usage: "Date to"},
cli.BoolFlag{Name: "json", Usage: "output as JSON"},
},
Action: func(c *cli.Context) {
var dateFrom, dateTo time.Time
dateFrom = time.Unix(int64(c.Int("from")), 0)
if c.Int("to") == 0 {
dateTo = time.Now()
} else {
dateTo = time.Unix(int64(c.Int("to")), 0)
}
IDs, err := meClient.GetBillIDs(dateFrom, dateTo)
dieOnError(err)
println(formatOutput(IDs, c.Bool("json")))
dieOk()
},
}, {
// CMD getbyid - returns bill by its ID
Name: "getbyid",
Description: "returns bill from its ID",
Usage: "ovh me bill getbyid ID [--json]" + NLTAB + "Example: ovh me bill getbyid 123456789 --json",
Flags: []cli.Flag{
cli.BoolFlag{Name: "json", Usage: "output as JSON"},
},
Action: func(c *cli.Context) {
dieIfArgsMiss(len(c.Args()), 1)
bill, err := meClient.GetBillByID(c.Args().First())
dieOnError(err)
println(formatOutput(bill, c.Bool("json")))
dieOk()
},
}, {
// CMD dowload - download bills as PDF
Name: "download",
Description: "download bills from dateFrom to dateTo and save them to directory path",
Usage: "ovh me bill download --path SAVEPATH [--from TIMESTAMP] [--to TIMESTAMP] [--json]" + NLTAB + "Example: ovh me bill list --path /tmp --from 1420066800 --to 1451602800",
Flags: []cli.Flag{
cli.StringFlag{Name: "path", Value: "", Usage: "path to save bills pdf"},
cli.IntFlag{Name: "from", Value: 0, Usage: "Date from"},
cli.IntFlag{Name: "to", Value: 0, Usage: "Date to"},
},
Action: func(c *cli.Context) {
savePath := c.String("path")
if savePath == "" {
dieBadArgs("--path option is missing")
}
savePath, err = filepath.Abs(filepath.Clean(savePath))
dieOnError(err)
println(savePath)
stat, err := os.Stat(savePath)
//fmt.Printf("%v - %v\n", stat, err)
dieOnError(err)
if !stat.IsDir() {
dieError(fmt.Errorf("path %s is not a directory", savePath))
}
var dateFrom, dateTo time.Time
dateFrom = time.Unix(int64(c.Int("from")), 0)
if c.Int("to") == 0 {
dateTo = time.Now()
} else {
dateTo = time.Unix(int64(c.Int("to")), 0)
}
IDs, err := meClient.GetBillIDs(dateFrom, dateTo)
dieOnError(err)
fmt.Println(IDs)
// DL && save files
for _, ID := range IDs {
println("Downloading " + ID)
bill, err := meClient.GetBillByID(ID)
dieOnError(err)
f, err := os.Create(savePath + "/" + ID + ".pdf")
dieOnError(err)
defer f.Close()
resp, err := http.Get(bill.PdfURL)
dieOnError(err)
defer resp.Body.Close()
io.Copy(f, resp.Body)
}
dieOk()
},
},
},
},
}
return
}