Skip to content

Commit

Permalink
add base url on json schema (#408)
Browse files Browse the repository at this point in the history
* add base url on json schema

* add documentation on SCHEMA_BASE_URL

* improve documentation on json schema generation
  • Loading branch information
creativeprojects authored Oct 2, 2024
1 parent 57d1439 commit 249fd41
Show file tree
Hide file tree
Showing 12 changed files with 33 additions and 32 deletions.
27 changes: 7 additions & 20 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -243,26 +243,13 @@ generate-jsonschema: build

mkdir -p $(JSONSCHEMA_DIR) || echo "$(JSONSCHEMA_DIR) exists"

$(abspath $(BINARY)) generate --json-schema v1 > $(JSONSCHEMA_DIR)/config-1.json
$(abspath $(BINARY)) generate --json-schema v2 > $(JSONSCHEMA_DIR)/config-2.json
$(abspath $(BINARY)) generate --json-schema --version 0.9 v1 > $(JSONSCHEMA_DIR)/config-1-restic-0-9.json
$(abspath $(BINARY)) generate --json-schema --version 0.9 v2 > $(JSONSCHEMA_DIR)/config-2-restic-0-9.json
$(abspath $(BINARY)) generate --json-schema --version 0.10 v1 > $(JSONSCHEMA_DIR)/config-1-restic-0-10.json
$(abspath $(BINARY)) generate --json-schema --version 0.10 v2 > $(JSONSCHEMA_DIR)/config-2-restic-0-10.json
$(abspath $(BINARY)) generate --json-schema --version 0.11 v1 > $(JSONSCHEMA_DIR)/config-1-restic-0-11.json
$(abspath $(BINARY)) generate --json-schema --version 0.11 v2 > $(JSONSCHEMA_DIR)/config-2-restic-0-11.json
$(abspath $(BINARY)) generate --json-schema --version 0.12 v1 > $(JSONSCHEMA_DIR)/config-1-restic-0-12.json
$(abspath $(BINARY)) generate --json-schema --version 0.12 v2 > $(JSONSCHEMA_DIR)/config-2-restic-0-12.json
$(abspath $(BINARY)) generate --json-schema --version 0.13 v1 > $(JSONSCHEMA_DIR)/config-1-restic-0-13.json
$(abspath $(BINARY)) generate --json-schema --version 0.13 v2 > $(JSONSCHEMA_DIR)/config-2-restic-0-13.json
$(abspath $(BINARY)) generate --json-schema --version 0.14 v1 > $(JSONSCHEMA_DIR)/config-1-restic-0-14.json
$(abspath $(BINARY)) generate --json-schema --version 0.14 v2 > $(JSONSCHEMA_DIR)/config-2-restic-0-14.json
$(abspath $(BINARY)) generate --json-schema --version 0.15 v1 > $(JSONSCHEMA_DIR)/config-1-restic-0-15.json
$(abspath $(BINARY)) generate --json-schema --version 0.15 v2 > $(JSONSCHEMA_DIR)/config-2-restic-0-15.json
$(abspath $(BINARY)) generate --json-schema --version 0.16 v1 > $(JSONSCHEMA_DIR)/config-1-restic-0-16.json
$(abspath $(BINARY)) generate --json-schema --version 0.16 v2 > $(JSONSCHEMA_DIR)/config-2-restic-0-16.json
$(abspath $(BINARY)) generate --json-schema --version 0.17 v1 > $(JSONSCHEMA_DIR)/config-1-restic-0-17.json
$(abspath $(BINARY)) generate --json-schema --version 0.17 v2 > $(JSONSCHEMA_DIR)/config-2-restic-0-17.json
for config_version in 1 2 ; do \
$(abspath $(BINARY)) generate --json-schema v$$config_version > $(JSONSCHEMA_DIR)/config-$$config_version.json ; \
for restic_version in 0.9 0.10 0.11 0.12 0.13 0.14 0.15 0.16 0.17 ; do \
name=$$(echo $$restic_version | sed 's/\./-/g') ; \
$(abspath $(BINARY)) generate --json-schema --version $$restic_version v$$config_version > $(JSONSCHEMA_DIR)/config-$$config_version-restic-$$name.json ; \
done ; \
done

generate-config-reference: build
@echo "[*] $@"
Expand Down
1 change: 1 addition & 0 deletions codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ ignore:
- systemd.go
- update.go
- main.go
- run_profile.go
- syslog.go
- syslog_windows.go

Expand Down
10 changes: 8 additions & 2 deletions config/jsonschema/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/hex"
"encoding/json"
"fmt"
"os"
"regexp"
"slices"
"strings"
Expand All @@ -16,9 +17,10 @@ import (
const (
// for best compatibility the schema is mostly "draft-07", but some new vocabulary of 2019-09 is used
jsonSchemaVersion = "https://json-schema.org/draft/2019-09/schema"
schemaUrlTemplate = "/resticprofile/jsonschema/config-%d%s.json"
schemaUrlTemplate = "%sconfig-%d%s.json"
referenceUrlTemplate = "#/$defs/%s"
arrayTitleSuffix = "..."
defaultBaseURL = "https://creativeprojects.github.io/resticprofile/jsonschema/"
)

type schemaRoot struct {
Expand Down Expand Up @@ -47,9 +49,13 @@ func newSchema(version config.Version, id string, content *schemaObject) (root *
if len(id) > 0 {
id = fmt.Sprintf("-restic-%s", strings.ReplaceAll(id, ".", "-"))
}
baseURL := os.Getenv("SCHEMA_BASE_URL")
if len(baseURL) == 0 {
baseURL = defaultBaseURL
}
root = &schemaRoot{
Schema: jsonSchemaVersion,
Id: fmt.Sprintf(schemaUrlTemplate, version, id),
Id: fmt.Sprintf(schemaUrlTemplate, baseURL, version, id),
Defs: make(map[string]SchemaType),
schemaObject: *content,
}
Expand Down
8 changes: 5 additions & 3 deletions docs/content/configuration/jsonschema/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@ These schemas contain only flags and commands of a specific *restic* version. Th
validate a config only when flags are used that the selected *restic* version supports
according to its manual pages.

{{% notice style="hint" %}}
{{% notice style="info" %}}
As an alternative to URLs, schemas can be generated locally with:
`resticprofile generate --json-schema [--version RESTIC_VERSION] [v2|v1]`

You can prefix the command with the environment variable `SCHEMA_BASE_URL` to change the base URL (defaults to `https://creativeprojects.github.io/resticprofile/jsonschema/`). This can be useful if you're working in an offline environment or need to use a custom schema location.
{{% /notice %}}

## Usage (vscode)
Expand Down Expand Up @@ -64,8 +66,8 @@ version: "2"
{{% /tab %}}
{{< /tabs >}}

{{% notice style="hint" %}}
YAML & TOML validation with JSON schema is not supported out of the box. Additional extensions are required.
{{% notice style="info" %}}
YAML & TOML validation with JSON schema is not supported out of the box. Additional extensions are required, such as 'redhat.vscode-yaml' for YAML and 'tamasfe.even-better-toml' for TOML in Visual Studio Code.
{{% /notice %}}

**Example**
Expand Down
13 changes: 8 additions & 5 deletions examples/dev.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
#:schema https://creativeprojects.github.io/resticprofile/jsonschema/config-1.json

version = "1"

[global]
default-command = "snapshots"
initialize = false
priority = "low"

[default]
initialize = false
Expand All @@ -17,11 +25,6 @@
[documents.snapshots]
tag = ["documents"]

[global]
default-command = "version"
initialize = false
priority = "low"

[groups]
full-backup = ["root","src"]

Expand Down
2 changes: 2 additions & 0 deletions examples/linux.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#:schema https://creativeprojects.github.io/resticprofile/jsonschema/config-1.json

[global]
priority = "low"
initialize = false
Expand Down
2 changes: 1 addition & 1 deletion examples/linux.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
version: "1"

global:
default-command: version
default-command: snapshots
initialize: false
priority: low
systemd-unit-template: sample.service
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion sonar-project.properties
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
sonar.organization=creativeprojects
sonar.projectKey=creativeprojects_resticprofile
sonar.projectName=resticprofile
sonar.projectVersion=0.27.0
sonar.projectVersion=0.28.0

sonar.sources=.
sonar.exclusions=**/*_test.go,/docs/**
Expand Down

0 comments on commit 249fd41

Please sign in to comment.