-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add json schema generation tool (#59)
Add a cli that generates image customizer API json schema. The generated schema file can be leveraged by other tools for API dependency and config validation. Here's an example using it in VSCode for syntax validation: ![image](https://github.com/user-attachments/assets/9d6e2457-c934-446a-98c0-9c487e84c154) ### **Checklist** - [x] Tests added/updated - [ ] Documentation updated (if needed) - [x] Code conforms to style guidelines --------- Signed-off-by: Roaa Sakr <[email protected]>
- Loading branch information
Showing
9 changed files
with
121 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# Copyright (c) Microsoft Corporation. | ||
# Licensed under the MIT License. | ||
|
||
BINARY_NAME := bin/imagecustomizerschemacli | ||
SCHEMA_FILE := schema.json | ||
|
||
.PHONY: all | ||
all: run | ||
|
||
.PHONY: run | ||
run: | ||
@echo "Generating $(SCHEMA_FILE)" | ||
go run . -o $(SCHEMA_FILE) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"log" | ||
"os" | ||
|
||
"github.com/invopop/jsonschema" | ||
"github.com/microsoft/azurelinux/toolkit/tools/imagecustomizerapi" | ||
"gopkg.in/alecthomas/kingpin.v2" | ||
) | ||
|
||
func main() { | ||
app := kingpin.New("jsonschemacli", "A CLI tool to generate JSON schema for the image customizer API.") | ||
outputFile := app.Flag("output", "Path to the output JSON schema file").Short('o').Required().String() | ||
|
||
kingpin.MustParse(app.Parse(os.Args[1:])) | ||
|
||
if err := generateJSONSchema(*outputFile); err != nil { | ||
log.Fatalf("Error: %v", err) | ||
} | ||
|
||
fmt.Printf("JSON schema has been written to %s\n", *outputFile) | ||
} | ||
|
||
func generateJSONSchema(outputFile string) error { | ||
reflector := jsonschema.Reflector{ | ||
RequiredFromJSONSchemaTags: true, | ||
} | ||
|
||
schema := reflector.Reflect(&imagecustomizerapi.Config{}) | ||
schemaJSON, err := json.MarshalIndent(schema, "", " ") | ||
if err != nil { | ||
return fmt.Errorf("failed to marshal schema: %w", err) | ||
} | ||
|
||
// Write schema to file | ||
if err := os.WriteFile(outputFile, schemaJSON, 0o644); err != nil { | ||
return fmt.Errorf("failed to write schema to file: %w", err) | ||
} | ||
|
||
return nil | ||
} |