Skip to content

Commit

Permalink
Merge pull request #691 from chrisallenlane/4.3.0
Browse files Browse the repository at this point in the history
4.3.0
  • Loading branch information
chrisallenlane committed Aug 7, 2022
2 parents 9de866d + f1e8602 commit b53a14b
Show file tree
Hide file tree
Showing 67 changed files with 183 additions and 174 deletions.
4 changes: 2 additions & 2 deletions INSTALLING.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ On Unix-like systems, you may simply paste the following snippet into your termi

```sh
cd /tmp \
&& wget https://github.com/cheat/cheat/releases/download/4.2.7/cheat-linux-amd64.gz \
&& wget https://github.com/cheat/cheat/releases/download/4.3.0/cheat-linux-amd64.gz \
&& gunzip cheat-linux-amd64.gz \
&& chmod +x cheat-linux-amd64 \
&& sudo mv cheat-linux-amd64 /usr/local/bin/cheat
```

You may need to need to change the version number (`4.2.7`) and the archive
You may need to need to change the version number (`4.3.0`) and the archive
(`cheat-linux-amd64.gz`) depending on your platform.

See the [releases page][releases] for a list of supported platforms.
Expand Down
11 changes: 11 additions & 0 deletions cmd/cheat/cmd_conf.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package main

import (
"fmt"

"github.com/cheat/cheat/internal/config"
)

func cmdConf(opts map[string]interface{}, conf config.Config) {
fmt.Println(conf.Path)
}
4 changes: 4 additions & 0 deletions cmd/cheat/docopt.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Options:
-T --tags List all tags in use
-v --version Print the version number
--rm=<cheatsheet> Remove (delete) <cheatsheet>
--conf Display the config file path

Examples:

Expand Down Expand Up @@ -53,3 +54,6 @@ Examples:

To remove (delete) the foo/bar cheatsheet:
cheat --rm foo/bar

To view the configuration file path:
cheat --conf
5 changes: 4 additions & 1 deletion cmd/cheat/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
"github.com/cheat/cheat/internal/installer"
)

const version = "4.2.7"
const version = "4.3.0"

