Library for building interactive terminal inputs by using JSON schema.
Library can be used to build interactive mode for golang terminal applications like Cobra (https://github.com/spf13/cobra) etc. Library uses JSON schema as specification for questions and survey go library to execute them (https://github.com/AlecAivazis/survey). Developers will get JSON files containing answers from the user that conform to specified JSON schema.
In typical execution library will:
Project provides an CLI that reads JSON schema as file input. To run cli with your local json schema execute
go get github.com/jackdelahunt/survey-json-schema
askjschema --file=jsonschema.json
You can also use remote json schema
askjschema --file https://raw.githubusercontent.com/wtrocki/survey-json-schema/main/pkg/surveyjson/test_data/basicTypes.test.schema.json
CLI binary files are also available from Github releases.
Add library to dependencies
go get github.com/jackdelahunt/survey-json-schema
Add this sample code to your cobra handler method
import("github.com/jackdelahunt/survey-json-schema/pkg/surveyjson")
// Creates JSONSchema based of
options := surveyjson.JSONSchemaOptions{
Out: os.Stdout,
In: os.Stdin,
OutErr: os.Stderr,
AskExisting: true,
AutoAcceptDefaults: false,
NoAsk: false,
IgnoreMissingValues: false,
}
yourSchema := ""
initialValues := make(map[string]interface{})
result, err := options.GenerateValues(yourSchema, initialValues)
if err != nil {
return err
}
fmt.Fprint(os.Stdin, string(result))
For fully functional example see pkg/cmd/ask.go file
Library can fetch default values for properties from environment variables. Env var should be constructed as SURVEYVALUE{JSON_SCHEMA_PATH}. For example:
SURVEY_VALUE_USER_AGE=3
Library is basing on JSONSchema processor code originally contributed by Pete Muir for JenkinsX project.