-
Notifications
You must be signed in to change notification settings - Fork 803
/
autorest.go
127 lines (109 loc) · 3.01 KB
/
autorest.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
package autorest
import (
"fmt"
"io"
"os/exec"
"github.com/Azure/azure-sdk-for-go/tools/generator/autorest/model"
)
// Generator collects all the related context of an autorest generation
type Generator struct {
arguments []string
cmd *exec.Cmd
}
// NewGeneratorFromOptions returns a new Generator with the given model.Options
func NewGeneratorFromOptions(o model.Options) *Generator {
return &Generator{
arguments: o.Arguments(),
}
}
// WithOption appends an model.Option to the argument list of the autorest generation
func (g *Generator) WithOption(option model.Option) *Generator {
g.arguments = append(g.arguments, option.Format())
return g
}
// WithTag appends a tag option to the autorest argument list
func (g *Generator) WithTag(tag string) *Generator {
return g.WithOption(model.NewKeyValueOption("tag", tag))
}
// WithMultiAPI appends a multiapi flag to the autorest argument list
func (g *Generator) WithMultiAPI() *Generator {
return g.WithOption(model.NewFlagOption("multiapi"))
}
// WithMetadataOutput appends a `metadata-output-folder` option to the autorest argument list
func (g *Generator) WithMetadataOutput(output string) *Generator {
return g.WithOption(model.NewKeyValueOption("metadata-output-folder", output))
}
// WithReadme appends a readme argument
func (g *Generator) WithReadme(readme string) *Generator {
return g.WithOption(model.NewArgument(readme))
}
// Generate executes the autorest generation. The error will be of type *GenerateError
func (g *Generator) Generate() error {
g.buildCommand()
o, err := g.cmd.CombinedOutput()
if err != nil {
return &GenerateError{
Arguments: g.arguments,
Message: string(o),
}
}
return nil
}
func (g *Generator) buildCommand() {
if g.cmd != nil {
return
}
g.cmd = exec.Command("autorest", g.arguments...)
}
// Start starts the generation
func (g *Generator) Start() error {
g.buildCommand()
if err := g.cmd.Start(); err != nil {
return &GenerateError{
Arguments: g.arguments,
Message: err.Error(),
}
}
return nil
}
// Wait waits for the generation to complete
func (g *Generator) Wait() error {
g.buildCommand()
if err := g.cmd.Wait(); err != nil {
return &GenerateError{
Arguments: g.arguments,
Message: err.Error(),
}
}
return nil
}
// Run starts and waits the generation
func (g *Generator) Run() error {
g.buildCommand()
if err := g.cmd.Run(); err != nil {
return &GenerateError{
Arguments: g.arguments,
Message: err.Error(),
}
}
return nil
}
// StdoutPipe returns the stdout pipeline of the command
func (g *Generator) StdoutPipe() (io.ReadCloser, error) {
g.buildCommand()
return g.cmd.StdoutPipe()
}
// StderrPipe returns the stderr pipeline of the command
func (g *Generator) StderrPipe() (io.ReadCloser, error) {
g.buildCommand()
return g.cmd.StderrPipe()
}
// GenerateError ...
type GenerateError struct {
Arguments []string
Message string
}
// Error ...
func (e *GenerateError) Error() string {
return fmt.Sprintf("autorest error with arguments '%s': \n%s", e.Arguments, e.Message)
}