forked from Azure/azure-sdk-for-go
-
Notifications
You must be signed in to change notification settings - Fork 1
/
work.go
90 lines (82 loc) · 2.38 KB
/
work.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
// Copyright 2018 Microsoft Corporation
//
// Licensed 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.
package cmd
import (
"bufio"
"fmt"
"os"
"os/exec"
"path/filepath"
"strings"
"time"
)
const (
autorestArgsPattern = "--use=@microsoft.azure/autorest.go@~2.1.99 %s --go --multiapi --go-sdk-folder=%s --use-onever"
)
type work struct {
filename string
sdkFolder string
}
func autorestCommand(file string, sdk string) *exec.Cmd {
autorestArgs := fmt.Sprintf(autorestArgsPattern, file, sdk)
c := exec.Command("autorest", strings.Split(autorestArgs, " ")...)
return c
}
func worker(id int, jobs <-chan work, results chan<- error) {
for work := range jobs {
start := time.Now()
c := autorestCommand(work.filename, work.sdkFolder)
vprintf("worker %d is starting on file %s\nparameters: %v\n", id, work.filename, c.Args)
output, err := c.CombinedOutput()
if err == nil {
vprintf("worker %d has done with file %s (%v)\n", id, work.filename, time.Since(start))
} else {
printf("worker %d fails with file %s (%v), error messages:\n%v\n", id, work.filename, time.Since(start), string(output))
}
results <- err
}
}
func startCmd(c *exec.Cmd) error {
stdout, err := c.StdoutPipe()
if err != nil {
return fmt.Errorf("failed to get stdout pipe: %v", err)
}
scanner := bufio.NewScanner(stdout)
go func() {
for scanner.Scan() {
printf("> %s\n", scanner.Text())
}
}()
stderr, err := c.StderrPipe()
if err != nil {
return fmt.Errorf("failed to get stderr pipe: %v", err)
}
scanner = bufio.NewScanner(stderr)
go func() {
for scanner.Scan() {
printf("> %s\n", scanner.Text())
}
}()
return c.Start()
}
func selectFilesWithName(path string, name string) ([]string, error) {
var files []string
err := filepath.Walk(path, func(p string, info os.FileInfo, err error) error {
if !info.IsDir() && info.Name() == name {
files = append(files, p)
}
return nil
})
return files, err
}