-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig.go
168 lines (153 loc) · 3.03 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
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
// See LICENSE.txt for licensing information.
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"time"
)
const (
CFG_VER_1 = iota + 1
)
const CFG_VER = CFG_VER_1
type Config struct {
Root string // absolute path for game directories
Games []*Game // slice of games
Version int `json:Version,omitempty` // config format version
}
func (c *Config) PrintWhole() {
if len(c.Games) < 1 {
fmt.Println("No games defined")
return
}
if flagVerbose {
fmt.Printf("Config version: %d\n\n", c.Version)
}
fmt.Printf("%3s ", "#")
c.Games[0].PrintHeader()
for i, g := range c.Games {
fmt.Printf("%3d ", i+1)
g.Print()
}
fmt.Println()
}
func (c *Config) GetGame(name string) *Game {
for _, g := range c.Games {
if g.Name == name {
return g
}
}
return nil
}
func (c *Config) AddGame(name, path string) (gm *Game, err error) {
r := filepath.Join(c.Root, name)
if _, err = os.Stat(r); os.IsExist(err) {
return
}
if _, err = os.Stat(path); os.IsNotExist(err) {
return
}
if err = os.MkdirAll(r, 0777); err != nil {
return
}
gm = &Game{
Name: name,
Path: path,
Root: r,
Stamp: time.Now(),
Saves: make([]*Save, 0),
}
c.Games = append(c.Games, gm)
return gm, nil
}
func (c *Config) DelGame(name string) error {
var i int
var g *Game
for i, g = range c.Games {
if g.Name == name {
break
}
}
if g.Name != name {
return fmt.Errorf(`Couldn't find game "%s"`, name)
}
if flagVerbose {
fmt.Fprintln(os.Stderr, "removing all from:", g.Root)
}
if err := os.RemoveAll(g.Root); err != nil {
return err
}
copy(c.Games[i:], c.Games[i+1:])
c.Games[len(c.Games)-1] = nil
c.Games = c.Games[:len(c.Games)-1]
return nil
}
func (c *Config) CheckVer(min_ver int, print bool) bool {
if c.Version >= min_ver {
return true
}
if print {
fmt.Fprintf(os.Stderr, "using old config format, please [migrate] (%d changes(s) behind)\n", CFG_VER-c.Version)
}
return false
}
func loadConfig(path string) (*Config, error) {
if flagVerbose {
fmt.Fprintln(os.Stderr, "loading config from", path)
}
f, err := os.Open(path)
if err != nil {
return nil, err
}
defer f.Close()
d, err := ioutil.ReadAll(f)
if err != nil {
return nil, err
}
var c *Config
if err := json.Unmarshal(d, &c); err != nil {
return nil, err
}
c.CheckVer(CFG_VER, flagVerbose)
return c, nil
}
func (c *Config) Save(path string) error {
if flagVerbose {
fmt.Fprintln(os.Stderr, "saving config to", path)
}
f, err := os.Create(path)
if err != nil {
return err
}
defer f.Close()
j := json.NewEncoder(f)
return j.Encode(c)
}
func (c *Config) Migrate() error {
if c.Version < CFG_VER_1 {
spinner.Msg("Migrating -> 1")
for _, g := range c.Games {
spinner.Tick()
for _, s := range g.Saves {
if s.Size > 0 {
spinner.Tick()
continue
}
i, err := os.Stat(s.Path)
if err != nil {
return err
}
spinner.Tick()
size := uint64(i.Size())
s.Size = size
g.Size += size
}
}
c.Version = CFG_VER_1
spinner.Finish()
}
fmt.Println("All done")
return nil
}