-
Notifications
You must be signed in to change notification settings - Fork 0
/
run.go
69 lines (55 loc) · 1.39 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
package main
import (
"fmt"
"os"
"os/exec"
"strings"
"github.com/code-game-project/go-utils/cgfile"
"github.com/code-game-project/go-utils/external"
"github.com/code-game-project/go-utils/modules"
cgExec "github.com/code-game-project/go-utils/exec"
)
func Run() error {
config, err := cgfile.LoadCodeGameFile("")
if err != nil {
return err
}
data, err := modules.ReadCommandConfig[modules.RunData]()
if err != nil {
return err
}
url := external.TrimURL(config.URL)
switch config.Type {
case "client":
return runClient(url, data.Args)
case "server":
return runServer(data.Args)
default:
return fmt.Errorf("Unknown project type: %s", config.Type)
}
}
func runClient(url string, args []string) error {
cmdArgs := []string{"run", "."}
cmdArgs = append(cmdArgs, args...)
env := []string{"CG_GAME_URL=" + url}
env = append(env, os.Environ()...)
if _, err := exec.LookPath("go"); err != nil {
return fmt.Errorf("'go' ist not installed!")
}
cmd := exec.Command("go", 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 go %s'", url, strings.Join(cmdArgs, " "))
}
return nil
}
func runServer(args []string) error {
cmdArgs := []string{"run", "."}
cmdArgs = append(cmdArgs, args...)
_, err := cgExec.Execute(false, "go", cmdArgs...)
return err
}