Skip to content

Commit 3d84592

Browse files
committed
update
1 parent e2e11c0 commit 3d84592

File tree

3 files changed

+17
-11
lines changed

3 files changed

+17
-11
lines changed

tools/cli/internal/cli/merge/merge.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ func (o *Opts) PreRunE(_ []string) error {
7575
return fmt.Errorf("output file must be either a JSON or YAML file, got %s", o.outputPath)
7676
}
7777

78-
if o.format != "json" && o.format != "yaml" {
79-
return fmt.Errorf("output format must be either 'json' or 'yaml', got %s", o.format)
78+
if err := openapi.ValidateFormat(o.format); err != nil {
79+
return err
8080
}
8181

8282
m, err := openapi.NewOasDiff(o.basePath, o.excludePrivatePaths)

tools/cli/internal/cli/split/split.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -103,12 +103,8 @@ func (o *Opts) PreRunE(_ []string) error {
103103
return fmt.Errorf("output file must be either a JSON or YAML file, got %s", o.outputPath)
104104
}
105105

106-
if o.format != openapi.JSON && o.format != openapi.YAML {
107-
return fmt.Errorf("output format must be either 'json' or 'yaml', got %s", o.format)
108-
}
109-
110-
if strings.Contains(o.basePath, openapi.DotYAML) {
111-
o.format = openapi.YAML
106+
if err := openapi.ValidateFormat(o.format); err != nil {
107+
return err
112108
}
113109

114110
return nil
@@ -136,7 +132,7 @@ func Builder() *cobra.Command {
136132
cmd.Flags().StringVarP(&opts.basePath, flag.Spec, flag.SpecShort, "-", usage.Spec)
137133
cmd.Flags().StringVar(&opts.env, flag.Environment, "", usage.Environment)
138134
cmd.Flags().StringVarP(&opts.outputPath, flag.Output, flag.OutputShort, "", usage.Output)
139-
cmd.Flags().StringVarP(&opts.format, flag.Format, flag.FormatShort, openapi.JSON, usage.Format)
135+
cmd.Flags().StringVarP(&opts.format, flag.Format, flag.FormatShort, openapi.ALL, usage.Format)
140136
cmd.Flags().StringVar(&opts.gitSha, flag.GitSha, "", usage.GitSha)
141137

142138
_ = cmd.MarkFlagRequired(flag.Output)

tools/cli/internal/openapi/file.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
const (
3030
JSON = "json"
3131
YAML = "yaml"
32+
ALL = "all"
3233
DotYAML = ".yaml"
3334
DotJSON = ".json"
3435
)
@@ -41,15 +42,15 @@ func SaveToFile[T any](path, format string, content T, fs afero.Fs) error {
4142
return err
4243
}
4344

44-
if format == JSON || format == "" || format == "all" {
45+
if format == JSON || format == "" || format == ALL {
4546
jsonPath := newPathWithExtension(path, JSON)
4647
if errJSON := afero.WriteFile(fs, jsonPath, data, 0o600); errJSON != nil {
4748
return errJSON
4849
}
4950
log.Printf("\nFile was saved in '%s'.\n\n", jsonPath)
5051
}
5152

52-
if format == YAML || format == "" || format == "all" {
53+
if format == YAML || format == "" || format == ALL {
5354
dataYAML, err := SerializeToYAML(data)
5455
if err != nil {
5556
return err
@@ -123,3 +124,12 @@ func SerializeToYAML(data []byte) ([]byte, error) {
123124
func Save(path string, oas *openapi3.T, format string, fs afero.Fs) error {
124125
return SaveToFile(path, format, newSpec(oas), fs)
125126
}
127+
128+
// ValidateFormat validates the format of files supported.
129+
func ValidateFormat(format string) error {
130+
if format != JSON && format != YAML && format != ALL {
131+
return fmt.Errorf("format must be either 'json', 'yaml' or 'all', got %s", format)
132+
}
133+
134+
return nil
135+
}

0 commit comments

Comments
 (0)