-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
238 lines (205 loc) · 4.59 KB
/
main.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
package main
import (
"encoding/json"
"flag"
"fmt"
"io"
"log"
"net/http"
"os"
"path/filepath"
"runtime"
"strings"
"github.com/BurntSushi/toml"
"github.com/mitchellh/go-homedir"
)
var minecraftPath string
func init() {
switch runtime.GOOS {
case "windows":
path := os.Getenv("APPDATA")
minecraftPath = filepath.Join(path, ".minecraft")
default:
path, err := homedir.Dir()
if err != nil {
panic(err)
}
minecraftPath = filepath.Join(path, ".minecraft")
}
}
func usage() {
os.Stderr.WriteString(`
Usage: mcm [OPTION]... RECIPE
Options:
--help show this help message
--version print the version
`[1:])
}
func version() {
os.Stderr.WriteString(`
mcm: v0.4.0
`[1:])
}
func exists(path string) bool {
_, err := os.Stat(path)
return err == nil
}
type Profile struct {
Profiles map[string]struct {
GameDir string
LastVersionId string
}
}
func (p *Profile) Valid(name string) bool {
_, ok := p.Profiles[name]
return ok
}
type Package struct {
Name string `toml:"name"`
URL string `toml:"url"`
}
type Manager struct {
log *log.Logger
prof *Profile
errors []string
root string
Name string `toml:"name"`
Mods []Package `toml:"mod"`
ResourcePacks []Package `toml:"resourcepack"`
}
func NewManager(w io.Writer) *Manager {
return &Manager{
log: log.New(w, "", log.LstdFlags),
}
}
func (m *Manager) InfoLog(a ...string) {
m.log.Println("INFO:", strings.Join(a, " "))
}
func (m *Manager) ErrorLog(err error, a ...string) {
m.log.Println("ERRO:", strings.Join(a, " "))
m.errors = append(m.errors, err.Error())
}
func (m *Manager) FatalLog(a ...string) {
m.log.Println("FATA:", strings.Join(a, " "))
}
func (m *Manager) LoadProfile() error {
path := filepath.Join(minecraftPath, "launcher_profiles.json")
m.InfoLog("Load profile:", path)
f, err := os.Open(path)
if err != nil {
m.FatalLog("Failed open profile:", path)
return err
}
defer f.Close()
m.prof = &Profile{}
if err = json.NewDecoder(f).Decode(m.prof); err != nil {
m.FatalLog("Failed read profile:", path)
return err
}
return nil
}
func (m *Manager) LoadRecipe(path string) error {
fullPath, err := filepath.Abs(path)
if err != nil {
m.FatalLog("Invalid path:", err.Error())
}
m.InfoLog("Load recipe:", fullPath)
_, err = toml.DecodeFile(fullPath, m)
if err != nil {
m.FatalLog("Failed load recipe")
return err
}
if m.Name != "" && !m.prof.Valid(m.Name) {
m.FatalLog("Failed find the version name:", m.Name)
return fmt.Errorf("invalid version name: %s", m.Name)
}
if m.Name == "" || m.prof.Profiles[m.Name].GameDir == "" {
m.root = minecraftPath
return nil
}
m.root = m.prof.Profiles[m.Name].GameDir
return nil
}
func (m *Manager) Download(kind string, a []Package) error {
rootPath := filepath.Join(m.root, kind)
m.InfoLog("Start install "+kind+" to:", rootPath)
if !exists(rootPath) {
m.InfoLog("Create " + kind + " directory")
if err := os.MkdirAll(rootPath, 0755); err != nil {
m.FatalLog("Failed create " + kind + " directory")
return err
}
}
for _, p := range a {
path := filepath.Join(rootPath, p.Name)
if exists(path) {
m.InfoLog("Already installed:", p.Name)
continue
}
m.InfoLog("Start install:", p.Name)
f, err := os.Create(path)
if err != nil {
m.ErrorLog(err, "Failed create file:", path)
continue
}
m.InfoLog("Download from:", p.URL)
res, err := http.Get(p.URL)
if err != nil {
m.ErrorLog(err, "Failed download:", p.URL)
continue
}
defer res.Body.Close()
m.InfoLog("Install to:", path)
_, err = io.Copy(f, res.Body)
if err != nil {
m.ErrorLog(err, "Failed write to:", path)
continue
}
}
return nil
}
func (m *Manager) Execute(recipePath string) error {
m.InfoLog("Start mcm")
if err := m.LoadProfile(); err != nil {
return err
}
if err := m.LoadRecipe(recipePath); err != nil {
return err
}
if err := m.Download("mods", m.Mods); err != nil {
return err
}
if err := m.Download("resourcepacks", m.ResourcePacks); err != nil {
return err
}
if len(m.errors) > 0 {
return fmt.Errorf("%d errors occurred:\n%s",
len(m.errors), strings.Join(m.errors, "\n"))
}
return nil
}
func main() {
isHelp := flag.Bool("help", false, "")
isVersion := flag.Bool("version", false, "")
flag.Usage = usage
flag.Parse()
if *isHelp {
usage()
os.Exit(2)
}
if *isVersion {
version()
os.Exit(2)
}
if flag.NArg() < 1 {
fmt.Fprintln(os.Stderr, "mcm:", "no input file")
usage()
os.Exit(1)
}
recipe := flag.Arg(0)
m := NewManager(os.Stdout)
if err := m.Execute(recipe); err != nil {
fmt.Fprintln(os.Stderr, "mcm:", err)
os.Exit(1)
}
}