cmd

package
v0.0.0-...-0c3937f Latest Latest
Warning

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

Go to latest
Published: Sep 13, 2022 License: Apache-2.0 Imports: 21 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ConfigureBashCompletion = &cli.Command{
	Use:   "configure-bash-completion",
	Short: "Configure bash the auto-completion",
	Run: func(c *cli.Command, _ []string) {
		if err := configureBashCompletion(); err != nil {
			exit.Fatal(fmt.Errorf("configure-bash-completion: %s", err))
		}

		exit.OK("Successfully configured bash auto-completion")
	},
}

ConfigureBashCompletion generates bash auto-completion script and installs it.

View Source
var Delete = &cli.Command{
	Use:   "delete <template-tag>",
	Short: "Delete a project template from the template registry",
	Run: func(c *cli.Command, args []string) {
		MustValidateVarArgs(args, validate.Argument{"template-path", validate.AlphanumericExt})

		MustValidateTemplateDir()

		for _, templateName := range args {
			targetDir := filepath.Join(boilr.Configuration.TemplateDirPath, templateName)

			switch exists, err := osutil.DirExists(targetDir); {
			case err != nil:
				tlog.Error(fmt.Sprintf("delete: %s", err))
				continue
			case !exists:
				tlog.Error(fmt.Sprintf("Template %v doesn't exist", templateName))
				continue
			}

			if err := os.RemoveAll(targetDir); err != nil {
				tlog.Error(fmt.Sprintf("delete: %v", err))
				continue
			}

			tlog.Success(fmt.Sprintf("Successfully deleted the template %v", templateName))
		}
	},
}

Delete contains the cli-command for deleting templates.

View Source
var Download = &cli.Command{
	Use:   "download <template-repo> <template-tag>",
	Short: "Download a project template from a github repository to template registry",

	Run: func(c *cli.Command, args []string) {
		MustValidateArgs(args, []validate.Argument{
			{"template-repo", validate.UnixPath},
			{"template-tag", validate.AlphanumericExt},
		})

		MustValidateTemplateDir()

		templateSubFolder := GetStringFlag(c, "sub-path")
		templateRemoteBranch := GetStringFlag(c, "branch")
		templateURL, templateName := args[0], args[1]
		targetDir, err := boilr.TemplatePath(templateName)
		targetTmpDir := targetDir

		if err != nil {
			exit.Error(fmt.Errorf("download: %s", err))
		}

		switch exists, err := osutil.DirExists(targetDir); {
		case err != nil:
			exit.Error(fmt.Errorf("download: %s", err))
		case exists:
			if shouldOverwrite := GetBoolFlag(c, "force"); !shouldOverwrite {
				exit.OK("Template %v already exists use -f to overwrite the template", templateName)
			}

			if err := os.RemoveAll(targetDir); err != nil {
				exit.Error(fmt.Errorf("download: %s", err))
			}
		}

		if templateSubFolder != "" {
			targetTmpDir, err = boilr.TemplateTempPath(templateName)
			if err != nil {
				exit.Error(fmt.Errorf("download: %s", err))
			}
			exists, err := osutil.DirExists(targetTmpDir)
			if exists || (!exists && err != nil) {
				if err := os.RemoveAll(targetTmpDir); err != nil {
					exit.Error(fmt.Errorf("download: %s", err))
				}
			}
		}

		gitCloneOptions := git.CloneOptions{
			URL: host.URL(templateURL),
		}
		if templateRemoteBranch != "" {
			gitCloneOptions.ReferenceName = plumbing.NewBranchReferenceName(templateRemoteBranch)
			gitCloneOptions.SingleBranch = true
		}
		if err := git.Clone(targetTmpDir, gitCloneOptions); err != nil {
			exit.Error(fmt.Errorf("download: Cloning repo - %s", err))
		}

		if templateSubFolder != "" {

			templateTmpDir := osutil.JoinPaths(targetTmpDir, templateSubFolder)
			exists, err := osutil.DirExists(templateTmpDir)
			if err != nil {
				exit.Error(fmt.Errorf("download: %s", err))
			}
			if !exists {
				exit.Error(fmt.Errorf("download: sub-folder doesn't exist"))
			}

			if exists, err = osutil.DirExists(targetDir); err != nil {
				exit.Error(fmt.Errorf("download: %s", err))
			}
			if !exists {
				if err = osutil.CreateDirs(targetDir); err != nil {
					exit.Error(fmt.Errorf("download: %s", err))
				}
			}
			if err = osutil.CopyRecursively(templateTmpDir, targetDir); err != nil {
				exit.Error(fmt.Errorf("download: Error copying files from temp %s", err))
			}

			if err := os.RemoveAll(targetTmpDir); err != nil {
				exit.Error(fmt.Errorf("download: Error deleting temp files %s", err))
			}
		}

		exists, err := osutil.DirExists(osutil.JoinPaths(targetDir, boilr.TemplateDirName))
		if err != nil {
			exit.Error(fmt.Errorf("download: Template error - %s", err))
		}
		if !exists {
			exit.Error(fmt.Errorf("download: Invalid template. Folder '%s' - doesn't exist at %s", boilr.TemplateDirName, targetDir))
		}

		if err := serializeMetadata(templateName, templateURL, targetDir); err != nil {
			exit.Error(fmt.Errorf("download: %s", err))
		}

		exit.OK("Successfully downloaded the template %#v", templateName)
	},
}

