-
Notifications
You must be signed in to change notification settings - Fork 8
/
forge.go
73 lines (58 loc) · 1.56 KB
/
forge.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
package server
import (
"fmt"
"github.com/nothub/mrpack-install/maven"
"github.com/nothub/mrpack-install/web"
"github.com/nothub/semver"
"log"
"os/exec"
"strings"
)
type ForgeInstaller struct {
MinecraftVersion string
ForgeVersion string
}
func (inst *ForgeInstaller) Install(serverDir string, serverFile string) error {
version := inst.ForgeVersion
if version == "" || version == "latest" {
meta, err := maven.FetchMetadata("https://maven.minecraftforge.net/net/minecraftforge/forge/maven-metadata.xml")
if err != nil {
return err
}
var versions []string
for _, ver := range meta.Versioning.Versions {
if !strings.HasPrefix(ver, inst.MinecraftVersion) {
// does not match mc version
continue
}
versions = append(versions, ver)
}
parsed, err := semver.ParseAll(versions)
if err != nil {
return err
}
sorted := semver.SortDesc(parsed)
if len(sorted) <= 0 {
return fmt.Errorf("no forge release version found for mc version %s", inst.MinecraftVersion)
}
version = sorted[0].String()
}
u := fmt.Sprintf(
"https://maven.minecraftforge.net/net/minecraftforge/forge/%s/forge-%s-installer.jar",
version,
version,
)
installerFile, err := web.DefaultClient.DownloadFile(u, ".", "")
if err != nil {
return err
}
cmd := exec.Command("java", "-jar", installerFile, "--installServer", serverDir)
log.Println("Executing command:", cmd.String())
if err = cmd.Run(); err != nil {
return err
}
if serverFile != "" {
log.Println("ignoring --server-file option for forge server installation!")
}
return nil
}