-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.go
175 lines (149 loc) · 4.69 KB
/
util.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
package main
import (
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"os/exec"
"path/filepath"
"github.com/magiconair/properties"
"github.com/pkg/errors"
jww "github.com/spf13/jwalterweatherman"
)
const FORGE_DL_URL = "https://files.minecraftforge.net/maven/net/minecraftforge/forge/"
// fileExists checks if a file exists and is not a directory before we
// try using it to prevent further errors.
func fileExists(filename string) bool {
info, err := os.Stat(filename)
if os.IsNotExist(err) {
return false
}
return !info.IsDir()
}
func folderExists(folder string) bool {
info, err := os.Stat(folder)
if os.IsNotExist(err) {
return false
}
return info.IsDir()
}
func installForgeServer(forgeVersion string, path string) error {
jww.INFO.Println("Downloading Forge Server")
installerJar := "forge-" + forgeVersion + "-installer.jar"
installerJarPath := filepath.Join(path, "installer.jar")
if fileExists(installerJarPath) {
err := os.Remove(installerJarPath)
if err != nil {
return errors.Wrap(err, "Failed removing existing Forge installer")
}
}
forgeFile, err := os.Create(installerJarPath)
if err != nil {
return errors.Wrap(err, "Failed creating Forge installer")
}
defer os.Remove(forgeFile.Name())
defer forgeFile.Close()
forgeResp, err := http.Get(FORGE_DL_URL + forgeVersion + "/" + installerJar)
if err != nil {
return errors.Wrap(err, "Failed downloading Forge")
}
defer forgeResp.Body.Close()
_, err = io.Copy(forgeFile, forgeResp.Body)
if err != nil {
return errors.Wrap(err, "Failed writing Forge to disk")
}
javaPath, err := exec.LookPath("java")
if err != nil {
return errors.Wrap(err, "Failed finding Java")
}
cmdInstall := &exec.Cmd{
Path: javaPath,
Args: []string{javaPath, "-jar", installerJarPath, "--installServer", path},
Stdout: jww.DEBUG.Writer(),
Stderr: jww.ERROR.Writer(),
}
jww.INFO.Println("Installing Forge Server")
err = cmdInstall.Run()
if err != nil {
return errors.Wrap(err, "Failed installing Forge")
}
src := filepath.Join(path, "forge-"+forgeVersion+".jar")
dest := filepath.Join(path, "server.jar")
return errors.Wrap(os.Rename(src, dest), "Failed renaming server jar")
}
func precreateUserDir(path string) error {
err := os.MkdirAll(filepath.Join(path, "mods"), 0700)
if err != nil {
return errors.Wrap(err, "Failed creating mods directory")
}
err = os.MkdirAll(filepath.Join(path, "config"), 0700)
if err != nil {
return errors.Wrap(err, "Failed creating config directory")
}
file, err := os.Create(filepath.Join(path, "README.txt"))
if err != nil {
return errors.Wrap(err, "Failed creating README")
}
defer file.Close()
_, err = file.WriteString("The contents of this folder should mirror the structure of the pack\n")
if err != nil {
return errors.Wrap(err, "Failed writting to README")
}
_, err = file.WriteString("Any user provided files will be copied from here into the pack\n")
if err != nil {
return errors.Wrap(err, "Failed writting to README")
}
return errors.Wrap(file.Sync(), "Failed flushing README to disk")
}
func writeVersionFile(version string, path string) error {
ver := CPVersion{
Version: version,
}
bytes, err := ver.Marshal()
if err != nil {
return errors.Wrap(err, "Failed to marshal version file")
}
return errors.Wrap(ioutil.WriteFile(path, bytes, 0700), "Failed to write version file")
}
// compareVersion will return true if versions match, false if otherwise
func compareVersion(version string, path string) (bool, error) {
if !fileExists(path) {
return false, nil
}
bytes, err := ioutil.ReadFile(path)
if err != nil {
return false, errors.Wrap(err, "Failed to open version file")
}
ver, err := UnmarshalCPVersion(bytes)
if err != nil {
return false, errors.Wrap(err, "Failed to parse version file")
}
return ver.Version == version, nil
}
func updateServerPropsVersion(version string, path string) error {
if fileExists(path) {
bytes, err := ioutil.ReadFile(path)
if err != nil {
return errors.Wrap(err, "Failed to read server properties")
}
props, err := properties.Load(bytes, properties.UTF8)
if err != nil {
return errors.Wrap(err, "Failed to parse server properties")
}
props.SetValue("motd", fmt.Sprintf("Version %s", version))
if err != nil {
return errors.Wrap(err, "Failed to set MOTD")
}
file, err := os.Create(path)
if err != nil {
return errors.Wrap(err, "Failed to open server properties")
}
defer file.Close()
_, err = props.Write(file, properties.UTF8)
if err != nil {
return errors.Wrap(err, "Failed to write server properties")
}
}
return nil
}