cmd

package
v3.0.1 Latest Latest
Warning

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

Go to latest
Published: Oct 11, 2021 License: MIT Imports: 19 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var CliColor = &gcli.Command{
	Name:     "color",
	Desc:     "this is a example for cli color usage",
	Aliases:  []string{"clr", "colors"},
	Func:     colorUsage,
	Examples: "{$binName} {$cmd} --id 12 -c val ag0 ag1",
	Config: func(c *gcli.Command) {
		c.IntOpt(&colorOpts.id, "id", "", 2, "the id option")
		c.StrOpt(&colorOpts.c, "c", "", "value", "the config option")
		c.StrOpt(&colorOpts.dir, "dir", "", "", "the dir option")

	},
}

CliColor command definition

View Source
var DaemonRun = &gcli.Command{
	Name:    "bgrun",
	Desc:    "an example for background run program",
	Func:    handleDaemonRun,
	Aliases: []string{"bgr"},
	Config: func(c *gcli.Command) {
		c.BoolOpt(&bgrOpts.deamon, "daemon", "d", false, "want background run")

	},
}
View Source
var EmojiDemo = &gcli.Command{
	Name:    "emoji",
	Desc:    "this is a emoji usage example command",
	Aliases: []string{"emoj"},

	Examples: `
An render example
  {$fullCmd} render ":car: a message text, contains emoji :smile:"
An search example
  {$fullCmd} search smi`,
	Subs: []*gcli.Command{
		{
			Name:    "render",
			Desc:    "render given string, will replace special char to emoji",
			Aliases: []string{"r"},
			Config: func(c *gcli.Command) {
				c.AddArg("msg", "The message string for render", true)
			},
			Func: func(c *gcli.Command, args []string) error {
				fmt.Println(emoji.Render(c.Arg("msg").String()))
				return nil
			},
		},
		{
			Name:    "search",
			Desc:    "search emojis by given keywords",
			Aliases: []string{"s"},
			Config: func(c *gcli.Command) {
				c.AddArg("keyword", "The keyword string for search", true)
			},
			Func: func(c *gcli.Command, args []string) error {
				kw := c.Arg("keyword").String()

				return searchEmoji(kw)
			},
		},
	},
}
View Source
var EnvInfo = &gcli.Command{
	Name:    "env",
	Desc:    "collect project info by git info",
	Aliases: []string{"env-info", "ei"},
	Config: func(c *gcli.Command) {
		c.IntOpt(&eiOpts.id, "id", "", 0, "the id option")
		c.StrOpt(&eiOpts.c, "c", "", "", "the config option")
		c.StrOpt(&eiOpts.dir, "dir", "d", "", "the dir option")

	},

	Func: func(c *gcli.Command, _ []string) error {
		eAble, _ := os.Executable()

		data := map[string]interface{}{
			"os":       runtime.GOOS,
			"binName":  c.BinName(),
			"workDir":  c.WorkDir(),
			"rawArgs":  os.Args,
			"execAble": eAble,
			"env":      os.Environ(),
		}

		show.JSON(&data)
		return nil
	},
}

EnvInfo command

View Source
var Example = &gcli.Command{
	Func:    exampleExecute,
	Name:    "example",
	Aliases: []string{"module-exp", "exp", "ex"},
	Desc:    "this is command description message",

	Examples: `
  {$binName} {$cmd} --id 12 -c val ag0 ag1
  <cyan>{$fullCmd} --names tom --names john -n c</> 	test use special option
`,
	Config: func(c *gcli.Command) {

		c.IntOpt(&exampleOpts.id, "id", "", 2, "the id option")
		c.BoolOpt(&exampleOpts.showErr, "err", "e", false, "display error example")
		c.StrOpt(&exampleOpts.c, "config", "c", "value", "the config option")

		c.StrOpt(&exampleOpts.dir, "dir", "d", "", "the `DIRECTORY` option")

		c.StrOpt(&exampleOpts.opt, "opt", "o", "", "the option message")

		c.VarOpt(&exampleOpts.names, "names", "n", "the option message")

		c.AddArg("arg0", "the first argument, is required", true)
		c.AddArg("arg1", "the second argument, is required", true)
		c.AddArg("arg2", "the optional argument, is optional")
		c.AddArg("arrArg", "the array argument, is array", false, true)

	},
}

Example command definition

View Source
var GitCmd = &gcli.Command{
	Name: "git",
	Desc: "git usage example",
	Subs: []*gcli.Command{
		GitInfo, GitPullMulti, GitRemote,
	},
}
View Source
var GitInfo = &gcli.Command{
	Name: "info",

	Desc: "collect project latest commit info by git log command",
	Config: func(c *gcli.Command) {
		c.IntOpt(&gitOpts.id, "id", "", 0, "the id option")
		c.StrOpt(&gitOpts.c, "c", "", "", "the config option")
		c.StrOpt(&gitOpts.dir, "dir", "d", "", "the dir option")
	},
	Func: gitExecute,
}

GitInfo git info command

