cmd

package
v0.0.0-...-ea67977 Latest Latest
Warning

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

Go to latest
Published: Feb 6, 2021 License: Apache-2.0 Imports: 20 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ConfigureBashCompletion = &cli.Command{
	Hidden: true,
	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{Name: "template-path", Validate: 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(cmd *cli.Command, args []string) {
		MustValidateArgs(args, []validate.Argument{
			{Name: "template-repo", Validate: validate.UnixPath},
			{Name: "template-tag", Validate: validate.AlphanumericExt},
		})

		MustValidateTemplateDir()

		tlog.SetLogLevel(GetStringFlag(cmd, "log-level"))

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

		targetDir, err := boilr.TemplatePath(templateName)
		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(cmd, "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 err := git.Clone(targetDir, git.CloneOptions{
			URL: host.URL(templateURL),
		}); err != nil {
			exit.Error(fmt.Errorf("download: %s", err))
		}

		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 indicates that the template is invalid.
	ErrTemplateInvalid = errors.New("validate: given 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{
	Hidden: true,
	Use:    "rename <old-template-tag> <new-template-tag>",
	Short:  "Rename a project template",
	Run: func(c *cli.Command, args []string) {
		MustValidateArgs(args, []validate.Argument{
			{Name: "old-template-tag", Validate: validate.UnixPath},
			{Name: "new-template-tag", Validate: 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 := os.Rename(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{
			{Name: "template-path", Validate: validate.UnixPath},
			{Name: "template-tag", Validate: 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{
			{Name: "template-tag", Validate: validate.UnixPath},
			{Name: "target-dir", Validate: validate.UnixPath},
		})

		MustValidateTemplateDir()

		tlog.SetLogLevel(GetStringFlag(cmd, "log-level"))

		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)
	},
}

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

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{
			{Name: "template-path", Validate: 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(cmd *cli.Command, args []string) {
		MustValidateArgs(args, []validate.Argument{})

		shouldntPrettify := GetBoolFlag(cmd, "dont-prettify")
		if shouldntPrettify {
			fmt.Println(boilr.Version)
		} else {
			fmt.Println("Version: ", boilr.Version)
			fmt.Println("Build Date (UTC): ", boilr.BuildDate)
			fmt.Println("Commit:", boilr.Commit)
		}
	},
}

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

GetStringFlag retrieves the named string command-line flag given the command that contains it.

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