Download contains the cli-command for downloading templates from github.

View Source
var ErrTemplateInvalid = errors.New("validate: given template is invalid")

ErrTemplateInvalid indicates that the template is invalid.

View Source
var Init = &cli.Command{
	Use:   "init",
	Short: "Initialize directories required by boilr (By default done by installation script)",
	Run: func(c *cli.Command, _ []string) {

		if exists, err := osutil.DirExists(boilr.Configuration.TemplateDirPath); exists {
			if shouldRecreate := GetBoolFlag(c, "force"); !shouldRecreate {
				exit.GoodEnough("template registry is already initialized use -f to reinitialize")
			}
		} else if err != nil {
			exit.Error(fmt.Errorf("init: %s", err))
		}

		if err := osutil.CreateDirs(boilr.Configuration.TemplateDirPath); err != nil {
			exit.Error(err)
		}

		exit.OK("Initialization complete")
	},
}

Init contains the cli-command for initializing the local template registry in case it's not initialized.

View Source
var List = &cli.Command{
	Use:   "list <template-path> <template-tag>",
	Short: "List project templates found in the local template registry",
	Run: func(cmd *cli.Command, args []string) {
		MustValidateArgs(args, []validate.Argument{})

		MustValidateTemplateDir()

		templateNames, err := ListTemplates()
		if err != nil {
			exit.Error(fmt.Errorf("list: %s", err))
		}

		names := []string{}
		for name := range templateNames {
			names = append(names, name)
		}
		sort.Strings(names)

		var data [][]string
		for _, name := range names {
			tmplPath, err := boilr.TemplatePath(name)
			if err != nil {
				exit.Fatal(fmt.Errorf("list: %s", err))
			}

			tmpl, err := template.Get(tmplPath)
			if err != nil {
				exit.Fatal(fmt.Errorf("list: %s", err))
			}

			data = append(data, tmpl.Info().String())
		}

		if GetBoolFlag(cmd, "dont-prettify") {
			fmt.Println(strings.Join(names, " "))
		} else {
			tabular.Print([]string{"Tag", "Repository", "Created"}, data)
		}
	},
}

List contains the cli-command for printing a list of saved templates.

View Source
var Rename = &cli.Command{
	Use:   "rename <old-template-tag> <new-template-tag>",
	Short: "Rename a project template",
	Run: func(c *cli.Command, args []string) {
		MustValidateArgs(args, []validate.Argument{
			{"old-template-tag", validate.UnixPath},
			{"new-template-tag", validate.UnixPath},
		})

		MustValidateTemplateDir()

		tmplName, newTmplName := args[0], args[1]

		if ok, err := TemplateInRegistry(tmplName); err != nil {
			exit.Fatal(fmt.Errorf("rename: %s", err))
		} else if !ok {
			exit.Fatal(fmt.Errorf("Template %q couldn't be found in the template registry", tmplName))
		}

		tmplPath, err := boilr.TemplatePath(tmplName)
		if err != nil {
			exit.Fatal(fmt.Errorf("rename: %s", err))
		}

		newTmplPath, err := boilr.TemplatePath(newTmplName)
		if err != nil {
			exit.Fatal(fmt.Errorf("rename: %s", err))
		}

		if err := renameTemplate(tmplPath, newTmplPath); err != nil {
			exit.Error(fmt.Errorf("rename: %s", err))
		}

		exit.OK("Successfully renamed the template %q to %q", tmplName, newTmplName)
	},
}

Rename contains the cli-command for renaming templates in the template registry.

View Source
var Root = &cli.Command{
	Use: "boilr",
}

Root contains the root cli-command containing all the other commands.

View Source
var Save = &cli.Command{
	Use:   "save <template-path> <template-tag>",
	Short: "Save a local project template to template registry",
	Run: func(c *cli.Command, args []string) {
		MustValidateArgs(args, []validate.Argument{
			{"template-path", validate.UnixPath},
			{"template-tag", validate.AlphanumericExt},
		})

		MustValidateTemplateDir()

		tmplDir, templateName := args[0], args[1]

		MustValidateTemplate(tmplDir)

		targetDir := filepath.Join(boilr.Configuration.TemplateDirPath, templateName)

		switch exists, err := osutil.DirExists(targetDir); {
		case err != nil:
			exit.Error(fmt.Errorf("save: %s", err))
		case exists:
			shouldOverwrite := GetBoolFlag(c, "force")

			if err != nil {
				exit.Error(fmt.Errorf("save: %v", err))
			}

			if !shouldOverwrite {
				exit.OK("Template %v already exists use -f to overwrite", templateName)
			}

			if err := os.RemoveAll(targetDir); err != nil {
				exit.Error(fmt.Errorf("save: %v", err))
			}
		}

		if _, err := exec.Cmd("cp", "-r", tmplDir, targetDir); err != nil {
			exit.Error(err)
		}

		absTemplateDir, err := filepath.Abs(tmplDir)
		if err != nil {
			exit.Error(err)
		}

		if err := serializeMetadata(templateName, "local:"+absTemplateDir, targetDir); err != nil {
			exit.Error(fmt.Errorf("save: %s", err))
		}

		exit.OK("Successfully saved the template %v", templateName)
	},
}

