AlecAivazis / survey

A golang library for building interactive and accessible prompts with full support for windows and posix terminals.
MIT License
4.08k stars 350 forks source link

Support loading surveys from yaml #154

Closed theodesp closed 5 years ago

theodesp commented 5 years ago

It would be nice if we could load surveys from yaml. For example:

survey:
  - question:
    name: name
    prompt:
      - input: "What is your name?"
    validate: required
    transform: title
  - question:
    name: color
    prompt:
      select:
        message: "Choose a color:"
        options: ["red", "blue", "green"]
        default: "red"

Then use a loader to bind them to a questions struct:


func main() {
    qs ,err: LoadFromYaml(somePath)
    if err != nil {
      log.Fatal(err);
    }
    // the answers will be written to this struct
    answers := struct {
        Name          string                  // survey will match the question and field names
        FavoriteColor string `survey:"color"` // or you can tag fields to match a specific name
        Age           int                     // if the types don't match exactly, survey will try to convert for you
    }{}

    // perform the questions
    err := survey.Ask(qs, &answers)
    if err != nil {
        fmt.Println(err.Error())
        return
    }

    fmt.Printf("%s chose %s.", answers.Name, answers.FavoriteColor)
}
AlecAivazis commented 5 years ago

Hey @theodesp - thanks for submitting this feature request.

Unforunately, I don't think this is a feature that I will add any time soon. In my opinion, it should be done in a layer "above" survey. I think implementing this feature adds a whole family of issues and requests around different file formats (aka, why yaml and not toml, or hsl, or xml, or....). That being said, if you create a library that performs this conversion, I'd gladly link to it in the README.

theodesp commented 5 years ago

Cool. We will leave it as an exercise then.

guumaster commented 4 years ago

Hi @theodesp, inspired by this request I've made a package called surveygen to create questions code from a yaml file. The tool generate questions as well as answers structure to store results.

The package is guumaster/surveygen and you can see an example

@AlecAivazis feel free to add a link to it if you like it.

theodesp commented 4 years ago

Awesome