func main() {

Expand Down Expand Up @@ -120,6 +120,9 @@ func main() {
var cmd func(map[string]interface{}, config.Config)

switch {
case opts["--conf"].(bool):
cmd = cmdConf

case opts["--directories"].(bool):
cmd = cmdDirectories

Expand Down
4 changes: 3 additions & 1 deletion cmd/cheat/str_config.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions cmd/cheat/str_usage.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion configs/conf.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ colorize: false
formatter: terminal

# Through which pager should output be piped?
# pager: less -FRX # <- recommended where available
# 'less -FRX' is recommended on Unix systems
# 'more' is recommended on Windows
pager: PAGER_PATH

# The paths at which cheatsheets are available. Tags associated with a cheatpath
# are automatically attached to all cheatsheets residing on that path.
Expand Down
15 changes: 9 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
module github.com/cheat/cheat

go 1.14
go 1.19

require (
github.com/alecthomas/chroma v0.10.0
github.com/davecgh/go-spew v1.1.1
github.com/dlclark/regexp2 v1.7.0 // indirect
github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815
github.com/kr/text v0.2.0 // indirect
github.com/mattn/go-isatty v0.0.14
github.com/mitchellh/go-homedir v1.1.0
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect
golang.org/x/sys v0.0.0-20220731174439-a90be440212d // indirect
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f // indirect
gopkg.in/yaml.v1 v1.0.0-20140924161607-9f9df34309c0
gopkg.in/yaml.v2 v2.4.0
)

require (
github.com/dlclark/regexp2 v1.7.0 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e // indirect
golang.org/x/sys v0.0.0-20220804214406-8e32c043e418 // indirect
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f // indirect
)
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220731174439-a90be440212d h1:Sv5ogFZatcgIMMtBSTTAgMYsicp25MXBubjXNDKwm80=
golang.org/x/sys v0.0.0-20220731174439-a90be440212d/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220804214406-8e32c043e418 h1:9vYwv7OjYaky/tlAeD7C4oC9EsPTlaFl1H2jS++V+ME=
golang.org/x/sys v0.0.0-20220804214406-8e32c043e418/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f h1:BLraFXnmrev5lT+xlilqcH8XK9/i0At2xKjWk4p6zsU=
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Expand Down
64 changes: 9 additions & 55 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@ package config
import (
"fmt"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"

cp "github.com/cheat/cheat/internal/cheatpath"
Expand All @@ -22,6 +20,7 @@ type Config struct {
Style string `yaml:"style"`
Formatter string `yaml:"formatter"`
Pager string `yaml:"pager"`
Path string
}

// New returns a new Config struct
Expand All @@ -36,6 +35,9 @@ func New(opts map[string]interface{}, confPath string, resolve bool) (Config, er
// initialize a config object
conf := Config{}

// store the config path
conf.Path = confPath

// unmarshal the yaml
err = yaml.UnmarshalStrict(buf, &conf)
if err != nil {
Expand Down Expand Up @@ -93,28 +95,11 @@ func New(opts map[string]interface{}, confPath string, resolve bool) (Config, er
conf.Cheatpaths[i].Path = expanded
}

// if an editor was not provided in the configs, look to envvars
// if an editor was not provided in the configs, attempt to choose one
// that's appropriate for the environment
if conf.Editor == "" {
if os.Getenv("VISUAL") != "" {
conf.Editor = os.Getenv("VISUAL")
} else if os.Getenv("EDITOR") != "" {
conf.Editor = os.Getenv("EDITOR")
} else if runtime.GOOS == "windows" {
conf.Editor = "notepad"
} else {
// try to fall back to `nano`
path, err := exec.LookPath("nano")
if err != nil {
return Config{}, fmt.Errorf("failed to locate nano: %s", err)
}

// use `nano` if we found it
if path != "" {
conf.Editor = "nano"
// otherwise, give up
} else {
return Config{}, fmt.Errorf("no editor set")
}
if conf.Editor, err = Editor(); err != nil {
return Config{}, err
}
}

Expand All @@ -128,39 +113,8 @@ func New(opts map[string]interface{}, confPath string, resolve bool) (Config, er
conf.Formatter = "terminal"
}

// attempt to fall back to `PAGER` if a pager is not specified in configs
// load the pager
conf.Pager = strings.TrimSpace(conf.Pager)
if conf.Pager == "" {
// look for `pager`, `less`, and `more` on the system PATH
pagerPath, _ := exec.LookPath("pager")
lessPath, _ := exec.LookPath("less")
morePath, _ := exec.LookPath("more")

// search first for a `PAGER` envvar
if os.Getenv("PAGER") != "" {
conf.Pager = os.Getenv("PAGER")

// search for `pager`
} else if pagerPath != "" {
conf.Pager = pagerPath

// search for `less`
} else if lessPath != "" {
conf.Pager = lessPath

// search for `more`
//
// XXX: this causes issues on some Linux systems. See:
// https://github.com/cheat/cheat/issues/681#issuecomment-1201842334
//
// By checking for `more` last, we're hoping to at least mitigate
// the frequency of this occurrence, because `pager` and `less` are
// likely to be available on most systems on which a user is likely
// to have installed `cheat`.
} else if morePath != "" {
conf.Pager = morePath
}
}

return conf, nil
}
30 changes: 30 additions & 0 deletions internal/config/editor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package config

import (
"fmt"
"os"
"os/exec"
"runtime"
)

// Editor attempts to locate an editor that's appropriate for the environment.
func Editor() (string, error) {

// default to `notepad.exe` on Windows
if runtime.GOOS == "windows" {
return "notepad", nil
}

// look for `nano` on the `PATH`
nano, _ := exec.LookPath("nano")

// search for `$VISUAL`, `$EDITOR`, and then `nano`, in that order
for _, editor := range []string{os.Getenv("VISUAL"), os.Getenv("EDITOR"), nano} {
if editor != "" {
return editor, nil
}
}

// return an error if no path is found
return "", fmt.Errorf("no editor set")
}
26 changes: 26 additions & 0 deletions internal/config/pager.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package config

import (
"os"
"os/exec"
)

// Pager attempts to locate a pager that's appropriate for the environment.
func Pager() string {

// if $PAGER is set, return the corresponding pager
if os.Getenv("PAGER") != "" {
return os.Getenv("PAGER")
}

// Otherwise, search for `pager`, `less`, and `more` on the `$PATH`. If
// none are found, return an empty pager.
for _, pager := range []string{"pager", "less", "more"} {
if path, err := exec.LookPath(pager); err != nil {
return path
}
}

// default to no pager
return ""
}
1 change: 1 addition & 0 deletions internal/installer/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func Run(configs string, confpath string) error {
// template the above paths into the default configs
configs = strings.Replace(configs, "COMMUNITY_PATH", community, -1)
configs = strings.Replace(configs, "PERSONAL_PATH", personal, -1)
configs = strings.Replace(configs, "PAGER_PATH", config.Pager(), -1)

// prompt the user to download the community cheatsheets
yes, err := Prompt(
Expand Down
9 changes: 0 additions & 9 deletions vendor/github.com/alecthomas/chroma/go.mod

This file was deleted.

14 changes: 0 additions & 14 deletions vendor/github.com/alecthomas/chroma/go.sum

This file was deleted.

3 changes: 0 additions & 3 deletions vendor/github.com/dlclark/regexp2/go.mod

This file was deleted.

5 changes: 0 additions & 5 deletions vendor/github.com/mattn/go-isatty/go.mod

This file was deleted.

2 changes: 0 additions & 2 deletions vendor/github.com/mattn/go-isatty/go.sum

This file was deleted.

1 change: 0 additions & 1 deletion vendor/github.com/mitchellh/go-homedir/go.mod

This file was deleted.

4 changes: 2 additions & 2 deletions vendor/golang.org/x/sys/unix/zerrors_linux_386.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions vendor/golang.org/x/sys/unix/zerrors_linux_amd64.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions vendor/golang.org/x/sys/unix/zerrors_linux_arm.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit b53a14b

Please sign in to comment.