Save contains the cli-command for saving templates to template registry.

View Source
var Use = &cli.Command{
	Use:   "use <template-tag> <target-dir>",
	Short: "Execute a project template in the given directory",
	Run: func(cmd *cli.Command, args []string) {
		MustValidateArgs(args, []validate.Argument{
			{"template-tag", validate.UnixPath},
			{"target-dir", validate.UnixPath},
		})

		MustValidateTemplateDir()

		tmplName := args[0]
		targetDir, err := filepath.Abs(args[1])
		if err != nil {
			exit.Fatal(fmt.Errorf("use: %s", err))
		}

		templateFound, err := TemplateInRegistry(tmplName)
		if err != nil {
			exit.Fatal(fmt.Errorf("use: %s", err))
		}

		if !templateFound {
			exit.Fatal(fmt.Errorf("Template %q couldn't be found in the template registry", tmplName))
		}

		tmplPath, err := boilr.TemplatePath(tmplName)
		if err != nil {
			exit.Fatal(fmt.Errorf("use: %s", err))
		}

		tmpl, err := template.Get(tmplPath)
		if err != nil {
			exit.Fatal(fmt.Errorf("use: %s", err))
		}

		if shouldUseDefaults := GetBoolFlag(cmd, "use-defaults"); shouldUseDefaults {
			tmpl.UseDefaultValues()
		}

		executeTemplate := func() error {
			parentDir := filepath.Dir(targetDir)

			exists, err := osutil.DirExists(parentDir)
			if err != nil {
				return err
			}

			if !exists {
				return fmt.Errorf("use: parent directory %q doesn't exist", parentDir)
			}

			tmpDir, err := ioutil.TempDir("", "boilr-use-template")
			if err != nil {
				return err
			}
			defer os.RemoveAll(tmpDir)

			if err := tmpl.Execute(tmpDir); err != nil {
				return err
			}

			return osutil.CopyRecursively(tmpDir, targetDir)
		}

		if err := executeTemplate(); err != nil {
			exit.Fatal(fmt.Errorf("use: %v", err))
		}

		exit.OK("Successfully executed the project template %v in %v", tmplName, targetDir)
	},
}

TODO add --use-cache flag to execute a template from previous answers to prompts Use contains the cli-command for using templates located in the local template registry.

View Source
var Validate = &cli.Command{
	Use:   "validate <template-path>",
	Short: "Validate a local project template",
	Run: func(_ *cli.Command, args []string) {
		MustValidateArgs(args, []validate.Argument{
			{"template-path", validate.UnixPath},
		})

		templatePath := args[0]

		MustValidateTemplate(templatePath)

		exit.OK("Template is valid")
	},
}

Validate contains the cli-command for validating templates.

View Source
var Version = &cli.Command{
	Use:   "version",
	Short: "Show the boilr version information",
	Run: func(c *cli.Command, args []string) {
		MustValidateArgs(args, []validate.Argument{})

		shouldntPrettify := GetBoolFlag(c, "dont-prettify")
		if shouldntPrettify {
			fmt.Println(boilr.Version)
		} else {
			tlog.Info(fmt.Sprint("This is a forked version of boilr from github.com/Li-AnLin/boilr!"))
			tlog.Info(fmt.Sprint("You are running Git Commit Hash: ", boilr.Version))
			tlog.Info(fmt.Sprint("See https://github.com/Li-AnLin/boilr/commit/", boilr.Version))
		}
	},
}

Version contains the cli-command for printing the current version of the tool.

Functions

func GetBoolFlag

func GetBoolFlag(c *cli.Command, name string) bool

GetBoolFlag retrieves the named boolean command-line flag given the command that contains it.

func GetStringFlag

func GetStringFlag(c *cli.Command, name string) string

func ListTemplates

func ListTemplates() (map[string]bool, error)

ListTemplates returns a list of templates saved in the local template registry.

func MustValidateArgs

func MustValidateArgs(args []string, validations []validate.Argument)

MustValidateArgs validates given arguments with the supplied validation functions. If there are any errors it exits the execution.

func MustValidateTemplate

func MustValidateTemplate(path string)

MustValidateTemplate validates a template given it's absolut path. If there are any errors it exits the execution.

func MustValidateTemplateDir

func MustValidateTemplateDir()

MustValidateTemplateDir ensures that template directory is initialized.

func MustValidateVarArgs

func MustValidateVarArgs(args []string, v validate.Argument)

MustValidateVarArgs validates given variadic arguments with the supplied validation function. If there are any errors it exits the execution.

func Run

func Run()

Run executes the cli-command root.

func TemplateInRegistry

func TemplateInRegistry(name string) (bool, error)

TemplateInRegistry checks whether the given name exists in the template registry.

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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