feat: add Source to Cheatpath, add command option -C/--config to show current config path

This commit is contained in:
Lonenso 2022-03-20 00:01:11 +08:00
parent 5513724232
commit e2ec1344f4
9 changed files with 103 additions and 126 deletions

View File

@ -105,6 +105,10 @@ wget -O ~/.local/bin/cheatsheets https://raw.githubusercontent.com/cheat/cheat/m
chmod +x ~/.local/bin/cheatsheets chmod +x ~/.local/bin/cheatsheets
``` ```
#### Init cheatsheets ####
This command requires `yq`
To init the cheatsheets call `cheatsheets init`
#### Pull changes #### #### Pull changes ####
To pull the community and personal cheatsheets call `cheatsheets pull` To pull the community and personal cheatsheets call `cheatsheets pull`

View File

@ -5,6 +5,7 @@ Options:
--init Write a default config file to stdout --init Write a default config file to stdout
-a --all Search among all cheatpaths -a --all Search among all cheatpaths
-c --colorize Colorize output -c --colorize Colorize output
-C --config Print config path
-d --directories List cheatsheet directories -d --directories List cheatsheet directories
-e --edit=<cheatsheet> Edit <cheatsheet> -e --edit=<cheatsheet> Edit <cheatsheet>
-l --list List cheatsheets -l --list List cheatsheets
@ -21,6 +22,9 @@ Examples:
To initialize a config file: To initialize a config file:
mkdir -p ~/.config/cheat && cheat --init > ~/.config/cheat/conf.yml mkdir -p ~/.config/cheat && cheat --init > ~/.config/cheat/conf.yml
To show current config path:
cheat -C
To view the tar cheatsheet: To view the tar cheatsheet:
cheat tar cheat tar

View File

