Skip to content

Commit ceba9f4

Browse files
chrisghillCopilot
andauthored
Re-add mass schema dereference command (#171)
* Re-add command * Update docs/helpdocs/schema/dereference.md Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * doc changes --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 2c61ad4 commit ceba9f4

5 files changed

Lines changed: 127 additions & 2 deletions

File tree

cmd/schema.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
package cmd
22

33
import (
4+
"encoding/json"
45
"fmt"
6+
"os"
7+
"path/filepath"
58

69
"github.com/massdriver-cloud/mass/docs/helpdocs"
10+
"github.com/massdriver-cloud/mass/pkg/bundle"
711
"github.com/massdriver-cloud/mass/pkg/jsonschema"
812
"github.com/massdriver-cloud/mass/pkg/prettylogs"
13+
"github.com/massdriver-cloud/massdriver-sdk-go/massdriver/client"
914
"github.com/spf13/cobra"
1015
)
1116

@@ -16,6 +21,15 @@ func NewCmdSchema() *cobra.Command {
1621
Long: helpdocs.MustRender("schema"),
1722
}
1823

24+
schemaDereferenceCmd := &cobra.Command{
25+
Use: "dereference",
26+
Short: "Dereferences a JSON Schema",
27+
Long: helpdocs.MustRender("schema/dereference"),
28+
RunE: runSchemaDereference,
29+
}
30+
schemaDereferenceCmd.Flags().StringP("file", "f", "", "Path to JSON document (use - for stdin)")
31+
schemaDereferenceCmd.MarkFlagRequired("file")
32+
1933
schemaValidateCmd := &cobra.Command{
2034
Use: "validate",
2135
Short: "Validates a JSON document against a JSON Schema",
@@ -27,11 +41,59 @@ func NewCmdSchema() *cobra.Command {
2741
schemaValidateCmd.MarkFlagRequired("document")
2842
schemaValidateCmd.MarkFlagRequired("schema")
2943

44+
schemaCmd.AddCommand(schemaDereferenceCmd)
3045
schemaCmd.AddCommand(schemaValidateCmd)
3146

3247
return schemaCmd
3348
}
3449

50+
func runSchemaDereference(cmd *cobra.Command, args []string) error {
51+
schemaPath, _ := cmd.Flags().GetString("file")
52+
cmd.SilenceUsage = true
53+
54+
var schemaFile *os.File
55+
basePath := "."
56+
if schemaPath == "-" {
57+
schemaFile = os.Stdin
58+
} else {
59+
var openErr error
60+
schemaFile, openErr = os.Open(schemaPath)
61+
basePath = filepath.Dir(schemaPath)
62+
if openErr != nil {
63+
return fmt.Errorf("failed to open schema file %s: %w", schemaPath, openErr)
64+
}
65+
defer schemaFile.Close()
66+
}
67+
68+
var rawSchema map[string]any
69+
if err := json.NewDecoder(schemaFile).Decode(&rawSchema); err != nil {
70+
return fmt.Errorf("failed to decode JSON schema: %w", err)
71+
}
72+
73+
mdClient, mdClientErr := client.New()
74+
if mdClientErr != nil {
75+
return fmt.Errorf("error initializing massdriver client: %w", mdClientErr)
76+
}
77+
78+
derefOpts := bundle.DereferenceOptions{
79+
Client: mdClient,
80+
Cwd: basePath,
81+
}
82+
dereferencedSchema, derefErr := bundle.DereferenceSchema(rawSchema, derefOpts)
83+
if derefErr != nil {
84+
return fmt.Errorf("failed to dereference schema: %w", derefErr)
85+
}
86+
87+
dereferencedJSON, jsonErr := json.MarshalIndent(dereferencedSchema, "", " ")
88+
if jsonErr != nil {
89+
return fmt.Errorf("failed to marshal dereferenced schema to JSON: %w", jsonErr)
90+
}
91+
92+
fmt.Println(string(dereferencedJSON))
93+
94+
return nil
95+
}
96+
3597
func runSchemaValidate(cmd *cobra.Command, args []string) error {
3698
schemaPath, _ := cmd.Flags().GetString("schema")
3799
documentPath, _ := cmd.Flags().GetString("document")

docs/generated/mass_bundle_new.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,10 @@ mass bundle new [flags]
4444
-c, --connections strings Connections and names to add to the bundle - example: network=massdriver/vpc
4545
-d, --description string Description of the new bundle
4646
-h, --help help for new
47-
-n, --name string Name of the new bundle. Setting this will disable the interactive prompt.
47+
-n, --name string Name of the new bundle. Setting this along with --template-name will disable the interactive prompt.
4848
-o, --output-directory string Directory to output the new bundle (default ".")
4949
-p, --params-directory string Path with existing params to use - opentofu module directory or helm chart values.yaml
50-
-t, --template-name string Name of the bundle template to use. Setting this will disable the interactive prompt.
50+
-t, --template-name string Name of the bundle template to use. Setting this along with --name will disable the interactive prompt.
5151
```
5252

5353
### SEE ALSO

docs/generated/mass_schema.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,5 @@ mass schema validate --schema=my-json-schema.json --document=my-document.json
2626
### SEE ALSO
2727

2828
* [mass](/cli/commands/mass) - Massdriver Cloud CLI
29+
* [mass schema dereference](/cli/commands/mass_schema_dereference) - Dereferences a JSON Schema
2930
* [mass schema validate](/cli/commands/mass_schema_validate) - Validates a JSON document against a JSON Schema
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
---
2+
id: mass_schema_dereference.md
3+
slug: /cli/commands/mass_schema_dereference
4+
title: Mass Schema Dereference
5+
sidebar_label: Mass Schema Dereference
6+
---
7+
## mass schema dereference
8+
9+
Dereferences a JSON Schema
10+
11+
### Synopsis
12+
13+
# Dereferences a JSON Schema Document
14+
15+
This command will expand all the `$ref` statements in a JSON Schema. This command is useful when managing artifact definition schemas and using `$refs` to keep your definitions "DRY".
16+
17+
## Examples
18+
19+
From an existing file
20+
21+
```shell
22+
mass schema dereference --file artdef.json
23+
```
24+
25+
From stdin
26+
27+
```shell
28+
cat artdef.json | mass schema dereference -f -
29+
```
30+
31+
32+
```
33+
mass schema dereference [flags]
34+
```
35+
36+
### Options
37+
38+
```
39+
-f, --file string Path to JSON document (use - for stdin)
40+
-h, --help help for dereference
41+
```
42+
43+
### SEE ALSO
44+
45+
* [mass schema](/cli/commands/mass_schema) - Manage JSON Schemas
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Dereferences a JSON Schema Document
2+
3+
This command will expand all the `$ref` statements in a JSON Schema. This command is useful when managing artifact definition schemas and using `$refs` to keep your definitions "DRY".
4+
5+
## Examples
6+
7+
From an existing file
8+
9+
```shell
10+
mass schema dereference --file artdef.json
11+
```
12+
13+
From stdin
14+
15+
```shell
16+
cat artdef.json | mass schema dereference -f -
17+
```

0 commit comments

Comments
 (0)