Simple command line interface template for creating internal CLI's π»
βββ cmd
βΒ Β βββ {cmd_name}
βΒ Β βββ main.go
βββ {cmd_name}.go
βββ {cmd_sub_name}.go
βββ ...
βββ go.mod
βββ go.sum
βββ LICENSE
βββ Makefile
βββ README.md
The layout consists on the pkg
directory which has all the dependencies that I
need for creating custom CLI's and the cmd
directory which just executes the
cmd
package which is all the {cmd_name}.go, {cmd_sub_name}.go files...
The config pkg works as a getter and setter of json, where you can
config.Query()
and config.Set()
on a specific config path.
conf := c.Conf{
Id: "{cmd_name}",
Dir: "{configs_path}",
File: "config.json",
}
Check https://github.com/thedevsaddam/gojsonq
- Clone, fork it as a template
- Do
make init
and add your command name - go mod download
Put this snippet on your .bashrc
or any file that is integrated to the
.bashrc
(sourced in some way).
Note: change your_cmd
to the name of your executable.
#!/usr/bin/env bash
if [[ -x "$(command -v your_cmd)" ]]; then
_cli_bash_autocomplete() {
if [[ "${COMP_WORDS[0]}" != "source" ]]; then
local cur opts
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
if [[ "$cur" == "-"* ]]; then
opts=$("${COMP_WORDS[@]:0:$COMP_CWORD}" "${cur}" --generate-bash-completion)
else
opts=$("${COMP_WORDS[@]:0:$COMP_CWORD}" --generate-bash-completion)
fi
COMPREPLY=($(compgen -W "${opts}" -- "${cur}"))
return 0
fi
}
complete -o nospace -F _cli_bash_autocomplete your_cmd
fi