@ -104,6 +104,12 @@ func main() {
os.Exit(1) os.Exit(1)
} }
// print config path
if opts["--config"] != nil && opts["--config"] == true {
fmt.Fprintf(os.Stdout, "config: %s\n", confpath)
os.Exit(0)
}
// filter the cheatpaths if --path was passed // filter the cheatpaths if --path was passed
if opts["--path"] != nil { if opts["--path"] != nil {
conf.Cheatpaths, err = cheatpath.Filter( conf.Cheatpaths, err = cheatpath.Filter(

View File

@ -14,6 +14,7 @@ Options:
--init Write a default config file to stdout --init Write a default config file to stdout
-a --all Search among all cheatpaths -a --all Search among all cheatpaths
-c --colorize Colorize output -c --colorize Colorize output
-C --config Print config path
-d --directories List cheatsheet directories -d --directories List cheatsheet directories
-e --edit=<cheatsheet> Edit <cheatsheet> -e --edit=<cheatsheet> Edit <cheatsheet>
-l --list List cheatsheets -l --list List cheatsheets
@ -30,6 +31,9 @@ Examples:
To initialize a config file: To initialize a config file:
mkdir -p ~/.config/cheat && cheat --init > ~/.config/cheat/conf.yml mkdir -p ~/.config/cheat && cheat --init > ~/.config/cheat/conf.yml
To show current config path:
cheat -C
To view the tar cheatsheet: To view the tar cheatsheet:
cheat tar cheat tar

View File

@ -1,182 +1,139 @@
.\" Automatically generated by Pandoc 2.2.1 .\" Automatically generated by Pandoc 2.14.2
.\" .\"
.TH "CHEAT" "1" "" "" "General Commands Manual" .TH "CHEAT" "1" "" "" "General Commands Manual"
.hy .hy
.SH NAME .SH NAME
.PP .PP
\f[B]cheat\f[] \[em] create and view command\-line cheatsheets \f[B]cheat\f[R] \[em] create and view command-line cheatsheets
.SH SYNOPSIS .SH SYNOPSIS
.PP .PP
\f[B]cheat\f[] [options] [\f[I]CHEATSHEET\f[]] \f[B]cheat\f[R] [options] [\f[I]CHEATSHEET\f[R]]
.SH DESCRIPTION .SH DESCRIPTION
.PP .PP
\f[B]cheat\f[] allows you to create and view interactive cheatsheets on \f[B]cheat\f[R] allows you to create and view interactive cheatsheets on
the command\-line. the command-line.
It was designed to help remind *nix system administrators of options for It was designed to help remind *nix system administrators of options for
commands that they use frequently, but not frequently enough to commands that they use frequently, but not frequently enough to
remember. remember.
.SH OPTIONS .SH OPTIONS
.TP .TP
.B \[en]init \[en]init
Print a config file to stdout. Print a config file to stdout.
.RS
.RE
.TP .TP
.B \-c, \[en]colorize -c, \[en]colorize
Colorize output. Colorize output.
.RS
.RE
.TP .TP
.B \-d, \[en]directories -C, \[en]config
Print config path
.TP
-d, \[en]directories
List cheatsheet directories. List cheatsheet directories.
.RS
.RE
.TP .TP
.B \-e, \[en]edit=\f[I]CHEATSHEET\f[] -e, \[en]edit=\f[I]CHEATSHEET\f[R]
Open \f[I]CHEATSHEET\f[] for editing. Open \f[I]CHEATSHEET\f[R] for editing.
.RS
.RE
.TP .TP
.B \-l, \[en]list -l, \[en]list
List available cheatsheets. List available cheatsheets.
.RS
.RE
.TP .TP
.B \-p, \[en]path=\f[I]PATH\f[] -p, \[en]path=\f[I]PATH\f[R]
Filter only to sheets found on path \f[I]PATH\f[]. Filter only to sheets found on path \f[I]PATH\f[R].
.RS
.RE
.TP .TP
.B \-r, \[en]regex -r, \[en]regex
Treat search \f[I]PHRASE\f[] as a regular expression. Treat search \f[I]PHRASE\f[R] as a regular expression.
.RS
.RE
.TP .TP
.B \-s, \[en]search=\f[I]PHRASE\f[] -s, \[en]search=\f[I]PHRASE\f[R]
Search cheatsheets for \f[I]PHRASE\f[]. Search cheatsheets for \f[I]PHRASE\f[R].
.RS
.RE
.TP .TP
.B \-t, \[en]tag=\f[I]TAG\f[] -t, \[en]tag=\f[I]TAG\f[R]
Filter only to sheets tagged with \f[I]TAG\f[]. Filter only to sheets tagged with \f[I]TAG\f[R].
.RS
.RE
.TP .TP
.B \-T, \[en]tags -T, \[en]tags
List all tags in use. List all tags in use.
.RS
.RE
.TP .TP
.B \-v, \[en]version -v, \[en]version
Print the version number. Print the version number.
.RS
.RE
.TP .TP
.B \[en]rm=\f[I]CHEATSHEET\f[] \[en]rm=\f[I]CHEATSHEET\f[R]
Remove (deletes) \f[I]CHEATSHEET\f[]. Remove (deletes) \f[I]CHEATSHEET\f[R].
.RS
.RE
.SH EXAMPLES .SH EXAMPLES
.TP .TP
.B To view the foo cheatsheet: To view the foo cheatsheet:
cheat \f[I]foo\f[] cheat \f[I]foo\f[R]
.RS
.RE
.TP .TP
.B To edit (or create) the foo cheatsheet: To edit (or create) the foo cheatsheet:
cheat \-e \f[I]foo\f[] cheat -e \f[I]foo\f[R]
.RS
.RE
.TP .TP
.B To edit (or create) the foo/bar cheatsheet on the `work' cheatpath: To edit (or create) the foo/bar cheatsheet on the `work' cheatpath:
cheat \-p \f[I]work\f[] \-e \f[I]foo/bar\f[] cheat -p \f[I]work\f[R] -e \f[I]foo/bar\f[R]
.RS
.RE
.TP .TP
.B To view all cheatsheet directories: To view all cheatsheet directories:
cheat \-d cheat -d
.RS
.RE
.TP .TP
.B To list all available cheatsheets: To list all available cheatsheets:
cheat \-l cheat -l
.RS
.RE
.TP .TP
.B To list all cheatsheets whose titles match `apt': To list all cheatsheets whose titles match `apt':
cheat \-l \f[I]apt\f[] cheat -l \f[I]apt\f[R]
.RS
.RE
.TP .TP
.B To list all tags in use: To list all tags in use:
cheat \-T cheat -T
.RS
.RE
.TP .TP
.B To list available cheatsheets that are tagged as `personal': To list available cheatsheets that are tagged as `personal':
cheat \-l \-t \f[I]personal\f[] cheat -l -t \f[I]personal\f[R]
.RS
.RE
.TP .TP
.B To search for `ssh' among all cheatsheets, and colorize matches: To search for `ssh' among all cheatsheets, and colorize matches:
cheat \-c \-s \f[I]ssh\f[] cheat -c -s \f[I]ssh\f[R]
.RS
.RE
.TP .TP
.B To search (by regex) for cheatsheets that contain an IP address: To search (by regex) for cheatsheets that contain an IP address:
cheat \-c \-r \-s \f[I]`(?:[0\-9]{1,3}.){3}[0\-9]{1,3}'\f[] cheat -c -r -s \f[I]`(?:[0-9]{1,3}.){3}[0-9]{1,3}'\f[R]
.RS
.RE
.TP .TP
.B To remove (delete) the foo/bar cheatsheet: To remove (delete) the foo/bar cheatsheet:
cheat \[en]rm \f[I]foo/bar\f[] cheat \[en]rm \f[I]foo/bar\f[R]
.RS
.RE
.SH FILES .SH FILES
.SS Configuration .SS Configuration
.PP .PP
\f[B]cheat\f[] is configured via a YAML file that is conventionally \f[B]cheat\f[R] is configured via a YAML file that is conventionally
named \f[I]conf.yaml\f[]. named \f[I]conf.yaml\f[R].
\f[B]cheat\f[] will search for \f[I]conf.yaml\f[] in varying locations, \f[B]cheat\f[R] will search for \f[I]conf.yaml\f[R] in varying
depending upon your platform: locations, depending upon your platform:
.SS Linux, OSX, and other Unixes .SS Linux, OSX, and other Unixes
.IP "1." 3 .IP "1." 3
\f[B]CHEAT_CONFIG_PATH\f[] \f[B]CHEAT_CONFIG_PATH\f[R]
.IP "2." 3 .IP "2." 3
\f[B]XDG_CONFIG_HOME\f[]/cheat/conf.yaml \f[B]XDG_CONFIG_HOME\f[R]/cheat/conf.yaml
.IP "3." 3 .IP "3." 3
\f[B]$HOME\f[]/.config/cheat/conf.yml \f[B]$HOME\f[R]/.config/cheat/conf.yml
.IP "4." 3 .IP "4." 3
\f[B]$HOME\f[]/.cheat/conf.yml \f[B]$HOME\f[R]/.cheat/conf.yml
.SS Windows .SS Windows
.IP "1." 3 .IP "1." 3
\f[B]CHEAT_CONFIG_PATH\f[] \f[B]CHEAT_CONFIG_PATH\f[R]
.IP "2." 3 .IP "2." 3
\f[B]APPDATA\f[]/cheat/conf.yml \f[B]APPDATA\f[R]/cheat/conf.yml
.IP "3." 3 .IP "3." 3
\f[B]PROGRAMDATA\f[]/cheat/conf.yml \f[B]PROGRAMDATA\f[R]/cheat/conf.yml
.PP .PP
\f[B]cheat\f[] will search in the order specified above. \f[B]cheat\f[R] will search in the order specified above.
The first \f[I]conf.yaml\f[] encountered will be respected. The first \f[I]conf.yaml\f[R] encountered will be respected.
.PP .PP
If \f[B]cheat\f[] cannot locate a config file, it will ask if you'd like If \f[B]cheat\f[R] cannot locate a config file, it will ask if you\[cq]d
to generate one automatically. like to generate one automatically.
Alternatively, you may also generate a config file manually by running Alternatively, you may also generate a config file manually by running
\f[B]cheat \[en]init\f[] and saving its output to the appropriate \f[B]cheat \[en]init\f[R] and saving its output to the appropriate
location for your platform. location for your platform.
.SS Cheatpaths .SS Cheatpaths
.PP .PP
\f[B]cheat\f[] reads its cheatsheets from \[lq]cheatpaths\[rq], which \f[B]cheat\f[R] reads its cheatsheets from \[lq]cheatpaths\[rq], which
are the directories in which cheatsheets are stored. are the directories in which cheatsheets are stored.
Cheatpaths may be configured in \f[I]conf.yaml\f[], and viewed via Cheatpaths may be configured in \f[I]conf.yaml\f[R], and viewed via
\f[B]cheat \-d\f[]. \f[B]cheat -d\f[R].
.PP .PP
For detailed instructions on how to configure cheatpaths, please refer For detailed instructions on how to configure cheatpaths, please refer
to the comments in conf.yml. to the comments in conf.yml.
.SS Autocompletion .SS Autocompletion
.PP .PP
Autocompletion scripts for \f[B]bash\f[], \f[B]zsh\f[], and Autocompletion scripts for \f[B]bash\f[R], \f[B]zsh\f[R], and
\f[B]fish\f[] are available for download: \f[B]fish\f[R] are available for download:
.IP \[bu] 2 .IP \[bu] 2
<https://github.com/cheat/cheat/blob/master/scripts/cheat.bash> <https://github.com/cheat/cheat/blob/master/scripts/cheat.bash>
.IP \[bu] 2 .IP \[bu] 2
@ -184,25 +141,22 @@ Autocompletion scripts for \f[B]bash\f[], \f[B]zsh\f[], and
.IP \[bu] 2 .IP \[bu] 2
<https://github.com/cheat/cheat/blob/master/scripts/cheat.zsh> <https://github.com/cheat/cheat/blob/master/scripts/cheat.zsh>
.PP .PP
The \f[B]bash\f[] and \f[B]zsh\f[] scripts provide optional integration The \f[B]bash\f[R] and \f[B]zsh\f[R] scripts provide optional
with \f[B]fzf\f[], if the latter is available on your \f[B]PATH\f[]. integration with \f[B]fzf\f[R], if the latter is available on your
\f[B]PATH\f[R].
.PP .PP
The installation process will vary per system and shell configuration, The installation process will vary per system and shell configuration,
and thus will not be discussed here. and thus will not be discussed here.
.SH ENVIRONMENT .SH ENVIRONMENT
.TP .TP
.B \f[B]CHEAT_CONFIG_PATH\f[] \f[B]CHEAT_CONFIG_PATH\f[R]
The path at which the config file is available. The path at which the config file is available.
If \f[B]CHEAT_CONFIG_PATH\f[] is set, all other config paths will be If \f[B]CHEAT_CONFIG_PATH\f[R] is set, all other config paths will be
ignored. ignored.
.RS
.RE
.TP .TP
.B \f[B]CHEAT_USE_FZF\f[] \f[B]CHEAT_USE_FZF\f[R]
If set, autocompletion scripts will attempt to integrate with If set, autocompletion scripts will attempt to integrate with
\f[B]fzf\f[]. \f[B]fzf\f[R].
.RS
.RE
.SH RETURN VALUES .SH RETURN VALUES
.IP "0." 3 .IP "0." 3
Successful termination Successful termination
@ -218,4 +172,4 @@ See GitHub issues: <https://github.com/cheat/cheat/issues>
Christopher Allen Lane <chris@chris-allen-lane.com> Christopher Allen Lane <chris@chris-allen-lane.com>
.SH SEE ALSO .SH SEE ALSO
.PP .PP
\f[B]fzf(1)\f[] \f[B]fzf(1)\f[R]

View File

@ -26,6 +26,9 @@ OPTIONS
-c, --colorize -c, --colorize
: Colorize output. : Colorize output.
-C, --config
: Print config path
-d, --directories -d, --directories
: List cheatsheet directories. : List cheatsheet directories.

View File

@ -4,6 +4,7 @@ package cheatpath
type Cheatpath struct { type Cheatpath struct {
Name string `yaml:"name"` Name string `yaml:"name"`
Path string `yaml:"path"` Path string `yaml:"path"`
Source string `yaml:"source"`
ReadOnly bool `yaml:"readonly"` ReadOnly bool `yaml:"readonly"`
Tags []string `yaml:"tags"` Tags []string `yaml:"tags"`
} }

View File

@ -38,6 +38,7 @@ _cheat()
# implement completions # implement completions
case $prev in case $prev in
--colorize|-c|\ --colorize|-c|\
--config|-C|\
--directories|-d|\ --directories|-d|\
--init|\ --init|\
--regex|-r|\ --regex|-r|\

View File

@ -1,15 +1,15 @@
#!/bin/sh -e #!/bin/sh -e
init() { init() {
readarray d < <(yq -c '.cheatpaths[]' ~/.cheat.yml) readarray d < <(yq -c '.cheatpaths[]' "$(cheat -C | awk '{print $2}')")
for c in "${d[@]}"; for c in "${d[@]}";
do do
name="$(echo ${c} | yq -r '.name')" name="$(echo ${c} | yq -r '.name')"
path="$(echo ${c} | yq -r '.path')" path="$(echo ${c} | yq -r '.path')"
path="${path/#\~/$HOME}"
src="$( echo ${c} | yq -r '.source')" src="$( echo ${c} | yq -r '.source')"
echo "Init $name" [ ! -d "$path" ] && (echo "Init $name"; git clone $src $path) || :
[ ! -d "$path" ] && git clone $src $path || :
done done
echo "Finished init" echo "Finished init"
} }