/
utils.go
154 lines (139 loc) · 4.05 KB
/
utils.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
package cmd
import (
"fmt"
"io"
"os"
"os/exec"
"github.com/minepkg/minepkg/internals/commands"
"github.com/minepkg/minepkg/internals/globals"
"github.com/minepkg/minepkg/internals/instances"
"github.com/minepkg/minepkg/internals/mojang"
)
// MinepkgMapping is a server mapping (very unfinished)
type MinepkgMapping struct {
Platform string `json:"platform"`
Modpack string `json:"modpack"`
}
// HumanUint32 returns the number in a human readable format
func HumanUint32(num uint32) string {
switch {
case num >= 1000000000:
return fmt.Sprintf("%v B", num/1000000000)
case num >= 1000000:
return fmt.Sprintf("%v M", num/1000000)
case num >= 1000:
return fmt.Sprintf("%v K", num/1000)
}
return fmt.Sprintf("%v", num)
}
func ensureMojangAuth() (*mojang.AuthResponse, error) {
var loginData = &mojang.AuthResponse{}
credStore := globals.CredStore
mojangClient := globals.MojangClient
if credStore.MojangAuth == nil || credStore.MojangAuth.AccessToken == "" {
loginData = login()
if err := credStore.SetMojangAuth(loginData); err != nil {
return nil, err
}
return credStore.MojangAuth, nil
}
loginData, err := mojangClient.MojangEnsureToken(
credStore.MojangAuth.AccessToken,
credStore.MojangAuth.ClientToken,
)
if err != nil {
// TODO: check if expired or other problem!
logger.Info("Your token maybe expired. Please login again")
// TODO: error handling!
loginData = login()
}
// only update access token and client token
// because `SelectedProfile` is omited here
credStore.MojangAuth.AccessToken = loginData.AccessToken
credStore.MojangAuth.ClientToken = loginData.ClientToken
// HACK: maybe not pass credstore its own field
if err := credStore.SetMojangAuth(credStore.MojangAuth); err != nil {
return nil, err
}
return credStore.MojangAuth, nil
}
// CopyFile copies a file from src to dst. If src and dst files exist, and are
// the same, then return success. Otherwise, attempt to create a hard link
// between the two files. If that fail, copy the file contents from src to dst.
func CopyFile(src, dst string) (err error) {
sfi, err := os.Stat(src)
if err != nil {
return
}
if !sfi.Mode().IsRegular() {
// cannot copy non-regular files (e.g., directories,
// symlinks, devices, etc.)
return fmt.Errorf("CopyFile: non-regular source file %s (%q)", sfi.Name(), sfi.Mode().String())
}
dfi, err := os.Stat(dst)
if err != nil {
if !os.IsNotExist(err) {
return
}
} else {
if !(dfi.Mode().IsRegular()) {
return fmt.Errorf("CopyFile: non-regular destination file %s (%q)", dfi.Name(), dfi.Mode().String())
}
if os.SameFile(sfi, dfi) {
return
}
}
if err = os.Link(src, dst); err == nil {
return
}
err = copyFileContents(src, dst)
return
}
// copyFileContents copies the contents of the file named src to the file named
// by dst. The file will be created if it does not already exist. If the
// destination file exists, all it's contents will be replaced by the contents
// of the source file.
func copyFileContents(src, dst string) (err error) {
in, err := os.Open(src)
if err != nil {
return
}
defer in.Close()
out, err := os.Create(dst)
if err != nil {
return
}
defer func() {
cerr := out.Close()
if err == nil {
err = cerr
}
}()
if _, err = io.Copy(out, in); err != nil {
return
}
err = out.Sync()
return
}
func cmdTerminalOutput(b *exec.Cmd) {
b.Stderr = os.Stderr
b.Stdout = os.Stdout
}
func getJarFileForInstance(i *instances.Instance) (*instances.MatchedJar, error) {
jars, err := i.FindModJar()
if err != nil {
return nil, err
}
if i.Manifest.Dev.Jar != "" {
fmt.Printf("Searching according to your pattern \"%s\" to find jar file\n", i.Manifest.Dev.Jar)
}
if len(jars) > 1 && i.Manifest.Dev.Jar == "" {
text := fmt.Sprintf("Found multiple jar files. Using %s", jars[0].Name())
fmt.Println(commands.StyleWarnBox.Render(text))
fmt.Println(" Checkout https://preview.minepkg.io/docs/manifest#devjar if you want to use a different file.")
} else {
text := fmt.Sprintf("Using jar: %s", jars[0].Path())
fmt.Println(commands.StyleInfoBox.Render(text))
}
return &jars[0], nil
}