-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconfig.go
More file actions
154 lines (135 loc) · 3.8 KB
/
config.go
File metadata and controls
154 lines (135 loc) · 3.8 KB
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
package main
import (
"fmt"
"github.com/go-ini/ini"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"strconv"
"strings"
"time"
)
func GetLastVersion() (string, error) {
var version_url = "http://deb.codepicnic.com/version"
req, err := http.NewRequest("GET", version_url, nil)
req.Header.Set("Content-Type", "application/json")
req.Header.Set("User-Agent", user_agent)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
fmt.Printf(err.Error())
return "", err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
return strings.TrimRight(string(body), "\r\n"), nil
}
func IsLastVersion() bool {
last_version, err := GetLastVersion()
if err != nil {
return true
}
float_last_version, err := strconv.ParseFloat(last_version, 64)
float_version, err := strconv.ParseFloat(version, 64)
if float_last_version > float_version {
return false
}
return true
}
func CreateConfigDir() error {
config_file := config_dir + string(filepath.Separator) + cfg_file
os.Mkdir(config_dir, 0755)
if _, err := os.Stat(config_file); os.IsNotExist(err) {
f, err := os.Create(config_file)
if err != nil {
return err
}
f.Close()
}
return nil
}
func SaveMountsToFile(container string, mountpoint string) error {
cfg, err := ini.Load(getHomeDir() + "/" + cfg_dir + "/" + cfg_file)
if err != nil {
return err
}
cfg.Section("mounts").Key(container).SetValue(mountpoint)
err = cfg.SaveTo(getHomeDir() + "/" + cfg_dir + "/" + cfg_file)
if err != nil {
fmt.Println(color(msg_rwperms, "error"))
}
return nil
}
func RemoveMountFromFile(container string) error {
cfg, err := ini.Load(getHomeDir() + "/" + cfg_dir + "/" + cfg_file)
if err != nil {
return err
}
cfg.Section("mounts").DeleteKey(container)
err = cfg.SaveTo(getHomeDir() + "/" + cfg_dir + "/" + cfg_file)
if err != nil {
fmt.Println(color(msg_rwperms, "error"))
}
return nil
}
func GetMountsFromFile(container string) string {
cfg, err := ini.Load(getHomeDir() + "/" + cfg_dir + "/" + cfg_file)
if err != nil {
fmt.Println(color("Error1.", "error"))
fmt.Println(color(msg_rwperms, "error"))
}
mountpoint := cfg.Section("mounts").Key(container).String()
return mountpoint
}
func GetAllMountsFromFile() []string {
cfg, err := ini.Load(getHomeDir() + "/" + cfg_dir + "/" + cfg_file)
if err != nil {
fmt.Println(color("Error.", "error"))
fmt.Println(color(msg_rwperms, "error"))
}
mountpoint := cfg.Section("mounts").KeyStrings()
return mountpoint
}
func SaveTokenToFile(access_token string) {
cfg, err := ini.Load(getHomeDir() + "/" + cfg_dir + "/" + cfg_file)
if err != nil {
fmt.Println(color("Error2.", "error"))
fmt.Println(color(msg_rwperms, "error"))
}
cfg.Section("credentials").Key("access_token").SetValue(access_token)
err = cfg.SaveTo(getHomeDir() + "/" + cfg_dir + "/" + cfg_file)
if err != nil {
fmt.Println(color("Error.", "error"))
fmt.Println(color(msg_rwperms, "error"))
}
return
}
func IsFirstCheck() (bool, error) {
day := time.Duration(24) * time.Hour
cfg, err := ini.Load(getHomeDir() + "/" + cfg_dir + "/" + cfg_file)
if err != nil {
return false, err
}
last_update := cfg.Section("update").Key("last_update")
if last_update.String() == "" {
err = cfg.SaveTo(getHomeDir() + "/" + cfg_dir + "/" + cfg_file)
if err != nil {
return false, err
}
cfg.Section("update").Key("last_update").SetValue(time.Now().Format(time.RFC3339))
err = cfg.SaveTo(getHomeDir() + "/" + cfg_dir + "/" + cfg_file)
return true, nil
} else {
last_update_time := last_update.MustTime()
diff_update := time.Now().Sub(last_update_time)
if diff_update > day {
cfg.Section("update").Key("last_update").SetValue(time.Now().Format(time.RFC3339))
err = cfg.SaveTo(getHomeDir() + "/" + cfg_dir + "/" + cfg_file)
return true, nil
} else {
return false, nil
}
return false, nil
}
}