forked from apache/mynewt-newt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnewtvm.go
194 lines (162 loc) · 4.21 KB
/
newtvm.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
// newtvm is a Windows wrapper for the newt tool. It runs the specified
// commands within a Linux docker instance, giving access to newt and the
// utilities that it depends on.
package main
import (
"bufio"
"fmt"
"io"
"os"
"os/exec"
"regexp"
"strings"
)
const debug bool = false
const dockerMachineName string = "default"
const newtvmImage = "mynewt/mynewt"
const newtvmVersion = "0.0.3"
// Sets the necessary environment variables to allow docker to run.
func configEnv() error {
re, err := regexp.Compile("^SET ([^ ]+)=(.+)")
if err != nil {
return err
}
cmd := exec.Command("docker-machine", "env", "--shell", "cmd", "default")
outputBytes, err := cmd.CombinedOutput()
if err != nil {
return err
}
output := string(outputBytes[:])
lines := strings.Split(output, "\n")
for _, line := range lines {
matches := re.FindStringSubmatch(line)
if matches != nil {
if debug {
fmt.Fprintf(os.Stderr, "os.Setenv(\"%s\", \"%s\")\n",
matches[1], matches[2])
}
err = os.Setenv(matches[1], matches[2])
if err != nil {
return err
}
}
}
return nil
}
// Calculates a virtualbox-compatible representation of the present working
// directory.
//
// E.g.,
// C:\Users\me\Documents
//
// becomes:
// /c/Users/me/documents
func fixedPwd() (string, error) {
var pwd string
var err error
if pwd, err = os.Getwd(); err != nil {
return "", err
}
// Begin with a slash; convert drive letter to lowercase.
pwd = "/" + strings.ToLower(pwd[0:1]) + pwd[2:]
// Replace backslashes with slashes.
pwd = strings.Replace(pwd, "\\", "/", -1)
return pwd, nil
}
// Constructs a command that will run the specified shell tokens in the
// docker environment.
//
// E.g., the args parameter might be: { "echo", "'hello", "world'" }
func buildCmd(args []string) (*exec.Cmd, error) {
pwd, err := fixedPwd()
if err != nil {
return nil, err
}
fullArgs := []string{
"run", "--device=/dev/bus/usb", "--rm=true",
"-v", fmt.Sprintf("%s:/larva", pwd),
"-w", "/larva", fmt.Sprintf("%s:%s", newtvmImage, newtvmVersion),
"script", "-qc",
strings.Join(args, " "), "/dev/null"}
return exec.Command("docker", fullArgs...), nil
}
// Executes the specified command, displaying output as it is generated.
func execCmd(cmd *exec.Cmd) error {
// Reader / scanner pair for printing stdout output.
stdoutReader, err := cmd.StdoutPipe()
if err != nil {
return err
}
stdoutScanner := bufio.NewScanner(stdoutReader)
go func() {
for stdoutScanner.Scan() {
fmt.Println(stdoutScanner.Text())
}
}()
// Reader / scanner pair for printing stderr output.
stderrReader, err := cmd.StderrPipe()
if err != nil {
return err
}
stderrScanner := bufio.NewScanner(stderrReader)
go func() {
for stderrScanner.Scan() {
fmt.Fprintln(os.Stderr, stderrScanner.Text())
}
}()
// Execute command.
err = cmd.Start()
if err != nil {
return err
}
err = cmd.Wait()
if err != nil {
return err
}
return nil
}
func printUsage(w io.Writer) {
fmt.Fprintf(w, "usage: newtvm <command> [arg-1] [arg-2] [...]\n")
}
func usageErr(msg string, rc int) {
if msg != "" {
fmt.Fprintf(os.Stderr, "* error: %s\n", msg)
}
printUsage(os.Stderr)
os.Exit(rc)
}
func main() {
if len(os.Args) < 2 {
usageErr("", 1)
}
cmd, err := buildCmd(os.Args[1:])
if err != nil {
usageErr(err.Error(), 1)
}
err = configEnv()
if err != nil {
usageErr(err.Error(), 1)
}
err = execCmd(cmd)
if err != nil {
usageErr(err.Error(), 1)
}
}