github.com/goreleaser/nfpm/v2@v2.44.0/internal/cmd/schema.go (about) 1 package cmd 2 3 import ( 4 "encoding/json" 5 "fmt" 6 "os" 7 "path/filepath" 8 9 "github.com/goreleaser/nfpm/v2" 10 "github.com/invopop/jsonschema" 11 "github.com/spf13/cobra" 12 ) 13 14 type schemaCmd struct { 15 cmd *cobra.Command 16 output string 17 } 18 19 func newSchemaCmd() *schemaCmd { 20 root := &schemaCmd{} 21 cmd := &cobra.Command{ 22 Use: "jsonschema", 23 Aliases: []string{"schema"}, 24 Short: "Outputs nFPM's JSON schema", 25 SilenceUsage: true, 26 SilenceErrors: true, 27 Args: cobra.NoArgs, 28 ValidArgsFunction: cobra.NoFileCompletions, 29 RunE: func(*cobra.Command, []string) error { 30 schema := jsonschema.Reflect(&nfpm.Config{}) 31 schema.Description = "nFPM configuration definition file" 32 bts, err := json.MarshalIndent(schema, " ", " ") 33 if err != nil { 34 return fmt.Errorf("failed to create jsonschema: %w", err) 35 } 36 if root.output == "-" { 37 fmt.Println(string(bts)) 38 return nil 39 } 40 if err := os.MkdirAll(filepath.Dir(root.output), 0o755); err != nil { 41 return fmt.Errorf("failed to write jsonschema file: %w", err) 42 } 43 if err := os.WriteFile(root.output, bts, 0o666); err != nil { 44 return fmt.Errorf("failed to write jsonschema file: %w", err) 45 } 46 return nil 47 }, 48 } 49 50 cmd.Flags().StringVarP(&root.output, "output", "o", "-", "where to save the json schema") 51 _ = cmd.MarkFlagFilename("output", "json") 52 53 root.cmd = cmd 54 return root 55 }