cmd

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Sep 14, 2023 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Gen = &cli.Command{
	Name:  "gen",
	Usage: "generate all errors according to blunder.yaml",
	Flags: []cli.Flag{
		&cli.BoolFlag{
			Name:    "complete",
			Value:   false,
			Usage:   "auto complete error detail with gpt3",
			Aliases: []string{"c"},
		},
	},
	Action: func(cCtx *cli.Context) error {
		sGen := util.PrintLoading("generating all errors...", "all errors are generated successfully")

		blunderPath := util.LocateBlunderYamlPath()
		if blunderPath == "" {
			return util.PrintErrAndReturn("blunder.yaml not found, please init the project first")
		}
		blunderRootPath := util.GetFileDirPath(blunderPath)

		f, err := os.ReadFile(blunderPath)
		if err != nil {
			return util.PrintErrAndReturn(err.Error())
		}

		var blunderConfig model.Blunder
		if err := yaml.Unmarshal(f, &blunderConfig); err != nil {
			return util.PrintErrAndReturn(err.Error())
		}

		if !checkAllErrorCodeIsProvided(&blunderConfig) {
			return util.PrintErrAndReturn("in blunder.yaml, all error code must be provided")
		}

		complete := cCtx.Bool("complete")
		if complete {

			_, exist := os.LookupEnv("OPENAI_API_TOKEN")
			if !exist {
				return util.PrintErrAndReturn("OPENAI_API_TOKEN not found, please export with your own openai api token")
			}

			sComplete := util.PrintLoading("auto-completing error detail with gpt3...", "error detail is completed successfully")
			hasSomethingToComplete, indexMap, errorCodes := determineWhichToComplete(&blunderConfig)
			if hasSomethingToComplete {
				completed, err := gpt.CompleteErrorDetail(errorCodes)
				if err != nil {
					return util.PrintErrAndReturn(err.Error())
				}

				for _, ec := range completed {
					blunderConfig.
						Details[indexMap[ec.Code].DetailIndex].
						Errors[indexMap[ec.Code].ErrorIndex].
						HttpStatusCode = ec.HttpStatusCode
					blunderConfig.
						Details[indexMap[ec.Code].DetailIndex].
						Errors[indexMap[ec.Code].ErrorIndex].
						GrpcStatusCode = ec.GrpcStatusCode
					blunderConfig.
						Details[indexMap[ec.Code].DetailIndex].
						Errors[indexMap[ec.Code].ErrorIndex].
						Message = ec.Message
				}
			}

			if err := template.Generate(blunderPath, constant.GenerateBlunderYamlTemplateFileName, &blunderConfig); err != nil {
				return util.PrintErrAndReturn(err.Error())
			}
			sComplete.Stop()
		}

		generatedRootPath := filepath.Join(blunderRootPath, constant.GeneratedDirName)
		clearGeneratedFolder(generatedRootPath)

		for _, detail := range blunderConfig.Details {
			errorFilePath := filepath.Join(generatedRootPath, detail.Package, constant.ErrorFileName)

			if err := template.Generate(errorFilePath, constant.ErrorFileTemplateFileName, &detail); err != nil {
				return util.PrintErrAndReturn(err.Error())
			}
		}

		sGen.Stop()
		return nil
	},
}
View Source
var Init = &cli.Command{
	Name:  "init",
	Usage: "initiate blunder configuration file and module structure",
	Action: func(cCtx *cli.Context) error {
		initPath := cCtx.Args().Get(0)
		if initPath == "" {
			return util.PrintErrAndReturn("you should specify the path to init")
		}

		s := util.PrintLoading("generating blunder.yaml ...", "blunder.yaml is generated successfully")
		blunderYamlPath := filepath.Join(initPath, "errors", constant.BlunderYamlFileName)
		if err := template.Generate(blunderYamlPath, constant.BlunderYamlTemplateFileName, nil); err != nil {
			return util.PrintErrAndReturn(err.Error())
		}
		s.Stop()

		return nil
	},
}

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

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