File: autorest.go

package info (click to toggle)
golang-github-azure-azure-sdk-for-go 68.0.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 556,256 kB
  • sloc: javascript: 196; sh: 96; makefile: 7
file content (144 lines) | stat: -rw-r--r-- 3,676 bytes parent folder | download | duplicates (3)
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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

package autorest

import (
	"fmt"
	"io"
	"os/exec"
	"strings"

	"github.com/Azure/azure-sdk-for-go/eng/tools/generator/autorest/model"
)

// Generator collects all the related context of an autorest generation
type Generator struct {
	options model.Options
	cmd     *exec.Cmd
}

// NewGeneratorFromOptions returns a new Generator with the given model.Options
func NewGeneratorFromOptions(o model.Options) *Generator {
	return &Generator{
		options: o,
	}
}

// WithOption appends an model.Option to the argument list of the autorest generation
func (g *Generator) WithOption(option model.Option) *Generator {
	g.options = g.options.MergeOptions(option)
	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{
			Options: g.options,
			Message: string(o),
		}
	}
	return nil
}

func (g *Generator) buildCommand() {
	if g.cmd != nil {
		return
	}
	arguments := make([]string, len(g.options.Arguments()))
	for i, o := range g.options.Arguments() {
		arguments[i] = o.Format()
	}
	g.cmd = exec.Command("autorest", arguments...)
}

// Arguments returns the arguments which are using in the autorest command ('autorest' itself excluded)
func (g *Generator) Arguments() []model.Option {
	return g.options.Arguments()
}

// Start starts the generation
func (g *Generator) Start() error {
	g.buildCommand()
	if err := g.cmd.Start(); err != nil {
		return &GenerateError{
			Options: g.options,
			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{
			Options: g.options,
			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{
			Options: g.options,
			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 {
	Options model.Options
	Message string
}

// Error ...
func (e *GenerateError) Error() string {
	arguments := make([]string, len(e.Options.Arguments()))
	for i, o := range e.Options.Arguments() {
		arguments[i] = o.Format()
	}
	return fmt.Sprintf("autorest error with arguments '%s': \n%s", strings.Join(arguments, ", "), e.Message)
}