本文整理汇总了Golang中github.com/raphael/goa/goagen/codegen.RegisterFlags函数的典型用法代码示例。如果您正苦于以下问题:Golang RegisterFlags函数的具体用法?Golang RegisterFlags怎么用?Golang RegisterFlags使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了RegisterFlags函数的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: Generate
// Generate is the generator entry point called by the meta generator.
func Generate(api *design.APIDefinition) (_ []string, err error) {
var genfiles []string
cleanup := func() {
for _, f := range genfiles {
os.Remove(f)
}
}
go utils.Catch(nil, cleanup)
defer func() {
if err != nil {
cleanup()
}
}()
app := kingpin.New("Swagger generator", "Swagger spec generator")
codegen.RegisterFlags(app)
_, err = app.Parse(os.Args[1:])
if err != nil {
return nil, fmt.Errorf(`invalid command line: %s. Command line was "%s"`,
err, strings.Join(os.Args, " "))
}
s, err := New(api)
if err != nil {
return
}
b, err := json.Marshal(s)
if err != nil {
return
}
swaggerDir := filepath.Join(codegen.OutputDir, "swagger")
os.RemoveAll(swaggerDir)
if err = os.MkdirAll(swaggerDir, 0755); err != nil {
return
}
genfiles = append(genfiles, swaggerDir)
swaggerFile := filepath.Join(swaggerDir, "swagger.json")
err = ioutil.WriteFile(swaggerFile, b, 0644)
if err != nil {
return
}
genfiles = append(genfiles, swaggerFile)
controllerFile := filepath.Join(swaggerDir, "swagger.go")
genfiles = append(genfiles, controllerFile)
gg := codegen.NewGoGenerator(controllerFile)
imports := []*codegen.ImportSpec{
codegen.SimpleImport("github.com/julienschmidt/httprouter"),
codegen.SimpleImport("github.com/raphael/goa"),
}
gg.WriteHeader(fmt.Sprintf("%s Swagger Spec", api.Name), "swagger", imports)
gg.Write([]byte(swagger))
if err = gg.FormatCode(); err != nil {
return
}
return genfiles, nil
}
示例2: NewGenerator
// NewGenerator returns the application code generator.
func NewGenerator() (*Generator, error) {
app := kingpin.New("Code generator", "application code generator")
codegen.RegisterFlags(app)
NewCommand().RegisterFlags(app)
_, err := app.Parse(os.Args[1:])
if err != nil {
return nil, fmt.Errorf(`invalid command line: %s. Command line was "%s"`,
err, strings.Join(os.Args, " "))
}
outdir := AppOutputDir()
os.RemoveAll(outdir)
if err = os.MkdirAll(outdir, 0777); err != nil {
return nil, err
}
ctxFile := filepath.Join(outdir, "contexts.go")
ctlFile := filepath.Join(outdir, "controllers.go")
resFile := filepath.Join(outdir, "hrefs.go")
mtFile := filepath.Join(outdir, "media_types.go")
utFile := filepath.Join(outdir, "user_types.go")
ctxWr, err := NewContextsWriter(ctxFile)
if err != nil {
panic(err) // bug
}
ctlWr, err := NewControllersWriter(ctlFile)
if err != nil {
panic(err) // bug
}
resWr, err := NewResourcesWriter(resFile)
if err != nil {
panic(err) // bug
}
mtWr, err := NewMediaTypesWriter(mtFile)
if err != nil {
panic(err) // bug
}
utWr, err := NewUserTypesWriter(utFile)
if err != nil {
panic(err) // bug
}
return &Generator{
GoGenerator: codegen.NewGoGenerator(outdir),
ContextsWriter: ctxWr,
ControllersWriter: ctlWr,
ResourcesWriter: resWr,
MediaTypesWriter: mtWr,
UserTypesWriter: utWr,
contextsFilename: ctxFile,
controllersFilename: ctlFile,
resourcesFilename: resFile,
mediaTypesFilename: mtFile,
userTypesFilename: utFile,
genfiles: []string{outdir},
}, nil
}
示例3: NewGenerator
// NewGenerator returns the application code generator.
func NewGenerator() (*Generator, error) {
app := kingpin.New("Main generator", "application main generator")
codegen.RegisterFlags(app)
NewCommand().RegisterFlags(app)
_, err := app.Parse(os.Args[1:])
if err != nil {
return nil, fmt.Errorf(`invalid command line: %s. Command line was "%s"`,
err, strings.Join(os.Args, " "))
}
return new(Generator), nil
}
示例4: Generate
// Generate is the generator entry point called by the meta generator.
func Generate(api *design.APIDefinition) ([]string, error) {
app := kingpin.New("Swagger generator", "Swagger spec generator")
codegen.RegisterFlags(app)
_, err := app.Parse(os.Args[1:])
if err != nil {
return nil, fmt.Errorf(`invalid command line: %s. Command line was "%s"`,
err, strings.Join(os.Args, " "))
}
s, err := New(api)
if err != nil {
return nil, err
}
b, err := json.Marshal(s)
if err != nil {
return nil, err
}
swaggerDir := filepath.Join(codegen.OutputDir, "swagger")
os.RemoveAll(swaggerDir)
if err = os.MkdirAll(swaggerDir, 0755); err != nil {
return nil, err
}
swaggerFile := filepath.Join(swaggerDir, "swagger.json")
err = ioutil.WriteFile(swaggerFile, b, 0644)
if err != nil {
return nil, err
}
controllerFile := filepath.Join(swaggerDir, "swagger.go")
tmpl, err := template.New("swagger").Parse(swaggerTmpl)
if err != nil {
panic(err.Error()) // bug
}
gg := codegen.NewGoGenerator(controllerFile)
imports := []*codegen.ImportSpec{
codegen.SimpleImport("github.com/raphael/goa"),
}
gg.WriteHeader(fmt.Sprintf("%s Swagger Spec", api.Name), "swagger", imports)
data := map[string]interface{}{
"spec": string(b),
}
err = tmpl.Execute(gg, data)
if err != nil {
return nil, err
}
if err := gg.FormatCode(); err != nil {
return nil, err
}
return []string{controllerFile, swaggerFile}, nil
}
示例5: NewGenerator
// NewGenerator returns the application code generator.
func NewGenerator() (*Generator, error) {
app := kingpin.New("Code generator", "application code generator")
codegen.RegisterFlags(app)
NewCommand().RegisterFlags(app)
_, err := app.Parse(os.Args[1:])
if err != nil {
return nil, fmt.Errorf(`invalid command line: %s. Command line was "%s"`,
err, strings.Join(os.Args, " "))
}
outdir := AppOutputDir()
os.RemoveAll(outdir)
if err = os.MkdirAll(outdir, 0777); err != nil {
return nil, err
}
return &Generator{
genfiles: []string{outdir},
}, nil
}
示例6: command
// command parses the command line and returns the specified sub-command.
func command() codegen.Command {
app := kingpin.New("codegen", "goa code generation tool")
app.Version(codegen.Version)
app.Help = help
codegen.RegisterFlags(app)
for _, c := range Commands {
cmd := app.Command(c.Name(), c.Description())
c.RegisterFlags(cmd)
}
codegen.CommandName = kingpin.MustParse(app.Parse(os.Args[1:]))
for _, c := range Commands {
if codegen.CommandName == c.Name() {
return c
}
}
app.Usage(os.Args[1:])
os.Exit(1)
return nil
}
示例7: command
// command parses the command line and returns the specified sub-command.
func command() codegen.Command {
app := kingpin.New("goagen", "goa code generation tool")
app.Version(codegen.Version)
app.Help = help
codegen.RegisterFlags(app)
for _, c := range Commands {
cmd := app.Command(c.Name(), c.Description())
c.RegisterFlags(cmd)
}
if os.Args[len(os.Args)-1] == "--help" {
args := append([]string{os.Args[0], "help"}, os.Args[1:len(os.Args)-1]...)
os.Args = args
}
codegen.CommandName = kingpin.MustParse(app.Parse(os.Args[1:]))
for _, c := range Commands {
if codegen.CommandName == c.Name() {
return c
}
}
app.Usage(os.Args[1:])
os.Exit(1)
return nil
}
示例8:
})
})
Context("with command line flags", func() {
var kapp *kingpin.Application
var cmd *kingpin.CmdClause
const flagVal = "testme"
var args []string
var parsedCmd string
BeforeEach(func() {
kapp = kingpin.New("test", "test")
cmd = kapp.Command("testCmd", "testCmd")
args = []string{testCmd, "-o" + flagVal, "-d=design", "--pkg=dummy"}
})
JustBeforeEach(func() {
codegen.RegisterFlags(cmd)
appCmd.RegisterFlags(cmd)
var err error
parsedCmd, err = kapp.Parse(args)
Ω(err).ShouldNot(HaveOccurred())
})
It("parses the default flags", func() {
Ω(parsedCmd).Should(Equal(testCmd))
Ω(codegen.OutputDir).Should(Equal(flagVal))
})
})
})