Skip to content

Commit 249fd41

Browse files
add base url on json schema (#408)
* add base url on json schema * add documentation on SCHEMA_BASE_URL * improve documentation on json schema generation
1 parent 57d1439 commit 249fd41

File tree

12 files changed

+33
-32
lines changed

12 files changed

+33
-32
lines changed

Makefile

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -243,26 +243,13 @@ generate-jsonschema: build
243243

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

246-
$(abspath $(BINARY)) generate --json-schema v1 > $(JSONSCHEMA_DIR)/config-1.json
247-
$(abspath $(BINARY)) generate --json-schema v2 > $(JSONSCHEMA_DIR)/config-2.json
248-
$(abspath $(BINARY)) generate --json-schema --version 0.9 v1 > $(JSONSCHEMA_DIR)/config-1-restic-0-9.json
249-
$(abspath $(BINARY)) generate --json-schema --version 0.9 v2 > $(JSONSCHEMA_DIR)/config-2-restic-0-9.json
250-
$(abspath $(BINARY)) generate --json-schema --version 0.10 v1 > $(JSONSCHEMA_DIR)/config-1-restic-0-10.json
251-
$(abspath $(BINARY)) generate --json-schema --version 0.10 v2 > $(JSONSCHEMA_DIR)/config-2-restic-0-10.json
252-
$(abspath $(BINARY)) generate --json-schema --version 0.11 v1 > $(JSONSCHEMA_DIR)/config-1-restic-0-11.json
253-
$(abspath $(BINARY)) generate --json-schema --version 0.11 v2 > $(JSONSCHEMA_DIR)/config-2-restic-0-11.json
254-
$(abspath $(BINARY)) generate --json-schema --version 0.12 v1 > $(JSONSCHEMA_DIR)/config-1-restic-0-12.json
255-
$(abspath $(BINARY)) generate --json-schema --version 0.12 v2 > $(JSONSCHEMA_DIR)/config-2-restic-0-12.json
256-
$(abspath $(BINARY)) generate --json-schema --version 0.13 v1 > $(JSONSCHEMA_DIR)/config-1-restic-0-13.json
257-
$(abspath $(BINARY)) generate --json-schema --version 0.13 v2 > $(JSONSCHEMA_DIR)/config-2-restic-0-13.json
258-
$(abspath $(BINARY)) generate --json-schema --version 0.14 v1 > $(JSONSCHEMA_DIR)/config-1-restic-0-14.json
259-
$(abspath $(BINARY)) generate --json-schema --version 0.14 v2 > $(JSONSCHEMA_DIR)/config-2-restic-0-14.json
260-
$(abspath $(BINARY)) generate --json-schema --version 0.15 v1 > $(JSONSCHEMA_DIR)/config-1-restic-0-15.json
261-
$(abspath $(BINARY)) generate --json-schema --version 0.15 v2 > $(JSONSCHEMA_DIR)/config-2-restic-0-15.json
262-
$(abspath $(BINARY)) generate --json-schema --version 0.16 v1 > $(JSONSCHEMA_DIR)/config-1-restic-0-16.json
263-
$(abspath $(BINARY)) generate --json-schema --version 0.16 v2 > $(JSONSCHEMA_DIR)/config-2-restic-0-16.json
264-
$(abspath $(BINARY)) generate --json-schema --version 0.17 v1 > $(JSONSCHEMA_DIR)/config-1-restic-0-17.json
265-
$(abspath $(BINARY)) generate --json-schema --version 0.17 v2 > $(JSONSCHEMA_DIR)/config-2-restic-0-17.json
246+
for config_version in 1 2 ; do \
247+
$(abspath $(BINARY)) generate --json-schema v$$config_version > $(JSONSCHEMA_DIR)/config-$$config_version.json ; \
248+
for restic_version in 0.9 0.10 0.11 0.12 0.13 0.14 0.15 0.16 0.17 ; do \
249+
name=$$(echo $$restic_version | sed 's/\./-/g') ; \
250+
$(abspath $(BINARY)) generate --json-schema --version $$restic_version v$$config_version > $(JSONSCHEMA_DIR)/config-$$config_version-restic-$$name.json ; \
251+
done ; \
252+
done
266253

267254
generate-config-reference: build
268255
@echo "[*] $@"

codecov.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ ignore:
88
- systemd.go
99
- update.go
1010
- main.go
11+
- run_profile.go
1112
- syslog.go
1213
- syslog_windows.go
1314

config/jsonschema/model.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"encoding/hex"
66
"encoding/json"
77
"fmt"
8+
"os"
89
"regexp"
910
"slices"
1011
"strings"
@@ -16,9 +17,10 @@ import (
1617
const (
1718
// for best compatibility the schema is mostly "draft-07", but some new vocabulary of 2019-09 is used
1819
jsonSchemaVersion = "https://json-schema.org/draft/2019-09/schema"
19-
schemaUrlTemplate = "/resticprofile/jsonschema/config-%d%s.json"
20+
schemaUrlTemplate = "%sconfig-%d%s.json"
2021
referenceUrlTemplate = "#/$defs/%s"
2122
arrayTitleSuffix = "..."
23+
defaultBaseURL = "https://creativeprojects.github.io/resticprofile/jsonschema/"
2224
)
2325

2426
type schemaRoot struct {
@@ -47,9 +49,13 @@ func newSchema(version config.Version, id string, content *schemaObject) (root *
4749
if len(id) > 0 {
4850
id = fmt.Sprintf("-restic-%s", strings.ReplaceAll(id, ".", "-"))
4951
}
52+
baseURL := os.Getenv("SCHEMA_BASE_URL")
53+
if len(baseURL) == 0 {
54+
baseURL = defaultBaseURL
55+
}
5056
root = &schemaRoot{
5157
Schema: jsonSchemaVersion,
52-
Id: fmt.Sprintf(schemaUrlTemplate, version, id),
58+
Id: fmt.Sprintf(schemaUrlTemplate, baseURL, version, id),
5359
Defs: make(map[string]SchemaType),
5460
schemaObject: *content,
5561
}

docs/content/configuration/jsonschema/index.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,11 @@ These schemas contain only flags and commands of a specific *restic* version. Th
2828
validate a config only when flags are used that the selected *restic* version supports
2929
according to its manual pages.
3030

31-
{{% notice style="hint" %}}
31+
{{% notice style="info" %}}
3232
As an alternative to URLs, schemas can be generated locally with:
3333
`resticprofile generate --json-schema [--version RESTIC_VERSION] [v2|v1]`
34+
35+
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.
3436
{{% /notice %}}
3537

3638
## Usage (vscode)
@@ -64,8 +66,8 @@ version: "2"
6466
{{% /tab %}}
6567
{{< /tabs >}}
6668

67-
{{% notice style="hint" %}}
68-
YAML & TOML validation with JSON schema is not supported out of the box. Additional extensions are required.
69+
{{% notice style="info" %}}
70+
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.
6971
{{% /notice %}}
7072

7173
**Example**

examples/dev.toml

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
#:schema https://creativeprojects.github.io/resticprofile/jsonschema/config-1.json
2+
3+
version = "1"
4+
5+
[global]
6+
default-command = "snapshots"
7+
initialize = false
8+
priority = "low"
19

210
[default]
311
initialize = false
@@ -17,11 +25,6 @@
1725
[documents.snapshots]
1826
tag = ["documents"]
1927

20-
[global]
21-
default-command = "version"
22-
initialize = false
23-
priority = "low"
24-
2528
[groups]
2629
full-backup = ["root","src"]
2730

examples/linux.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#:schema https://creativeprojects.github.io/resticprofile/jsonschema/config-1.json
2+
13
[global]
24
priority = "low"
35
initialize = false

examples/linux.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
version: "1"
44

55
global:
6-
default-command: version
6+
default-command: snapshots
77
initialize: false
88
priority: low
99
systemd-unit-template: sample.service
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)