initialization

package
v1.2.84 Latest Latest
Warning

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

Go to latest
Published: Jan 17, 2024 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Command = &cli.Command{
	Name:        "init",
	Aliases:     nil,
	Usage:       "fns init --mod={mod} --img={docker image name} --work={true} --version={go version} {project dir}",
	Description: "init fns project",
	ArgsUsage:   "",
	Category:    "",
	Flags: []cli.Flag{
		&cli.StringFlag{
			Name:     "mod",
			Aliases:  []string{"m"},
			Required: true,
			Usage:    "project go mod path",
		},
		&cli.BoolFlag{
			Name:     "work",
			Aliases:  []string{"w"},
			Required: false,
			Usage:    "use go work",
		},
		&cli.StringFlag{
			Name:     "img",
			Aliases:  []string{"i"},
			Required: false,
			Usage:    "project docker image name",
		},
		&cli.StringFlag{
			Name:     "version",
			Required: false,
			Usage:    "go version, e.g.: 1.21.0",
		},
	},
	Action: func(ctx *cli.Context) (err error) {
		projectDir := strings.TrimSpace(ctx.Args().First())
		if projectDir == "" {
			projectDir = "."
		}
		if !filepath.IsAbs(projectDir) {
			projectDir, err = filepath.Abs(projectDir)
			if err != nil {
				err = errors.Warning("fns: init fns project failed").WithCause(err).WithMeta("dir", projectDir)
				return
			}
		}
		projectDir = filepath.ToSlash(projectDir)
		projectPath := strings.TrimSpace(ctx.String("mod"))
		img := strings.TrimSpace(ctx.String("img"))
		work := ctx.Bool("work")
		goVersion := ctx.String("version")
		if goVersion == "" {
			goVersion = runtime.Version()[2:]
		}
		if !ValidGoVersion(goVersion) {
			err = errors.Warning("fns: init fns project failed").WithCause(fmt.Errorf("go version must be gte %s", runtime.Version())).WithMeta("dir", projectDir).WithMeta("path", projectPath)
			return
		}
		writeErr := base.Write(ctx.Context, goVersion, projectPath, img, work, projectDir)
		if writeErr != nil {
			err = errors.Warning("fns: init fns project failed").WithCause(writeErr).WithMeta("dir", projectDir).WithMeta("path", projectPath)
			return
		}
		if work {
			fmt.Println("fns: project has been initialized, please run `go work sync` and `go mod tidy` to fetch requires and run `go generate` to generate source files!")
		} else {
			fmt.Println("fns: project has been initialized, please run `go mod tidy` to fetch requires and run `go generate` to generate source files!")
		}
		return
	},
}

Functions

func ValidGoVersion added in v1.2.82

func ValidGoVersion(target string) (ok bool)

Types

type Version added in v1.2.82

type Version struct {
	Major int
	Miner int
	Patch int
}

func ParseGoVersion added in v1.2.82

func ParseGoVersion(s string) (v Version)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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