-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
114 lines (91 loc) · 1.98 KB
/
config.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
package main
import (
"github.com/BurntSushi/toml"
"io/ioutil"
"log"
"os"
)
type Config struct {
GyazoId string
HomeDir string
HistDir string
IdFilename string
Endpoint string
BasicUser string
BasicPassword string
}
type Toml struct {
Profile []Profiles `toml:"profile"`
}
type Profiles struct {
BasicUser string `toml:"basicUser"`
BasicPassword string `toml:"basicPassword"`
Endpoint string `toml:"endpoint"`
Name string `toml:"name"`
}
func NewGyazo() *Gyazo {
var g Gyazo
g.Config.IdFilename = "/.gyazo.id"
g.Config.createHomeDir()
g.Config.createHistDir()
g.Config.getGyazoId()
return &g
}
func (c *Config) createHomeDir() {
//home directory
c.HomeDir = os.Getenv("HOME") + "/.gyagoyle"
err := os.MkdirAll(c.HomeDir, 0777)
if err != nil {
log.Fatalf("Make a home directory is failed: %v", err)
}
return
}
func (c *Config) createHistDir() {
//history directory
c.HistDir = c.HomeDir + "/history"
err := os.MkdirAll(c.HistDir, 0777)
if err != nil {
log.Fatalf("Make a history directory is failed: %v", err)
}
return
}
func (c *Config) getGyazoId() {
filePath := c.HomeDir + c.IdFilename
if isExist(filePath) == false {
}
id, err := ioutil.ReadFile(filePath)
if err != nil {
//The ID is blank is no problem
return
}
c.GyazoId = string(id)
return
}
func isExist(filename string) bool {
_, err := os.Stat(filename)
return err == nil
}
func (c *Config) SetGyazoId(id string) {
if c.GyazoId == "" {
ioutil.WriteFile(c.HomeDir+c.IdFilename, []byte(id), 0644)
}
return
}
func (c *Config) GetToml(profile string) {
filePath := c.HomeDir + "/config.toml"
if isExist(filePath) == false {
log.Fatalf("config.toml not found:")
}
var t Toml
_, err := toml.DecodeFile(filePath, &t)
if err != nil {
log.Fatalf("config.toml read error:", err)
}
for _, v := range t.Profile {
if v.Name == profile {
c.BasicUser = v.BasicUser
c.BasicPassword = v.BasicPassword
c.Endpoint = v.Endpoint
}
}
}