inspect

package
v0.0.0-...-8635785 Latest Latest
Warning

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

Go to latest
Published: Dec 19, 2018 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultTemplate = template.Must(template.New("default").Parse(`
package {{ .Package }}

{{ range $name, $path := .Imports -}}
import {{ $name }} "{{ $path }}"
{{ end }}

func specs() []cli.Spec {
  return []cli.Spec{
  {{ range .Funcs -}}
    &{{ .FuncNamePriv }}Spec{
      {{ if .HasDefaultOpts -}}
      opt: {{ .DefaultOptsName }},
      {{- end }}
    },
  {{ end }}
  }
}

{{ range .Funcs }}
type {{ .FuncNamePriv }}Spec struct {
  cmd *cli.Cmd
  {{ if .HasOpts -}}
  opt {{ .OptsType }}
  {{- end }}
  args struct {
    {{ range .Args -}}
      arg{{ .Idx }} {{ .Type }}
    {{ end }}
  }
}

func (cmd *{{ .FuncNamePriv }}Spec) Run() {
  {{ .FuncName }}(
  {{- if .HasOpts }}
    cmd.opt,
  {{ end -}}
  {{- range .Args -}}
    {{ if .Variadic -}}
    cmd.args.arg{{ .Idx }}...,
    {{- else -}}
    cmd.args.arg{{ .Idx }},
    {{- end }}
  {{ end -}}
  )
}

func (cmd *{{ .FuncNamePriv }}Spec) Cmd() *cli.Cmd {
  if cmd.cmd != nil {
    return cmd.cmd
  }
  cmd.cmd = &cli.Cmd{
    RawName:   {{ .FuncName | printf "%q" }},
    RawDoc: {{ .Doc | printf "%q" }},
    Args: []*cli.Arg{
      {{ range .Args -}}
      {
        Name: "{{ .Name }}",
        Type: "{{ .Type }}",
        Variadic: {{ .Variadic }},
        Value: &cmd.args.arg{{ .Idx }},
      },
      {{- end }}
    },
    Opts: []*cli.Opt{
      {{ range .Opts -}}
      {
        Key: {{ .Key | printf "%#v" }},
        RawDoc: {{ .Doc | printf "%q" }},
        Value: &cmd.opt.{{ .KeyJoined }},
        DefaultValue: cmd.opt.{{ .KeyJoined }},
        Type: {{ .Type | printf "%q" }},
        Short: {{ .Short | printf "%q" }},
      },
      {{- end }}
    },
  }
  cli.Enrich(cmd.cmd)
  return cmd.cmd
}
{{ end }}
`))

Functions

func Generate

func Generate(pkg *Package, tpl *template.Template) (err error)

func TemplateVars

func TemplateVars(pkg *Package) map[string]interface{}

Types

type Arg

type Arg struct {
	Name     string
	Type     types.Type
	Variadic bool
}

type ErrGofmt

type ErrGofmt error

type Func

type Func struct {
	Name           string
	Package        string
	Doc            string
	Opts           []*Leaf
	OptsType       *types.Named
	HasDefaultOpts bool
	Args           []Arg
}

type Leaf

type Leaf struct {
	// The path to the leaf field, e.g. "Root.Sub.SubOne"
	Key []string
	// The comment attached to the leaf, e.g. "Comment for SubOne field."
	Doc  string
	Type types.Type
	Tag  string
}

Leaf holds information about a leaf in a tree of struct fields. For example:

type Root struct {
  RootOne string
  Sub struct {
    // Comment for SubOne field.
    SubOne string
  }
}

Root.RootOne and Root.Sub.SubOne are leaves.

type Package

type Package struct {
	Name  string
	Path  string
	Dir   string
	Funcs []*Func
}

func Inspect

func Inspect(packages []string) (*Package, error)

Jump to

Keyboard shortcuts

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