-
Notifications
You must be signed in to change notification settings - Fork 11
/
restart.go
68 lines (52 loc) · 1.64 KB
/
restart.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
package main
import (
"fmt"
"log"
"os"
"os/exec"
"strings"
)
// restartPackage will rebuild the given package and restart the process
func restartPackage(packagePath string, arguments []string) {
fmt.Println("")
log.Println("BUILDING\n----------------------------")
// build and install the package
builder := exec.Command("go", "install", packagePath)
// redirect all output to the standard console
builder.Stdout = os.Stdout
builder.Stderr = os.Stderr
// run the build command and wait for it to exit
err := builder.Run()
if err != nil {
fmt.Fprintln(os.Stderr, "ERROR: could not build the package", err)
return
}
// get the package name to run the package
executable := getExecutableName(packagePath)
// stop all previous running instances of the project
kill := exec.Command("pkill", "-x", executable)
// redirect all output to the standard console
kill.Stdout = os.Stdout
kill.Stderr = os.Stderr
// note: ignore errors from the kill command
// (will occur if no executable is running)
_ = kill.Run()
// start the go executable
runner := exec.Command(executable, arguments...)
// set the current directory to the packagePath
os.Chdir("/go/src/" + packagePath)
// redirect all output to the standard console
runner.Stdout = os.Stdout
runner.Stderr = os.Stderr
// run the program
err = runner.Start()
if err != nil {
fmt.Fprintln(os.Stderr, "ERROR: could not run the package", err)
}
}
// getExecutableName will get the last part of the package path that is used
// to name the go executable
func getExecutableName(packagePath string) string {
parts := strings.Split(packagePath, "/")
return parts[len(parts)-1]
}