cmds

package
v0.3.6 Latest Latest
Warning

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

Go to latest
Published: Apr 25, 2018 License: Apache-2.0 Imports: 10 Imported by: 2

Documentation

Index

Constants

This section is empty.

Variables

View Source
var RootCMD = &cobra.Command{
	Use:   "snips",
	Short: "A code generator for RESTful APIs.",
	Long: `A code generator for RESTful APIs.

For example:
  $ snips -f ./specs/qingstor/api.json
          -t ./templates/qingstor/go \
          -o ./publish/qingstor-sdk-go/service
  $ ...
  $ snips --file=./specs/qingstor/api.json \
          --template=./templates/qingstor/ruby \
          --output=./publish/qingstor-sdk-ruby/lib/qingstor/sdk/service
  $ ...

Copyright (C) 2016-2017 Yunify, Inc.`,
	PreRunE: func(cmd *cobra.Command, args []string) error {
		if flagVersion {
			return nil
		}

		if codeSpecFile == "" {
			return errors.New("please specify the spec file")
		}
		if _, err := os.Stat(codeSpecFile); err != nil {
			return errors.New("please make sure the spec file exists")
		}

		if codeTemplateDirectory == "" {
			return errors.New("please specify templates directory")
		}
		if _, err := os.Stat(codeTemplateDirectory); err != nil {
			return errors.New("please make sure the templates directory exists")
		}

		if codeOutputDirectory == "" {
			return errors.New("please specify output files directory")
		}

		return nil
	},
	Run: func(cmd *cobra.Command, args []string) {
		if flagVersion {
			fmt.Println("snips version " + constants.Version)
			return
		}

		loadedTemplates, manifest, err := templates.LoadTemplates(codeTemplateDirectory)
		utils.CheckErrorForExit(err)
		fmt.Println("Loaded templates from " + codeTemplateDirectory)
		fmt.Println(len(loadedTemplates), "template(s) detected.")

		spec, err := specs.LoadSpec(codeSpecFile, codeSpecFormat)
		utils.CheckErrorForExit(err)
		fmt.Printf("Loaded specification file %s (%s)\n\n", codeSpecFile, codeSpecFormat)

		if spec != nil {
			codeCapsule := &capsules.BaseCapsule{CapsulePowder: &capsules.CapsulePowder{}}
			codeGenerator := generator.New()

			if manifest.MetaData != nil {
				spec.Data.MetaData = manifest.MetaData
			}
			codeCapsule.SetData(spec.Data)

			sharedTemplateContent := ""
			if template := loadedTemplates["shared"]; template != nil {
				sharedTemplateContent = template.FileContent
			}

			if template := loadedTemplates["service"]; template != nil {
				template.FileContent += sharedTemplateContent
				template.UpdateOutputFilename(spec.Data.Service.Name, template.OutputFileNaming.Style)
				template.UpdateOutputFilePath(codeOutputDirectory)
				codeCapsule.SetMode("", "")
				codeGenerator.Set(codeCapsule, template)
				err = codeGenerator.Run()
				utils.CheckErrorForExit(err)
			}

			if template := loadedTemplates["sub_service"]; template != nil {
				template.FileContent += sharedTemplateContent
				for _, subService := range spec.Data.SubServices {
					template.UpdateOutputFilename(subService.Name, template.OutputFileNaming.Style)
					template.UpdateOutputFilePath(codeOutputDirectory)
					codeCapsule.SetMode(template.ID, subService.ID)
					codeGenerator.Set(codeCapsule, template)
					err = codeGenerator.Run()
					utils.CheckErrorForExit(err)
				}
			}

			if template := loadedTemplates["types"]; template != nil {
				template.FileContent += sharedTemplateContent
				template.UpdateOutputFilename("types", template.OutputFileNaming.Style)
				template.UpdateOutputFilePath(codeOutputDirectory)
				codeCapsule.SetMode("", "")
				codeGenerator.Set(codeCapsule, template)
				err = codeGenerator.Run()
				utils.CheckErrorForExit(err)
			}
		}

		fmt.Println("\nEverything looks fine.")
	},
}

RootCMD represents the base command when called without any subcommands.

Functions

func Execute

func Execute()

Execute adds all child commands to the root command sets flags appropriately. This is called by entrance of the application. It only needs to happen once to the rootCMD.

Types

This section is empty.

Jump to

Keyboard shortcuts

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