View Source
var GitPullMulti = &gcli.Command{
	Name:    "pull",
	Desc:    "use git pull for update multi project",
	Aliases: []string{"pul"},
	Config: func(c *gcli.Command) {
		c.AddArg(
			"basePath",
			"the base operate dir path. default is current dir",
			true,
		).WithValidator(func(v interface{}) (i interface{}, e error) {
			if !fsutil.IsDir(v.(string)) {
				return nil, fmt.Errorf("the base path must be an exist dir")
			}
			return v, nil
		})

		c.AddArg(
			"dirNames",
			"the operate dir names in the base path, allow multi by spaces",
			false, true,
		)
	},
	Examples: `
	{$fullCmd} /my/workspace project1 project2
`,
	Func: func(c *gcli.Command, _ []string) (err error) {
		var ret string
		basePath := c.Arg("basePath").String("./")
		dirNames := c.Arg("dirNames").Strings()

		if len(dirNames) == 0 {
			dirNames = getSubDirs(basePath)
			if len(dirNames) == 0 {
				return fmt.Errorf("no valid subdirs in the base path: %s", basePath)
			}
		}

		color.Green.Println("The operate bash path:", basePath)
		fmt.Println("- want updated project dir names:", dirNames)

		for _, name := range dirNames {
			ret, err = execCmd("git pull", path.Join(basePath, name))
			if err != nil {
				return
			}
			color.Info.Println("RESULT:")
			fmt.Println(ret)
		}

		color.Cyan.Println("Update Complete :)")
		return
	},
}

GitPullMulti use git pull for update multi project

View Source
var GitRemote = &gcli.Command{
	Name:    "remote",
	Desc:    "remote command of the git",
	Aliases: []string{"rmt"},
	Config: func(c *gcli.Command) {
		c.BoolOpt(&gitRmtOPts.v, "v", "", false, "option for git remote")
	},
	Func: func(c *gcli.Command, args []string) error {
		dump.P(c.Path())
		return nil
	},
	Subs: []*gcli.Command{
		{
			Name:    "set-url",
			Desc:    "set-url command of git remote",
			Aliases: []string{"su"},
			Config: func(c *gcli.Command) {
				c.AddArg("name", "the remote name", true)
				c.AddArg("address", "the remote address", true)
			},
			Func: func(c *gcli.Command, args []string) error {
				dump.P(c.Path())
				return nil
			},
		},
	},
}

GitRemote remote command of the git.

View Source
var InteractDemo = &gcli.Command{
	Name:    "interact",
	Func:    interactDemo,
	Aliases: []string{"itt"},
	Config: func(c *gcli.Command) {
		c.AddArg("name", "want running interact method name", true)
	},
	Desc: "the command will show some interactive methods",
	Examples: `{$fullCmd} confirm
  {$fullCmd} select
`,
	Help: `
Supported interactive methods:
  read           read user input text
  answerIsYes    check user answer is Yes
  confirm        confirm message
  select         select one from given options
  password       read user hidden input
  multiSelect    select multi from given options
`,
}
View Source
var ProgressDemo = &gcli.Command{
	Name:    "prog",
	Desc:    "there are some progress bar run demos",
	Aliases: []string{"prg-demo", "progress"},

	Config: func(c *gcli.Command) {
		c.IntOpt(&pdOpts.maxSteps, "max-step", "", 100, "setting the max step value")
		c.BoolOpt(&pdOpts.overwrite, "overwrite", "o", true, "setting overwrite progress bar line")
		c.BoolVar(&pdOpts.random, &gcli.FlagMeta{Name: "random", Desc: "use random style for progress bar"})

		c.BindArg(&gcli.Argument{
			Name: "name",
			Desc: "progress bar type name. allow: bar,txt,dtxt,loading,roundTrip",

			Required: true,
		})
	},
	Examples: `Text progress bar:
  {$fullCmd} txt
Image progress bar:
  {$fullCmd} bar`,
	Func: func(c *gcli.Command, args []string) error {
		name := c.Arg("name").String()
		max := pdOpts.maxSteps

		color.Infoln("Progress Demo:")
		switch name {
		case "bar":
			showProgressBar(max)
		case "bars", "all-bar":
			showAllProgressBar(max)
		case "dt", "dtxt", "dynamicText":
			dynamicTextBar(max)
		case "txt", "text":
			txtProgressBar(max)
		case "spr", "load", "loading", "spinner":
			runLoadingBar(max)
		case "rt", "roundTrip":
			runRoundTripBar(max)
		default:
			return c.Errorf("the progress bar type name only allow: bar,txt,dtxt,loading,roundTrip. input is: %s", name)
		}
		return nil
	},
}
View Source
var ShowDemo = &gcli.Command{
	Name: "show",
	Func: runShow,

	Desc: "the command will show some data format methods",
}
View Source
var SpinnerDemo = &gcli.Command{
	Name:    "spinner",
	Desc:    "there are some CLI spinner bar run demos",
	Aliases: []string{"spr", "spr-demo"},

	Config: func(c *gcli.Command) {
		c.IntOpt(&spOpts.speed, "speed", "s", 100, "setting the spinner running speed")
		c.IntOpt(&spOpts.themeNum, "theme-num", "t", 0, "setting the theme numbering. allow: 0 - 16")

		c.AddArg("name",
			"spinner type name. allow: loading,roundTrip",
			false,
		)
	},
	Examples: `Loading spinner:
  {$fullCmd} loading
roundTrip spinner:
  {$fullCmd} roundTrip`,
	Func: func(c *gcli.Command, _ []string) error {
		name := c.Arg("name").String()

		switch name {
		case "", "spinner", "load", "loading":
			spOpts.runLoadingSpinner()
		case "rt", "roundTrip":
			spOpts.runRoundTripSpinner()
		default:
			return c.Errorf("the spinner type name only allow: loading,roundTrip. input is: %s", name)
		}
		return nil
	},
}

Functions

This section is empty.

Types

type GitInfoData

type GitInfoData struct {
	Tag       string `json:"tag" description:"get tag name"`
	Version   string `json:"version" description:"git repo version."`
	ReleaseAt string `json:"releaseAt" description:"latest commit date"`
}

type Names

type Names []string

The string flag list, implemented flag.Value interface

func (*Names) Set

func (ns *Names) Set(value string) error

func (*Names) String

func (ns *Names) String() string

Jump to

Keyboard shortcuts

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