forked from canthefason/go-watcher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.go
138 lines (110 loc) · 2.61 KB
/
common.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
package watcher
import (
"fmt"
"io"
"log"
"math/rand"
"os"
"os/exec"
"strings"
"time"
)
// Binary name used for built package
const binaryName = "watcher"
type Params struct {
// Package parameters
Package []string
// Go-Watcher parameters
System map[string]string
}
// NewParams creates a new instance of Params and returns the pointer
func NewParams() *Params {
return &Params{
Package: make([]string, 0),
System: make(map[string]string),
}
}
// Get returns the watcher parameter with given name
func (p *Params) Get(name string) string {
return p.System[name]
}
// CloneRun copies run parameter value to watch parameter in-case watch
// parameter does not exist
func (p *Params) CloneRun() {
if p.System["watch"] == "" && p.System["run"] != "" {
p.System["watch"] = p.System["run"]
}
}
func (p *Params) GetPackage() string {
run := p.Get("run")
if run != "" {
return run
}
return "."
}
// GetBinaryName prepares binary name with GOPATH if it is set
func (p *Params) createBinaryName() string {
rand.Seed(time.Now().UnixNano())
randName := rand.Int31n(999999)
packageName := strings.Replace(p.GetPackage(), "/", "-", -1)
return fmt.Sprintf("%s-%s-%d", getBinaryNameRoot(), packageName, randName)
}
func getBinaryNameRoot() string {
path := os.Getenv("GOPATH")
if path != "" {
return fmt.Sprintf("%s/bin/%s", path, binaryName)
}
return path
}
// runCommand runs the command with given name and arguments. It copies the
// logs to standard output
func runCommand(name string, args ...string) (*exec.Cmd, error) {
cmd := exec.Command(name, args...)
stderr, err := cmd.StderrPipe()
if err != nil {
return cmd, err
}
stdout, err := cmd.StdoutPipe()
if err != nil {
return cmd, err
}
if err := cmd.Start(); err != nil {
return cmd, err
}
go io.Copy(os.Stdout, stdout)
go io.Copy(os.Stderr, stderr)
return cmd, nil
}
// PrepareArgs filters the system parameters from package parameters
// and returns Params instance
func PrepareArgs(args []string) *Params {
params := NewParams()
// remove command
args = args[1:len(args)]
for i := 0; i < len(args); i++ {
arg := args[i]
arg = stripDash(arg)
if arg == "run" || arg == "watch" {
if len(args) <= i+1 {
log.Fatalf("missing parameter value: %s", arg)
}
params.System[arg] = args[i+1]
i++
continue
}
params.Package = append(params.Package, args[i])
}
params.CloneRun()
return params
}
// stripDash removes the dash chars and returns parameter name
func stripDash(arg string) string {
if len(arg) > 1 {
if arg[1] == '-' {
return arg[2:]
} else if arg[0] == '-' {
return arg[1:]
}
}
return arg
}