2023-12-31 01:13:31 +00:00
|
|
|
// Copyright (C) 2023 The Licensing Authors
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
2023-12-29 22:15:52 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"dario.cat/mergo"
|
|
|
|
)
|
|
|
|
|
|
|
|
func newCLI(name, usage string) *CLI {
|
|
|
|
cli := &CLI{}
|
|
|
|
cli.FlagSet = flag.NewFlagSet(name, flag.ExitOnError)
|
|
|
|
|
|
|
|
cli.FlagSet.Usage = func() {
|
|
|
|
cli.Printf("Usage:\n")
|
|
|
|
cli.Printf(" %s %s\n\n", name, usage)
|
|
|
|
cli.FlagSet.PrintDefaults()
|
|
|
|
cli.Printf("\n")
|
|
|
|
}
|
|
|
|
|
|
|
|
return cli
|
|
|
|
}
|
|
|
|
|
|
|
|
type CLI struct {
|
|
|
|
*flag.FlagSet
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cli *CLI) Printf(format string, args ...any) {
|
|
|
|
_, _ = fmt.Fprintf(cli.FlagSet.Output(), format, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
func readFile(match string, dest any) error {
|
|
|
|
data, err := os.ReadFile(match)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return json.Unmarshal(data, dest)
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
cli := newCLI("merge-swagger", "[opts] <glob...>")
|
|
|
|
|
|
|
|
output := cli.String("output", "-", `Specify where the merged file should be written. "-" for stdout.`)
|
|
|
|
|
|
|
|
err := cli.Parse(os.Args[1:])
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if cli.NArg() < 1 {
|
|
|
|
cli.Usage()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var all []string
|
|
|
|
|
|
|
|
for _, arg := range cli.Args() {
|
|
|
|
matches, err := filepath.Glob(arg)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
all = append(all, matches...)
|
|
|
|
}
|
|
|
|
|
|
|
|
full := map[string]interface{}{}
|
|
|
|
|
|
|
|
for _, match := range all {
|
|
|
|
data := map[string]interface{}{}
|
|
|
|
|
|
|
|
err = readFile(match, &data)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = mergo.Merge(&full, data, mergo.WithOverride, mergo.WithAppendSlice)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
full["consumes"] = []string{
|
|
|
|
"application/json",
|
|
|
|
}
|
|
|
|
|
|
|
|
full["produces"] = []string{
|
|
|
|
"application/json",
|
|
|
|
}
|
|
|
|
|
|
|
|
out := os.Stdout
|
|
|
|
if output != nil && *output != "-" && *output != "" {
|
|
|
|
out, err = os.Create(*output)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer out.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
enc := json.NewEncoder(out)
|
|
|
|
enc.SetIndent("", " ")
|
|
|
|
_ = enc.Encode(full)
|
|
|
|
}
|