generator

package
v0.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 16, 2020 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

View Source
const (
	SurveyTmpl = `
// Package {{ .PackageName }} is an autogenerated survey ready to use with "github.com/AlecAivazis/survey/v2".
// Code generated by github.com/guumaster/surveygen, DO NOT EDIT.
// source: {{ .SourceFile }}
package {{ .PackageName }}

/*
  Example Usage:

	func main() {
		s := {{ .PackageName }}.Create{{ .Name }}()

		err := survey.Ask(s.Questions, s.Answers)
		if err != nil {
			log.Fatal(err)
		}
		fmt.Println("Answers: \n", s.Answers)
	}
*/

import (
	"github.com/AlecAivazis/survey/v2"
)

// Survey{{ .Name }} is an autogenerated struct for survey "{{ .Name }}".
type Survey{{ .Name }} struct {
  Questions []*survey.Question
  Answers *Answers{{ .Name }}
}

// Answers{{ .Name }} is a struct that will contain survey's answers.
type Answers{{ .Name }}  struct {
{{- range .Questions }}
  {{ .Name  | trim | title -}}
  {{ if ne .AnswerType ""  }} {{ .AnswerType | lower }} {{ else }} {{ .QuestionType | lower }} {{ end -}}
  ` + "`survey:{{ .Name  | quote }}`" + `
{{- end }}
}

// Create{{ .Name }} creates a Survey{{ .Name }} struct with .Questions and .Answers ready to use with survey.Ask()
func Create{{ .Name }}() *Survey{{ .Name }} {
	return &Survey{{ .Name }} {
	  Answers: &Answers{{ .Name }}{},
		Questions: []*survey.Question{
{{ range .Questions -}}
   {{ $Validate := "" }}
   {{- if or .Required $.AllRequired -}}
   {{ $Validate = "Validate: survey.Required," }}
   {{- end -}}
   {{- if eq .InputType "input" -}}
   {
     Name:   {{ .Name | quote }},
     Prompt:  &survey.Input{
       Message: {{ .Prompt | quote }},
       {{ if .Help }}Help: {{ .Help | quote }},{{end}}
       {{ if .Default }}Default: {{ .Default | quote }},{{end}}
     },
     {{ if ne $Validate "" }}{{ print $Validate }}{{ end }}
   },
   {{ end -}}
   {{- if eq .InputType "password" -}}
   {
     Name:   {{ .Name | quote }},
     Prompt:  &survey.Password{
       Message: {{ .Prompt | quote }},
       {{ if .Help }}Help: {{ .Help | quote }},{{end}}
       {{ if .Default }}Default: {{ .Default | quote }},{{end}}
     },
     {{ if ne $Validate "" }}{{ print $Validate }}{{ end }}
   },
   {{- end -}}
   {{- if eq .InputType "confirm" -}}
   {
     Name:   {{ .Name | quote }},
     Prompt:  &survey.Confirm{
       Message: {{ .Prompt | quote }},
       {{ if .Help }}Help: {{ .Help | quote }},{{end}}
       {{ if .Default }}Default: {{ .Default  }},{{end}}
     },
     {{ if ne $Validate "" }}{{ print $Validate }}{{ end }}
   },
   {{- end -}}
   {{- if eq .InputType "select" -}}
   {
     Name:   {{ .Name | quote }},
     Prompt:  &survey.Select{
       Message: {{ .Prompt | quote }},
       {{ if .Help }}Help: {{ .Help | quote }},{{- end}}
       {{ if .Default }}Default: {{ .Default | quote }},{{- end -}}
       Options: []string{
       {{- range .Options }}
         {{ . | quote }},
       {{- end}}
       },
     },
     {{ if ne $Validate "" }}{{ print $Validate }}{{ end }}
   },
   {{- end -}}
   {{- if eq .InputType "multiline" -}}
   {
     Name:   {{ .Name | quote }},
     Prompt: &survey.Multiline{
       Message: {{ .Prompt | quote }},
       {{ if .Help }}Help: {{ .Help | quote }},{{end}}
       {{ if .Default }}Default: {{ .Default | quote }},{{end}}
     },
     {{ if ne $Validate "" }}{{ print $Validate }}{{ end }}
   },
   {{- end -}}
   {{- if eq .InputType "multiselect" -}}
   {
     Name: {{ .Name | quote }},
     Prompt: &survey.MultiSelect{
       Message: {{ .Prompt | quote }},
       {{ if .Help }}Help: {{ .Help | quote }},{{- end}}
       {{ if .Default }}Default: {{ .Default | quote }},{{- end}}
       Options: []string{
       {{- range .Options }}
         {{ . | quote }},
       {{- end}}
       },
     },
     {{ if ne $Validate "" }}{{ print $Validate }}{{ end }}
   },
   {{ end -}}
{{ end }}
   },
	}
}
`
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Generator

type Generator struct {
	// contains filtered or unexported fields
}

func New

func New() *Generator

func (*Generator) Generate

func (g *Generator) Generate(root string) error

type Question

type Question struct {
	Name         string `yaml:"name"`
	InputType    string `yaml:"type"`
	QuestionType string
	AnswerType   string   `yaml:"answer"`
	Help         string   `yaml:"help"`
	Required     bool     `yaml:"required"`
	Prompt       string   `yaml:"prompt"`
	Default      string   `yaml:"default"`
	Options      []string `yaml:"options"`
}

type Survey

type Survey struct {
	SourceFile  string
	Name        string      `yaml:"name"`
	PackageName string      `yaml:"packageName"`
	Questions   []*Question `yaml:"questions"`
	AllRequired bool        `yaml:"allRequired"`
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL