-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnew.go
178 lines (168 loc) · 4.94 KB
/
new.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package main
import (
"archive/zip"
"bytes"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"os"
"path/filepath"
"strings"
"github.com/finkf/pcwgo/api"
"github.com/spf13/cobra"
)
func init() {
newBookCommand.Flags().StringVarP(&opts.new.book.author, "author", "a", "",
"set book's author (required)")
newBookCommand.Flags().StringVarP(&opts.new.book.title, "title", "t", "",
"set book's title (required)")
newBookCommand.Flags().StringVarP(&opts.new.book.description,
"description", "d", "", "set book's description")
newBookCommand.Flags().StringVarP(&opts.new.book.language, "language", "l", "",
"set book's language (required)")
newBookCommand.Flags().StringVarP(&opts.new.book.profilerURL, "profilerurl", "u",
"local", "set book's profiler url")
newBookCommand.Flags().IntVarP(&opts.new.book.year, "year", "y", 1900,
"set book's year")
newBookCommand.Flags().StringVarP(&opts.new.book.histPatterns, "patters", "p", "",
"set additional historical patterns for the book")
_ = cobra.MarkFlagRequired(newBookCommand.Flags(), "author")
_ = cobra.MarkFlagRequired(newBookCommand.Flags(), "title")
_ = cobra.MarkFlagRequired(newBookCommand.Flags(), "language")
newUserCommand.Flags().StringVarP(&opts.new.user.name, "name", "n", "",
"set the user's name (required)")
newUserCommand.Flags().StringVarP(&opts.new.user.email, "email", "e", "",
"set the user's name (required)")
newUserCommand.Flags().StringVarP(&opts.new.user.password, "password", "p",
"", "set the user's password (required)")
newUserCommand.Flags().StringVarP(&opts.new.user.institute, "institute",
"i", "", "set the user's institute")
newUserCommand.Flags().BoolVarP(&opts.new.user.admin, "admin", "a", false,
"user has administrator permissions")
_ = cobra.MarkFlagRequired(newUserCommand.Flags(), "name")
_ = cobra.MarkFlagRequired(newUserCommand.Flags(), "email")
_ = cobra.MarkFlagRequired(newUserCommand.Flags(), "password")
}
var newCommand = cobra.Command{
Use: "new",
Short: "Create new books and users",
}
var newBookCommand = cobra.Command{
Use: "book [ZIP|DIR]",
Short: "Create a new book",
RunE: newBook,
Args: cobra.ExactArgs(1),
}
func newBook(_ *cobra.Command, args []string) error {
zip, err := openAsZIP(args[0])
if err != nil {
return fmt.Errorf("cannot create new book: open %s: %v", args[0], err)
}
defer zip.Close()
c := authenticate()
url := newBookURL(c)
req, err := http.NewRequest(http.MethodPost, url, zip)
if err != nil {
return fmt.Errorf("cannot create new book: %v", err)
}
req.Header.Add("Content-Type", "application/zip")
res, err := c.Do(req)
if err != nil {
return fmt.Errorf("cannot create new book: %v", err)
}
var book api.Book
if err := api.UnmarshalResponse(res, &book); err != nil {
return fmt.Errorf("cannot create new book: %v", err)
}
format(&book)
return nil
}
func openAsZIP(p string) (io.ReadCloser, error) {
fi, err := os.Lstat(p)
if err != nil {
return nil, err
}
if !fi.IsDir() {
return os.Open(p)
}
var buf bytes.Buffer
w := zip.NewWriter(&buf)
pre := filepath.Base(p)
err = filepath.Walk(p, func(p string, fi os.FileInfo, err error) error {
if err != nil {
return err
}
header, e := zip.FileInfoHeader(fi)
if e != nil {
return e
}
pos := strings.Index(p, pre)
if pos == -1 {
return fmt.Errorf("missing prefix in %q", p)
}
internalPath := filepath.Join(pre, p[pos+len(pre)+1:])
if fi.IsDir() {
internalPath += "/"
header.Name = internalPath
_, e := w.CreateHeader(header)
return e
}
// copy file
header.Method = zip.Deflate
header.Name = internalPath
// open file
in, e := os.Open(p)
if e != nil {
return e
}
defer in.Close()
out, e := w.CreateHeader(header)
if e != nil {
return e
}
// write to archive
_, e = io.Copy(out, in)
return e
})
w.Close()
return ioutil.NopCloser(&buf), err
}
func newBookURL(c *api.Client) string {
return c.URL("books?author=%s&title=%s&language=%s"+
"&description=%s&histPatterns=%s&profilerUrl=%s&year=%d",
url.QueryEscape(opts.new.book.author),
url.QueryEscape(opts.new.book.title),
url.QueryEscape(opts.new.book.language),
url.QueryEscape(opts.new.book.description),
url.QueryEscape(opts.new.book.histPatterns),
url.QueryEscape(opts.new.book.profilerURL),
opts.new.book.year)
}
var newUserCommand = cobra.Command{
Use: "user",
Short: "Create a new user",
RunE: newUser,
}
func newUser(cmd *cobra.Command, args []string) error {
if opts.new.user.email == "" || opts.new.user.password == "" {
return fmt.Errorf("missing user email and/or password")
}
var newUser api.User
c := authenticate()
err := post(c, c.URL("users"), api.CreateUserRequest{
User: api.User{
Name: opts.new.user.name,
Email: opts.new.user.email,
Institute: opts.new.user.institute,
Admin: opts.new.user.admin,
},
Password: opts.new.user.password,
}, &newUser)
if err != nil {
return fmt.Errorf("cannot create user %s: %v", opts.new.user.email, err)
}
format(&newUser)
return nil
}