-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.go
77 lines (63 loc) · 1.77 KB
/
run.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
package main
import (
"errors"
"fmt"
"os"
"os/exec"
"strings"
"github.com/code-game-project/go-utils/cgfile"
cgExec "github.com/code-game-project/go-utils/exec"
"github.com/code-game-project/go-utils/external"
"github.com/code-game-project/go-utils/modules"
)
func Run() error {
config, err := cgfile.LoadCodeGameFile("")
if err != nil {
return err
}
packageConf, ok := config.LangConfig["package"]
if !ok {
return errors.New("Missing language config field `package` in .codegame.json!")
}
packageName := packageConf.(string)
if packageConf == "" {
return errors.New("Empty language config field `package` in .codegame.json!")
}
data, err := modules.ReadCommandConfig[modules.RunData]()
if err != nil {
return err
}
url := external.TrimURL(config.URL)
switch config.Type {
case "client":
return runClient(url, packageName, data.Args)
default:
return fmt.Errorf("Unknown project type: %s", config.Type)
}
}
func runClient(url, packageName string, args []string) error {
_, err := cgExec.Execute(true, "mvn", "-q", "-B", "clean", "compile")
if err != nil {
return err
}
for i, a := range args {
a = strings.ReplaceAll(a, "'", "\\'")
args[i] = "'" + a + "'"
}
cmdArgs := []string{"-q", "-B", "-e", "exec:java", "-Dexec.mainClass=" + packageName + ".App", "-Dexec.args=" + strings.Join(args, " ")}
env := []string{"CG_GAME_URL=" + url}
env = append(env, os.Environ()...)
if _, err := exec.LookPath("mvn"); err != nil {
return fmt.Errorf("`mvn` (Maven) ist not installed!")
}
cmd := exec.Command("mvn", cmdArgs...)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
cmd.Env = env
err = cmd.Run()
if err != nil {
return fmt.Errorf("Failed to run 'CG_GAME_URL=%s mvn %s'", url, strings.Join(cmdArgs, " "))
}
return nil
}