resticprofile icon indicating copy to clipboard operation
resticprofile copied to clipboard

JSON Schema

Open rampd2 opened this issue 2 years ago • 9 comments

Is there a JSON Schema for restic(profile) configuration files?

I see a JSON Schema especially useful if the editor supports it (e.g. VS Code), because the editing of a supported file is much more comfortable.

some examples from other projects you might know:

I couldn't find any information about an existing schema for resticprofile, so maybe there isn't one? If this is not yet planned, this issue is more a feature request/enhancement idea.

rampd2 avatar Apr 18 '22 16:04 rampd2

No, there's no schema available yet. But I was planning to do so.

I need to look up how to do it, as I've never published any before

creativeprojects avatar Apr 18 '22 20:04 creativeprojects

Had a quick look on libs that can generate a schema (as a starting point):

  • https://github.com/atombender/go-jsonschema - very simple implementation, could be taken as source and adjusted to work on mapstructure tags.
  • https://github.com/invopop/jsonschema - is more complete, maintained and should also be usable on the structs when defining an AdditionalFields callback that maps mapstructure tags to json tags.

jkellerer avatar Apr 19 '22 13:04 jkellerer

Oh excellent!

It means little maintenance. I like that 😉

Maybe we could also read the cobra flags from restic repo so they get added automatically (wishful thinking at best).

creativeprojects avatar Apr 19 '22 20:04 creativeprojects

Maybe we could also read the cobra flags from restic repo so they get added automatically (wishful thinking at best)

This is not so hard, I think. Cobra generates manual pages that are quite well structured and not too difficult to parse. From there we can get commands, options and even descriptions. We'd only need to decide if that happens during build time (my preference), runtime or both (when manual pages exist already). Build time means we'd need to reference restic as dependency and build it. As a side effect this enables real integration tests.

Knowing all restic commands and valid options would enable more than just json-schema, it also allows to validate the config, filter CLI args (e.g. replace the static list I added few days ago) and improve shell completion.

jkellerer avatar Apr 20 '22 10:04 jkellerer

Knowing all restic commands and valid options would enable more than just json-schema, it also allows to validate the config, filter CLI args (e.g. replace the static list I added few days ago) and improve shell completion.

Originally I was thinking about running the restic initialisation code and get the flags definition from cobra. But since it's all happening in a main package that's not an option.

But you're right, it shouldn't be too difficult to parse the generated help for each command 👍🏻

creativeprojects avatar Apr 20 '22 20:04 creativeprojects

Another option would be to let some tooling generate the initial json schema and then convert that into a CUE definition: https://cuelang.org/docs/usecases/datadef/

CUE can then export that in various formats and cue can even be reused to directly generate and validate the user's configuration.

megamorf avatar Apr 23 '22 14:04 megamorf

@creativeprojects: Had played a bit with restic command dictionary, here's a working prototype: #107

Let me know what you think then I'll try to add json-schema generation (and tests).

jkellerer avatar Apr 23 '22 16:04 jkellerer

@megamorf - Thanks for the hint 👍. We're looking for low maintenance solutions that continuously generate the information from code. cue-lang does look interesting and might have the ability to work with this use-case.

However cue-lang to json-schema conversion is not ready https://github.com/cue-lang/cue/issues/929. So we probably implement json-schema for now only. But if you want a cue schema you can still generated it from the json-schema using the cue tools.

jkellerer avatar Apr 23 '22 17:04 jkellerer

Let me know what you think then I'll try to add json-schema generation (and tests).

Yes please, it's really cool

creativeprojects avatar Apr 23 '22 23:04 creativeprojects

Now available in v0.21.0 👍🏻 See Documentation

creativeprojects avatar Apr 03 '23 19:04 creativeprojects