1
0
mirror of https://github.com/goreleaser/goreleaser.git synced 2024-12-27 01:33:39 +02:00
goreleaser/cmd/schema.go
Carlos Alexandro Becker ec2db4a727
feat!: rename module to /v2 (#4894)
<!--

Hi, thanks for contributing!

Please make sure you read our CONTRIBUTING guide.

Also, add tests and the respective documentation changes as well.

-->


<!-- If applied, this commit will... -->

...

<!-- Why is this change being made? -->

...

<!-- # Provide links to any relevant tickets, URLs or other resources
-->

...

---------

Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>
2024-05-26 15:02:57 -03:00

57 lines
1.5 KiB
Go

package cmd
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"github.com/goreleaser/goreleaser/v2/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,
ValidArgsFunction: cobra.NoFileCompletions,
RunE: func(_ *cobra.Command, _ []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 JSONSchema file")
_ = cmd.MarkFlagFilename("output", "json")
root.cmd = cmd
return root
}