Skip to content

Commit d6fdbb6

Browse files
Docs: Schema generation (#2547)
* docs: update reference and include terragrunt * feat: dgctl schema --------- Co-authored-by: Mohamed Habib <moe.habib9@gmail.com>
1 parent 856d9d6 commit d6fdbb6

5 files changed

Lines changed: 125 additions & 4 deletions

File tree

dgctl/Makefile

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
.PHONY: build clean test schema schema-docs
2+
3+
# Binary name
4+
BINARY_NAME=dgctl
5+
6+
# Build the dgctl binary
7+
build:
8+
go build -o $(BINARY_NAME) ./main.go
9+
10+
# Clean build artifacts
11+
clean:
12+
rm -f $(BINARY_NAME)
13+
rm -f schema.json
14+
15+
# Run tests
16+
test:
17+
go test -v ./...
18+
19+
# Generate JSON Schema for digger.yml and output to stdout
20+
schema:
21+
go run . schema
22+
23+
# Generate JSON Schema and save to docs folder
24+
schema-docs:
25+
go run . schema -o ./schema.json
26+
@echo "Schema generated at ./schema.json"
27+
28+
# Install dependencies
29+
deps:
30+
go mod tidy
31+
32+
# Run the schema command and verify it works
33+
verify-schema:
34+
@echo "Generating schema..."
35+
@go run . schema > /dev/null && echo "Schema generation successful" || echo "Schema generation failed"

dgctl/cmd/schema.go

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
Copyright © 2024 diggerhq
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
package cmd
17+
18+
import (
19+
"encoding/json"
20+
"fmt"
21+
"os"
22+
"reflect"
23+
24+
"github.com/diggerhq/digger/libs/digger_config"
25+
"github.com/invopop/jsonschema"
26+
"github.com/spf13/cobra"
27+
)
28+
29+
var outputFile string
30+
31+
// schemaCmd represents the schema command
32+
var schemaCmd = &cobra.Command{
33+
Use: "schema",
34+
Short: "Generate JSON Schema for digger.yml",
35+
Long: `Generate a JSON Schema from the digger.yml configuration structure.
36+
37+
Examples:
38+
dgctl schema # Output schema to stdout
39+
dgctl schema -o schema.json # Write schema to file
40+
dgctl schema --output docs/schema.json`,
41+
Run: func(cmd *cobra.Command, args []string) {
42+
schema := generateSchema()
43+
44+
jsonBytes, err := json.MarshalIndent(schema, "", " ")
45+
if err != nil {
46+
fmt.Fprintf(os.Stderr, "Error marshaling schema: %v\n", err)
47+
os.Exit(1)
48+
}
49+
50+
if outputFile != "" {
51+
err = os.WriteFile(outputFile, jsonBytes, 0644)
52+
if err != nil {
53+
fmt.Fprintf(os.Stderr, "Error writing schema to file: %v\n", err)
54+
os.Exit(1)
55+
}
56+
fmt.Printf("Schema written to %s\n", outputFile)
57+
} else {
58+
fmt.Println(string(jsonBytes))
59+
}
60+
},
61+
}
62+
63+
func generateSchema() *jsonschema.Schema {
64+
r := &jsonschema.Reflector{
65+
KeyNamer: jsonschema.ToSnakeCase,
66+
RequiredFromJSONSchemaTags: true,
67+
AllowAdditionalProperties: true,
68+
DoNotReference: true,
69+
Namer: func(t reflect.Type) string {
70+
return t.Name()
71+
},
72+
}
73+
74+
schema := r.Reflect(&digger_config.DiggerConfigYaml{})
75+
schema.ID = jsonschema.ID("https://digger.dev/schemas/digger.yml.json")
76+
77+
return schema
78+
}
79+
80+
func init() {
81+
rootCmd.AddCommand(schemaCmd)
82+
schemaCmd.Flags().StringVarP(&outputFile, "output", "o", "", "Output file path (default: stdout)")
83+
}

dgctl/go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ replace github.com/diggerhq/digger/libs => ../libs
77
require (
88
github.com/diggerhq/digger/libs v0.0.0-00010101000000-000000000000
99
github.com/google/go-github/v61 v61.0.0
10+
github.com/invopop/jsonschema v0.12.0
1011
github.com/spf13/cobra v1.9.1
1112
github.com/spf13/pflag v1.0.6
1213
github.com/spf13/viper v1.20.1

docs/ce/reference/digger.yml.mdx

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -203,10 +203,6 @@ workflows:
203203
Disable the status check that verifies apply was executed.
204204
</ParamField>
205205

206-
<ParamField path="trusted_appIDs" type="array" default="[]">
207-
Allow bot comments from these GitHub user IDs. Example: `trusted_appIDs: [41898282]` for GitHub Actions.
208-
</ParamField>
209-
210206
<ParamField path="comment_render_mode" type="string" default="basic">
211207
How to render plan output in comments. Options: `basic`, `detailed`.
212208
</ParamField>

go.work.sum

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1126,6 +1126,8 @@ github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiE
11261126
github.com/aymanbagabas/go-osc52/v2 v2.0.1/go.mod h1:uYgXzlJ7ZpABp8OJ+exZzJJhRNQ2ASbcXHWsFqH8hp8=
11271127
github.com/aymerick/douceur v0.2.0 h1:Mv+mAeH1Q+n9Fr+oyamOlAkUNPWPlA8PPGR0QAaYuPk=
11281128
github.com/aymerick/douceur v0.2.0/go.mod h1:wlT5vV2O3h55X9m7iVYN0TBM0NH/MmbLnd30/FjWUq4=
1129+
github.com/bahlo/generic-list-go v0.2.0 h1:5sz/EEAK+ls5wF+NeqDpk5+iNdMDXrh3z3nPnH1Wvgk=
1130+
github.com/bahlo/generic-list-go v0.2.0/go.mod h1:2KvAjgMlE5NNynlg/5iLrrCCZ2+5xWbdbCW3pNTGyYg=
11291131
github.com/baiyubin/aliyun-sts-go-sdk v0.0.0-20180326062324-cfa1a18b161f h1:ZNv7On9kyUzm7fvRZumSyy/IUiSC7AzL0I1jKKtwooA=
11301132
github.com/bazelbuild/rules_go v0.49.0 h1:5vCbuvy8Q11g41lseGJDc5vxhDjJtfxr6nM/IC4VmqM=
11311133
github.com/bazelbuild/rules_go v0.49.0/go.mod h1:Dhcz716Kqg1RHNWos+N6MlXNkjNP2EwZQ0LukRKJfMs=
@@ -1743,6 +1745,8 @@ github.com/inancgumus/screen v0.0.0-20190314163918-06e984b86ed3 h1:fO9A67/izFYFY
17431745
github.com/inancgumus/screen v0.0.0-20190314163918-06e984b86ed3/go.mod h1:Ey4uAp+LvIl+s5jRbOHLcZpUDnkjLBROl15fZLwPlTM=
17441746
github.com/intel/goresctrl v0.3.0 h1:K2D3GOzihV7xSBedGxONSlaw/un1LZgWsc9IfqipN4c=
17451747
github.com/intel/goresctrl v0.3.0/go.mod h1:fdz3mD85cmP9sHD8JUlrNWAxvwM86CrbmVXltEKd7zk=
1748+
github.com/invopop/jsonschema v0.12.0 h1:6ovsNSuvn9wEQVOyc72aycBMVQFKz7cPdMJn10CvzRI=
1749+
github.com/invopop/jsonschema v0.12.0/go.mod h1:ffZ5Km5SWWRAIN6wbDXItl95euhFz2uON45H2qjYt+0=
17461750
github.com/iris-contrib/go.uuid v2.0.0+incompatible h1:XZubAYg61/JwnJNbZilGjf3b3pB80+OQg2qf6c8BfWE=
17471751
github.com/iris-contrib/go.uuid v2.0.0+incompatible/go.mod h1:iz2lgM/1UnEf1kP0L/+fafWORmlnuysV2EMP8MW+qe0=
17481752
github.com/iris-contrib/httpexpect/v2 v2.12.1 h1:3cTZSyBBen/kfjCtgNFoUKi1u0FVXNaAjyRJOo6AVS4=
@@ -2313,6 +2317,8 @@ github.com/vmware/govmomi v0.20.3 h1:gpw/0Ku+6RgF3jsi7fnCLmlcikBHfKBCUcu1qgc16OU
23132317
github.com/willf/bitset v1.1.11-0.20200630133818-d5bec3311243/go.mod h1:RjeCKbqT1RxIR/KWY6phxZiaY1IyutSBfGjNPySAYV4=
23142318
github.com/willf/bitset v1.1.11 h1:N7Z7E9UvjW+sGsEl7k/SJrvY2reP1A07MrGuCjIOjRE=
23152319
github.com/willf/bitset v1.1.11/go.mod h1:83CECat5yLh5zVOf4P1ErAgKA5UDvKtgyUABdr3+MjI=
2320+
github.com/wk8/go-ordered-map/v2 v2.1.8 h1:5h/BUHu93oj4gIdvHHHGsScSTMijfx5PeYkE/fJgbpc=
2321+
github.com/wk8/go-ordered-map/v2 v2.1.8/go.mod h1:5nJHM5DyteebpVlHnWMV0rPz6Zp7+xBAnxjb1X5vnTw=
23162322
github.com/x448/float16 v0.8.4 h1:qLwI1I70+NjRFUR3zs1JPUCgaCXSh3SW62uAKT1mSBM=
23172323
github.com/x448/float16 v0.8.4/go.mod h1:14CWIYCyZA/cWjXOioeEpHeN/83MdbZDRQHoFcYsOfg=
23182324
github.com/xdg-go/pbkdf2 v1.0.0 h1:Su7DPu48wXMwC3bs7MCNG+z4FhcyEuz5dlvchbq0B0c=

0 commit comments

Comments
 (0)