1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2024-12-31 01:53:50 +02:00
goreleaser/cmd/schema.go
Carlos A Becker ce264f1fca
fix: jsonschema
very hacky, fixes #3238 for good

refs https://github.com/invopop/jsonschema/issues/26

Signed-off-by: Carlos A Becker <caarlos0@users.noreply.github.com>
2022-08-17 23:50:14 -03:00

55 lines
1.4 KiB
Go

package cmd
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"github.com/goreleaser/goreleaser/pkg/config"
"github.com/invopop/jsonschema"
"github.com/spf13/cobra"
)
type schemaCmd struct {
cmd *cobra.Command
output string
}
func newSchemaCmd() *schemaCmd {
root := &schemaCmd{}
cmd := &cobra.Command{
Use: "jsonschema",
Aliases: []string{"schema"},
Short: "outputs goreleaser's JSON schema",
SilenceUsage: true,
SilenceErrors: true,
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
schema := jsonschema.Reflect(&config.Project{})
schema.Definitions["FileInfo"] = jsonschema.Reflect(&config.FileInfo{})
schema.Description = "goreleaser configuration definition file"
bts, err := json.MarshalIndent(schema, " ", " ")
if err != nil {
return fmt.Errorf("failed to create jsonschema: %w", err)
}
if root.output == "-" {
fmt.Println(string(bts))
return nil
}
if err := os.MkdirAll(filepath.Dir(root.output), 0o755); err != nil {
return fmt.Errorf("failed to write jsonschema file: %w", err)
}
if err := os.WriteFile(root.output, bts, 0o666); err != nil {
return fmt.Errorf("failed to write jsonschema file: %w", err)
}
return nil
},
}
cmd.Flags().StringVarP(&root.output, "output", "o", "-", "where to save the json schema")
root.cmd = cmd
return root
}