/
generate.go
160 lines (140 loc) · 3.7 KB
/
generate.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
// Copyright (c) 2017 Opsidian Ltd.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package openapi
import (
"encoding/json"
"errors"
"fmt"
"io"
"os"
"path"
"runtime"
"github.com/spf13/cobra"
"gopkg.in/yaml.v2"
"github.com/conflowio/conflow/examples/common"
"github.com/conflowio/conflow/src/conflow"
"github.com/conflowio/conflow/src/conflow/job"
"github.com/conflowio/conflow/src/loggers/zerolog"
"github.com/conflowio/conflow/src/openapi"
"github.com/conflowio/conflow/src/parsers"
)
func generateCommand() *cobra.Command {
var format string
var compact bool
var stdin bool
cmd := &cobra.Command{
Use: "generate",
Short: "Generates OpenAPI definition files",
Example: " To read from stdin, use '-' as argument",
Args: cobra.MaximumNArgs(1),
PreRunE: func(cmd *cobra.Command, args []string) error {
if format != "json" && format != "yaml" {
return fmt.Errorf("invalid value for 'format': must be json or yaml")
}
if stdin && len(args) > 0 {
return fmt.Errorf("no arguments are allowed when --stdin is used")
}
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
parseCtx := common.NewParseContext()
p := parsers.NewMain("main", openapi.OpenAPIInterpreter{})
if stdin {
var in []byte
var readErr error
finished := make(chan struct{})
go func() {
in, readErr = io.ReadAll(os.Stdin)
finished <- struct{}{}
}()
select {
case <-finished:
if readErr != nil {
return readErr
}
if err := p.ParseText(parseCtx, string(in)); err != nil {
return err
}
case <-cmd.Context().Done():
return errors.New("interrupt received")
}
} else {
target, err := os.Getwd()
if err != nil {
return fmt.Errorf("couldn't determine current working directory: %w", err)
}
if len(args) > 0 {
if path.IsAbs(args[0]) {
target = args[0]
} else {
target = path.Join(target, args[0])
}
}
target = path.Clean(target)
fileInfo, err := os.Lstat(target)
if err != nil {
return err
}
if fileInfo.IsDir() {
if err := p.ParseDir(parseCtx, target); err != nil {
return err
}
} else {
if err := p.ParseFile(parseCtx, target); err != nil {
return err
}
}
}
logger := zerolog.NewDisabledLogger()
scheduler := job.NewScheduler(logger, runtime.NumCPU()*2, 100)
scheduler.Start()
defer scheduler.Stop()
res, err := conflow.Evaluate(
parseCtx,
cmd.Context(),
nil,
logger,
scheduler,
"main",
nil,
)
if err != nil {
return err
}
switch format {
case "json":
encoder := json.NewEncoder(cmd.OutOrStdout())
if !compact {
encoder.SetIndent("", " ")
}
return encoder.Encode(res)
case "yaml":
jsonData, err := json.Marshal(res)
if err != nil {
return err
}
m := map[string]interface{}{}
if err := json.Unmarshal(jsonData, &m); err != nil {
return err
}
encoder := yaml.NewEncoder(cmd.OutOrStdout())
defer func() {
if err := encoder.Close(); err != nil {
_, _ = fmt.Fprintf(cmd.ErrOrStderr(), "Error: %s", err.Error())
os.Exit(1)
}
}()
return encoder.Encode(m)
default:
panic(fmt.Errorf("unexpected format: %s", format))
}
},
}
cmd.Flags().StringVar(&format, "format", "json", "Output format: json, or yaml")
cmd.Flags().BoolVar(&compact, "compact", false, "Whether to compact output (applies to json only)")
cmd.Flags().BoolVar(&stdin, "stdin", false, "Whether to read from stdin")
return cmd
}