-
Notifications
You must be signed in to change notification settings - Fork 223
/
client.go
56 lines (47 loc) · 1006 Bytes
/
client.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
package client
import (
"encoding/json"
"io/ioutil"
"os"
"github.com/fatih/color"
"github.com/xalanq/cf-tool/cookiejar"
)
// Client codeforces client
type Client struct {
Jar *cookiejar.Jar `json:"cookies"`
Username string `json:"username"`
Ftaa string `json:"ftaa"`
Bfaa string `json:"bfaa"`
path string
}
// New client
func New(path string) *Client {
jar, _ := cookiejar.New(nil)
c := &Client{Jar: jar, path: path}
c.load()
return c
}
// load from path
func (c *Client) load() (err error) {
file, err := os.Open(c.path)
if err != nil {
return
}
defer file.Close()
bytes, err := ioutil.ReadAll(file)
if err != nil {
return err
}
return json.Unmarshal(bytes, c)
}
// save file to path
func (c *Client) save() (err error) {
data, err := json.MarshalIndent(c, "", " ")
if err == nil {
err = ioutil.WriteFile(c.path, data, 0644)
}
if err != nil {
color.Red("Cannot save session to %v\n%v", c.path, err.Error())
}
return
}