mirror of
https://github.com/cheat/cheat.git
synced 2026-03-07 11:13:33 +01:00
Compare commits
11 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b8098dc1b9 | ||
|
|
e2aa2d3611 | ||
|
|
80e0e0d3ae | ||
|
|
417b5b4e42 | ||
|
|
9b92261604 | ||
|
|
ca1ec0e38d | ||
|
|
52403dbe4a | ||
|
|
5ad1a3c39f | ||
|
|
d4a8a79628 | ||
|
|
007c9f9efe | ||
|
|
f61203ac1b |
105
.test-mutations.json
Normal file
105
.test-mutations.json
Normal file
@@ -0,0 +1,105 @@
|
|||||||
|
{
|
||||||
|
"version": "1.0",
|
||||||
|
"test_command": "go test ./...",
|
||||||
|
"last_updated": "2026-02-15T00:00:00Z",
|
||||||
|
"modules": {
|
||||||
|
"internal/sheet/parse.go": {
|
||||||
|
"status": "completed",
|
||||||
|
"covering_tests": ["internal/sheet/parse_test.go", "internal/sheet/parse_extended_test.go"],
|
||||||
|
"last_tested": "2026-02-15T00:00:00Z",
|
||||||
|
"mutations_applied": 8,
|
||||||
|
"mutations_killed": 8,
|
||||||
|
"mutation_score": 100.0,
|
||||||
|
"notes": "Originally 7/8 (87.5%). Added TestHasMalformedYAML to kill YAML unmarshal error survivor."
|
||||||
|
},
|
||||||
|
"internal/config/validate.go": {
|
||||||
|
"status": "completed",
|
||||||
|
"covering_tests": ["internal/config/validate_test.go"],
|
||||||
|
"last_tested": "2026-02-15T00:00:00Z",
|
||||||
|
"mutations_applied": 8,
|
||||||
|
"mutations_killed": 8,
|
||||||
|
"mutation_score": 100.0,
|
||||||
|
"notes": "Originally 7/8 (87.5%). Added TestInvalidateInvalidCheatpath to kill cheatpath.Validate() delegation survivor."
|
||||||
|
},
|
||||||
|
"internal/sheets/filter.go": {
|
||||||
|
"status": "completed",
|
||||||
|
"covering_tests": ["internal/sheets/filter_test.go"],
|
||||||
|
"last_tested": "2026-02-15T00:00:00Z",
|
||||||
|
"mutations_applied": 7,
|
||||||
|
"mutations_killed": 5,
|
||||||
|
"mutation_score": 71.4,
|
||||||
|
"notes": "Survivors relate to UTF-8 condition ordering and OR→AND on dead code path. Not actionable — logically equivalent mutations."
|
||||||
|
},
|
||||||
|
"internal/config/paths.go": {
|
||||||
|
"status": "completed",
|
||||||
|
"covering_tests": ["internal/config/paths_test.go"],
|
||||||
|
"last_tested": "2026-02-15T00:00:00Z",
|
||||||
|
"mutations_applied": 8,
|
||||||
|
"mutations_killed": 8,
|
||||||
|
"mutation_score": 100.0,
|
||||||
|
"notes": "Perfect score. Excellent existing coverage."
|
||||||
|
},
|
||||||
|
"internal/sheet/colorize.go": {
|
||||||
|
"status": "completed",
|
||||||
|
"covering_tests": ["internal/sheet/colorize_test.go"],
|
||||||
|
"last_tested": "2026-02-15T00:00:00Z",
|
||||||
|
"mutations_applied": 5,
|
||||||
|
"mutations_killed": 5,
|
||||||
|
"mutation_score": 100.0,
|
||||||
|
"notes": "Originally 2/5 (40%). Added TestColorizeDefaultSyntax and TestColorizeExplicitSyntax. All 5 mutations now killed."
|
||||||
|
},
|
||||||
|
"internal/sheets/consolidate.go": {
|
||||||
|
"status": "completed",
|
||||||
|
"covering_tests": ["internal/sheets/consolidate_test.go"],
|
||||||
|
"last_tested": "2026-02-15T00:00:00Z",
|
||||||
|
"mutations_applied": 2,
|
||||||
|
"mutations_killed": 2,
|
||||||
|
"mutation_score": 100.0,
|
||||||
|
"notes": "Override semantics well-tested."
|
||||||
|
},
|
||||||
|
"internal/display/indent.go": {
|
||||||
|
"status": "completed",
|
||||||
|
"covering_tests": ["internal/display/indent_test.go"],
|
||||||
|
"last_tested": "2026-02-15T00:00:00Z",
|
||||||
|
"mutations_applied": 3,
|
||||||
|
"mutations_killed": 3,
|
||||||
|
"mutation_score": 100.0,
|
||||||
|
"notes": "Originally 2/3 (66.7%). Added TestIndentTrimsWhitespace to kill TrimSpace survivor."
|
||||||
|
},
|
||||||
|
"internal/display/faint.go": {
|
||||||
|
"status": "completed",
|
||||||
|
"covering_tests": ["internal/display/faint_test.go"],
|
||||||
|
"last_tested": "2026-02-15T00:00:00Z",
|
||||||
|
"mutations_applied": 3,
|
||||||
|
"mutations_killed": 3,
|
||||||
|
"mutation_score": 100.0,
|
||||||
|
"notes": "Perfect score."
|
||||||
|
},
|
||||||
|
"internal/sheets/tags.go": {
|
||||||
|
"status": "completed",
|
||||||
|
"covering_tests": ["internal/sheets/tags_test.go"],
|
||||||
|
"last_tested": "2026-02-15T00:00:00Z",
|
||||||
|
"mutations_applied": 2,
|
||||||
|
"mutations_killed": 2,
|
||||||
|
"mutation_score": 100.0,
|
||||||
|
"notes": "UTF-8 validation and sort order both tested."
|
||||||
|
},
|
||||||
|
"internal/sheet/validate.go": {
|
||||||
|
"status": "completed",
|
||||||
|
"covering_tests": ["internal/sheet/validate_test.go"],
|
||||||
|
"last_tested": "2026-02-15T00:00:00Z",
|
||||||
|
"mutations_applied": 10,
|
||||||
|
"mutations_killed": 10,
|
||||||
|
"mutation_score": 100.0,
|
||||||
|
"notes": "Perfect score. All security checks well-tested."
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"global_statistics": {
|
||||||
|
"total_modules": 10,
|
||||||
|
"completed_modules": 10,
|
||||||
|
"total_mutations": 56,
|
||||||
|
"total_killed": 54,
|
||||||
|
"total_survived": 2,
|
||||||
|
"overall_score": 96.4
|
||||||
|
}
|
||||||
|
}
|
||||||
17
CLAUDE.md
17
CLAUDE.md
@@ -55,9 +55,13 @@ make vendor-update
|
|||||||
The `cheat` command-line tool is organized into several key packages:
|
The `cheat` command-line tool is organized into several key packages:
|
||||||
|
|
||||||
### Command Layer (`cmd/cheat/`)
|
### Command Layer (`cmd/cheat/`)
|
||||||
- `main.go`: Entry point, argument parsing, command routing
|
- `main.go`: Entry point, cobra command definition, flag registration, command routing
|
||||||
- `cmd_*.go`: Individual command implementations (view, edit, list, search, etc.)
|
- `cmd_*.go`: Individual command implementations (view, edit, list, search, etc.)
|
||||||
- Commands are selected based on docopt parsed arguments
|
- Commands are routed via a `switch` block in the cobra `RunE` handler
|
||||||
|
|
||||||
|
### Completions (`internal/completions/`)
|
||||||
|
- Dynamic shell completion functions for cheatsheet names, tags, and cheatpath names
|
||||||
|
- `generate.go`: Generates completion scripts for bash, zsh, fish, and powershell
|
||||||
|
|
||||||
### Core Internal Packages
|
### Core Internal Packages
|
||||||
|
|
||||||
@@ -85,7 +89,11 @@ The `cheat` command-line tool is organized into several key packages:
|
|||||||
- Writes to stdout or pager
|
- Writes to stdout or pager
|
||||||
- Handles text formatting and indentation
|
- Handles text formatting and indentation
|
||||||
|
|
||||||
6. **`internal/repo`**: Git repository management
|
6. **`internal/installer`**: First-run installer
|
||||||
|
- Prompts user for initial configuration choices
|
||||||
|
- Generates default `conf.yml` and downloads community cheatsheets
|
||||||
|
|
||||||
|
7. **`internal/repo`**: Git repository management
|
||||||
- Clones community cheatsheet repositories
|
- Clones community cheatsheet repositories
|
||||||
- Updates existing repositories
|
- Updates existing repositories
|
||||||
|
|
||||||
@@ -95,6 +103,7 @@ The `cheat` command-line tool is organized into several key packages:
|
|||||||
- **Override mechanism**: Local sheets override community sheets with same name
|
- **Override mechanism**: Local sheets override community sheets with same name
|
||||||
- **Tag system**: Sheets can be categorized with tags in frontmatter
|
- **Tag system**: Sheets can be categorized with tags in frontmatter
|
||||||
- **Multiple cheatpaths**: Supports personal, community, and directory-scoped sheets
|
- **Multiple cheatpaths**: Supports personal, community, and directory-scoped sheets
|
||||||
|
- **Directory-scoped discovery**: Walks up from cwd to find the nearest `.cheat` directory (like `.git` discovery)
|
||||||
|
|
||||||
### Sheet Format
|
### Sheet Format
|
||||||
|
|
||||||
@@ -114,4 +123,4 @@ ssh -L 8080:localhost:80 user@remote
|
|||||||
- Use `go-git` for repository operations, not exec'ing git commands
|
- Use `go-git` for repository operations, not exec'ing git commands
|
||||||
- Platform-specific paths are handled in `internal/config/paths.go`
|
- Platform-specific paths are handled in `internal/config/paths.go`
|
||||||
- Color output uses ANSI codes via the Chroma library
|
- Color output uses ANSI codes via the Chroma library
|
||||||
- Test files use the `internal/mock` package for test data
|
- Test files use the `mocks` package for test data
|
||||||
@@ -1,5 +1,4 @@
|
|||||||
Contributing
|
# Contributing
|
||||||
============
|
|
||||||
|
|
||||||
Thank you for your interest in `cheat`.
|
Thank you for your interest in `cheat`.
|
||||||
|
|
||||||
@@ -11,4 +10,8 @@ Bug reports are still welcome. If you've found a bug, please open an issue in
|
|||||||
the [issue tracker][issues]. Before doing so, please search through the
|
the [issue tracker][issues]. Before doing so, please search through the
|
||||||
existing open issues to make sure it hasn't already been reported.
|
existing open issues to make sure it hasn't already been reported.
|
||||||
|
|
||||||
|
Feature requests may be filed, but are unlikely to be implemented. The project
|
||||||
|
is now mature and the maintainer considers its feature set to be essentially
|
||||||
|
complete.
|
||||||
|
|
||||||
[issues]: https://github.com/cheat/cheat/issues
|
[issues]: https://github.com/cheat/cheat/issues
|
||||||
|
|||||||
12
HACKING.md
12
HACKING.md
@@ -63,7 +63,7 @@ make coverage-text # Terminal output
|
|||||||
|
|
||||||
The `cheat` application follows a clean architecture with well-separated concerns:
|
The `cheat` application follows a clean architecture with well-separated concerns:
|
||||||
|
|
||||||
- **`cmd/cheat/`**: Command layer with argument parsing and command routing
|
- **`cmd/cheat/`**: Command layer (cobra-based CLI, flag registration, command routing, shell completions)
|
||||||
- **`internal/config`**: Configuration management (YAML loading, validation, paths)
|
- **`internal/config`**: Configuration management (YAML loading, validation, paths)
|
||||||
- **`internal/cheatpath`**: Cheatsheet path management (collections, filtering)
|
- **`internal/cheatpath`**: Cheatsheet path management (collections, filtering)
|
||||||
- **`internal/sheet`**: Individual cheatsheet handling (parsing, search, highlighting)
|
- **`internal/sheet`**: Individual cheatsheet handling (parsing, search, highlighting)
|
||||||
@@ -88,7 +88,7 @@ The main configuration structure:
|
|||||||
type Config struct {
|
type Config struct {
|
||||||
Colorize bool `yaml:"colorize"`
|
Colorize bool `yaml:"colorize"`
|
||||||
Editor string `yaml:"editor"`
|
Editor string `yaml:"editor"`
|
||||||
Cheatpaths []cp.Cheatpath `yaml:"cheatpaths"`
|
Cheatpaths []cp.Path `yaml:"cheatpaths"`
|
||||||
Style string `yaml:"style"`
|
Style string `yaml:"style"`
|
||||||
Formatter string `yaml:"formatter"`
|
Formatter string `yaml:"formatter"`
|
||||||
Pager string `yaml:"pager"`
|
Pager string `yaml:"pager"`
|
||||||
@@ -97,7 +97,7 @@ type Config struct {
|
|||||||
```
|
```
|
||||||
|
|
||||||
Key functions:
|
Key functions:
|
||||||
- `New(opts, confPath, resolve)` - Load config from file
|
- `New(confPath, resolve)` - Load config from file
|
||||||
- `Validate()` - Validate configuration values
|
- `Validate()` - Validate configuration values
|
||||||
- `Editor()` - Get editor from environment or defaults (package-level function)
|
- `Editor()` - Get editor from environment or defaults (package-level function)
|
||||||
- `Pager()` - Get pager from environment or defaults (package-level function)
|
- `Pager()` - Get pager from environment or defaults (package-level function)
|
||||||
@@ -107,7 +107,7 @@ Key functions:
|
|||||||
Represents a directory containing cheatsheets:
|
Represents a directory containing cheatsheets:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
type Cheatpath struct {
|
type Path struct {
|
||||||
Name string // Friendly name (e.g., "personal")
|
Name string // Friendly name (e.g., "personal")
|
||||||
Path string // Filesystem path
|
Path string // Filesystem path
|
||||||
Tags []string // Tags applied to all sheets in this path
|
Tags []string // Tags applied to all sheets in this path
|
||||||
@@ -202,7 +202,7 @@ go test ./... # Go test directly
|
|||||||
Test files follow Go conventions:
|
Test files follow Go conventions:
|
||||||
- `*_test.go` files in same package
|
- `*_test.go` files in same package
|
||||||
- Table-driven tests for multiple scenarios
|
- Table-driven tests for multiple scenarios
|
||||||
- Mock data in `internal/mock` package
|
- Mock data in `mocks` package
|
||||||
|
|
||||||
## Error Handling
|
## Error Handling
|
||||||
|
|
||||||
@@ -213,7 +213,7 @@ The codebase follows consistent error handling patterns:
|
|||||||
|
|
||||||
Example:
|
Example:
|
||||||
```go
|
```go
|
||||||
sheet, err := sheet.New(path, tags, false)
|
s, err := sheet.New(title, cheatpath, path, tags, false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to load sheet: %w", err)
|
return fmt.Errorf("failed to load sheet: %w", err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,38 +1,37 @@
|
|||||||
Installing
|
# Installing
|
||||||
==========
|
|
||||||
`cheat` has no runtime dependencies. As such, installing it is generally
|
`cheat` has no runtime dependencies. As such, installing it is generally
|
||||||
straightforward. There are a few methods available:
|
straightforward. There are a few methods available:
|
||||||
|
|
||||||
### Install manually
|
## Install manually
|
||||||
#### Unix-like
|
### Unix-like
|
||||||
On Unix-like systems, you may simply paste the following snippet into your terminal:
|
On Unix-like systems, you may simply paste the following snippet into your terminal:
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
cd /tmp \
|
cd /tmp \
|
||||||
&& wget https://github.com/cheat/cheat/releases/download/4.5.1/cheat-linux-amd64.gz \
|
&& wget https://github.com/cheat/cheat/releases/download/5.0.0/cheat-linux-amd64.gz \
|
||||||
&& gunzip cheat-linux-amd64.gz \
|
&& gunzip cheat-linux-amd64.gz \
|
||||||
&& chmod +x cheat-linux-amd64 \
|
&& chmod +x cheat-linux-amd64 \
|
||||||
&& sudo mv cheat-linux-amd64 /usr/local/bin/cheat
|
&& sudo mv cheat-linux-amd64 /usr/local/bin/cheat
|
||||||
```
|
```
|
||||||
|
|
||||||
You may need to need to change the version number (`4.5.1`) and the archive
|
You may need to need to change the version number (`5.0.0`) and the archive
|
||||||
(`cheat-linux-amd64.gz`) depending on your platform.
|
(`cheat-linux-amd64.gz`) depending on your platform.
|
||||||
|
|
||||||
See the [releases page][releases] for a list of supported platforms.
|
See the [releases page][releases] for a list of supported platforms.
|
||||||
|
|
||||||
#### Windows
|
### Windows
|
||||||
On Windows, download the appropriate binary from the [releases page][releases],
|
On Windows, download the appropriate binary from the [releases page][releases],
|
||||||
unzip the archive, and place the `cheat.exe` executable on your `PATH`.
|
unzip the archive, and place the `cheat.exe` executable on your `PATH`.
|
||||||
|
|
||||||
### Install via `go install`
|
## Install via `go install`
|
||||||
If you have `go` version `>=1.17` available on your `PATH`, you can install
|
If you have `go` version `>=1.26` available on your `PATH`, you can install
|
||||||
`cheat` via `go install`:
|
`cheat` via `go install`:
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
go install github.com/cheat/cheat/cmd/cheat@latest
|
go install github.com/cheat/cheat/cmd/cheat@latest
|
||||||
```
|
```
|
||||||
|
|
||||||
### Install via package manager
|
## Install via package manager
|
||||||
Several community-maintained packages are also available:
|
Several community-maintained packages are also available:
|
||||||
|
|
||||||
Package manager | Package(s)
|
Package manager | Package(s)
|
||||||
@@ -43,8 +42,6 @@ docker | [docker-cheat][pkg-docker]
|
|||||||
nix | [nixos.cheat][pkg-nix]
|
nix | [nixos.cheat][pkg-nix]
|
||||||
snap | [cheat][pkg-snap]
|
snap | [cheat][pkg-snap]
|
||||||
|
|
||||||
<!--[pacman][] |-->
|
|
||||||
|
|
||||||
## Configuring
|
## Configuring
|
||||||
Three things must be done before you can use `cheat`:
|
Three things must be done before you can use `cheat`:
|
||||||
1. A config file must be generated
|
1. A config file must be generated
|
||||||
@@ -56,7 +53,7 @@ automatically. After the installer is complete, it is strongly advised that you
|
|||||||
view the configuration file that was generated, as you may want to change some
|
view the configuration file that was generated, as you may want to change some
|
||||||
of its default values (to enable colorization, change the paginator, etc).
|
of its default values (to enable colorization, change the paginator, etc).
|
||||||
|
|
||||||
### conf.yml ###
|
### conf.yml
|
||||||
`cheat` is configured by a YAML file that will be auto-generated on first run.
|
`cheat` is configured by a YAML file that will be auto-generated on first run.
|
||||||
|
|
||||||
By default, the config file is assumed to exist on an XDG-compliant
|
By default, the config file is assumed to exist on an XDG-compliant
|
||||||
@@ -72,8 +69,8 @@ export CHEAT_CONFIG_PATH="~/.dotfiles/cheat/conf.yml"
|
|||||||
[community]: https://github.com/cheat/cheatsheets/
|
[community]: https://github.com/cheat/cheatsheets/
|
||||||
[pkg-aur-cheat-bin]: https://aur.archlinux.org/packages/cheat-bin
|
[pkg-aur-cheat-bin]: https://aur.archlinux.org/packages/cheat-bin
|
||||||
[pkg-aur-cheat]: https://aur.archlinux.org/packages/cheat
|
[pkg-aur-cheat]: https://aur.archlinux.org/packages/cheat
|
||||||
[pkg-brew]: https://formulae.brew.sh/formula/cheat
|
[pkg-brew]: https://formulae.brew.sh/formula/cheat
|
||||||
[pkg-docker]: https://github.com/bannmann/docker-cheat
|
[pkg-docker]: https://github.com/bannmann/docker-cheat
|
||||||
[pkg-nix]: https://search.nixos.org/packages?channel=unstable&show=cheat&from=0&size=50&sort=relevance&type=packages&query=cheat
|
[pkg-nix]: https://search.nixos.org/packages?channel=unstable&show=cheat&from=0&size=50&sort=relevance&type=packages&query=cheat
|
||||||
[pkg-snap]: https://snapcraft.io/cheat
|
[pkg-snap]: https://snapcraft.io/cheat
|
||||||
[releases]: https://github.com/cheat/cheat/releases
|
[releases]: https://github.com/cheat/cheat/releases
|
||||||
|
|||||||
21
Makefile
21
Makefile
@@ -109,11 +109,6 @@ $(dist_dir)/cheat-openbsd-amd64:
|
|||||||
GOARCH=amd64 GOOS=openbsd \
|
GOARCH=amd64 GOOS=openbsd \
|
||||||
$(GO) build $(BUILD_FLAGS) -o $@ $(cmd_dir) && $(GZIP) $@ && chmod -x $@.gz
|
$(GO) build $(BUILD_FLAGS) -o $@ $(cmd_dir) && $(GZIP) $@ && chmod -x $@.gz
|
||||||
|
|
||||||
# cheat-plan9-amd64
|
|
||||||
$(dist_dir)/cheat-plan9-amd64:
|
|
||||||
GOARCH=amd64 GOOS=plan9 \
|
|
||||||
$(GO) build $(BUILD_FLAGS) -o $@ $(cmd_dir) && $(GZIP) $@ && chmod -x $@.gz
|
|
||||||
|
|
||||||
# cheat-solaris-amd64
|
# cheat-solaris-amd64
|
||||||
$(dist_dir)/cheat-solaris-amd64:
|
$(dist_dir)/cheat-solaris-amd64:
|
||||||
GOARCH=amd64 GOOS=solaris \
|
GOARCH=amd64 GOOS=solaris \
|
||||||
@@ -213,12 +208,12 @@ test-all: test test-integration
|
|||||||
## test-fuzz: run quick fuzz tests for security-critical functions
|
## test-fuzz: run quick fuzz tests for security-critical functions
|
||||||
.PHONY: test-fuzz
|
.PHONY: test-fuzz
|
||||||
test-fuzz:
|
test-fuzz:
|
||||||
@./build/fuzz.sh 15s
|
@./test/fuzz.sh 15s
|
||||||
|
|
||||||
## test-fuzz-long: run extended fuzz tests (10 minutes each)
|
## test-fuzz-long: run extended fuzz tests (10 minutes each)
|
||||||
.PHONY: test-fuzz-long
|
.PHONY: test-fuzz-long
|
||||||
test-fuzz-long:
|
test-fuzz-long:
|
||||||
@./build/fuzz.sh 10m
|
@./test/fuzz.sh 10m
|
||||||
|
|
||||||
## coverage: generate a test coverage report
|
## coverage: generate a test coverage report
|
||||||
.PHONY: coverage
|
.PHONY: coverage
|
||||||
@@ -240,22 +235,22 @@ coverage-text: .tmp
|
|||||||
## benchmark: run performance benchmarks
|
## benchmark: run performance benchmarks
|
||||||
.PHONY: benchmark
|
.PHONY: benchmark
|
||||||
benchmark: .tmp
|
benchmark: .tmp
|
||||||
$(GO) test -tags=integration -bench=. -benchtime=10s -benchmem ./cmd/cheat | tee .tmp/benchmark-latest.txt && \
|
$(GO) test -tags=integration -bench=. -benchtime=10s -benchmem ./test/integration | tee .tmp/benchmark-latest.txt && \
|
||||||
$(RM) -f cheat.test
|
$(RM) -f integration.test
|
||||||
|
|
||||||
## benchmark-cpu: run benchmarks with CPU profiling
|
## benchmark-cpu: run benchmarks with CPU profiling
|
||||||
.PHONY: benchmark-cpu
|
.PHONY: benchmark-cpu
|
||||||
benchmark-cpu: .tmp
|
benchmark-cpu: .tmp
|
||||||
$(GO) test -tags=integration -bench=. -benchtime=10s -cpuprofile=.tmp/cpu.prof ./cmd/cheat && \
|
$(GO) test -tags=integration -bench=. -benchtime=10s -cpuprofile=.tmp/cpu.prof ./test/integration && \
|
||||||
$(RM) -f cheat.test && \
|
$(RM) -f integration.test && \
|
||||||
echo "CPU profile saved to .tmp/cpu.prof" && \
|
echo "CPU profile saved to .tmp/cpu.prof" && \
|
||||||
echo "View with: go tool pprof -http=:8080 .tmp/cpu.prof"
|
echo "View with: go tool pprof -http=:8080 .tmp/cpu.prof"
|
||||||
|
|
||||||
## benchmark-mem: run benchmarks with memory profiling
|
## benchmark-mem: run benchmarks with memory profiling
|
||||||
.PHONY: benchmark-mem
|
.PHONY: benchmark-mem
|
||||||
benchmark-mem: .tmp
|
benchmark-mem: .tmp
|
||||||
$(GO) test -tags=integration -bench=. -benchtime=10s -benchmem -memprofile=.tmp/mem.prof ./cmd/cheat && \
|
$(GO) test -tags=integration -bench=. -benchtime=10s -benchmem -memprofile=.tmp/mem.prof ./test/integration && \
|
||||||
$(RM) -f cheat.test && \
|
$(RM) -f integration.test && \
|
||||||
echo "Memory profile saved to .tmp/mem.prof" && \
|
echo "Memory profile saved to .tmp/mem.prof" && \
|
||||||
echo "View with: go tool pprof -http=:8080 .tmp/mem.prof"
|
echo "View with: go tool pprof -http=:8080 .tmp/mem.prof"
|
||||||
|
|
||||||
|
|||||||
75
README.md
75
README.md
@@ -1,8 +1,6 @@
|
|||||||

|

|
||||||
|
|
||||||
|
# cheat
|
||||||
cheat
|
|
||||||
=====
|
|
||||||
|
|
||||||
`cheat` allows you to create and view interactive cheatsheets on the
|
`cheat` allows you to create and view interactive cheatsheets on the
|
||||||
command-line. It was designed to help remind \*nix system administrators of
|
command-line. It was designed to help remind \*nix system administrators of
|
||||||
@@ -13,9 +11,7 @@ remember.
|
|||||||
|
|
||||||
Use `cheat` with [cheatsheets][].
|
Use `cheat` with [cheatsheets][].
|
||||||
|
|
||||||
|
## Example
|
||||||
Example
|
|
||||||
-------
|
|
||||||
The next time you're forced to disarm a nuclear weapon without consulting
|
The next time you're forced to disarm a nuclear weapon without consulting
|
||||||
Google, you may run:
|
Google, you may run:
|
||||||
|
|
||||||
@@ -42,8 +38,10 @@ tar -xjvf '/path/to/foo.tgz'
|
|||||||
tar -cjvf '/path/to/foo.tgz' '/path/to/foo/'
|
tar -cjvf '/path/to/foo.tgz' '/path/to/foo/'
|
||||||
```
|
```
|
||||||
|
|
||||||
Usage
|
## Installing
|
||||||
-----
|
For installation and configuration instructions, see [INSTALLING.md][].
|
||||||
|
|
||||||
|
## Usage
|
||||||
To view a cheatsheet:
|
To view a cheatsheet:
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
@@ -70,6 +68,12 @@ To list all available cheatsheets:
|
|||||||
cheat -l
|
cheat -l
|
||||||
```
|
```
|
||||||
|
|
||||||
|
To briefly list all cheatsheets (names and tags only):
|
||||||
|
|
||||||
|
```sh
|
||||||
|
cheat -b
|
||||||
|
```
|
||||||
|
|
||||||
To list all cheatsheets that are tagged with "networking":
|
To list all cheatsheets that are tagged with "networking":
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
@@ -101,14 +105,7 @@ Flags may be combined in intuitive ways. Example: to search sheets on the
|
|||||||
cheat -p personal -t networking --regex -s '(?:[0-9]{1,3}\.){3}[0-9]{1,3}'
|
cheat -p personal -t networking --regex -s '(?:[0-9]{1,3}\.){3}[0-9]{1,3}'
|
||||||
```
|
```
|
||||||
|
|
||||||
|
## Cheatsheets
|
||||||
|
|
||||||
Installing
|
|
||||||
----------
|
|
||||||
For installation and configuration instructions, see [INSTALLING.md][].
|
|
||||||
|
|
||||||
Cheatsheets
|
|
||||||
-----------
|
|
||||||
Cheatsheets are plain-text files with no file extension, and are named
|
Cheatsheets are plain-text files with no file extension, and are named
|
||||||
according to the command used to view them:
|
according to the command used to view them:
|
||||||
|
|
||||||
@@ -137,8 +134,7 @@ The `cheat` executable includes no cheatsheets, but [community-sourced
|
|||||||
cheatsheets are available][cheatsheets]. You will be asked if you would like to
|
cheatsheets are available][cheatsheets]. You will be asked if you would like to
|
||||||
install the community-sourced cheatsheets the first time you run `cheat`.
|
install the community-sourced cheatsheets the first time you run `cheat`.
|
||||||
|
|
||||||
Cheatpaths
|
## Cheatpaths
|
||||||
----------
|
|
||||||
Cheatsheets are stored on "cheatpaths", which are directories that contain
|
Cheatsheets are stored on "cheatpaths", which are directories that contain
|
||||||
cheatsheets. Cheatpaths are specified in the `conf.yml` file.
|
cheatsheets. Cheatpaths are specified in the `conf.yml` file.
|
||||||
|
|
||||||
@@ -170,7 +166,7 @@ If a user attempts to edit a cheatsheet on a read-only cheatpath, `cheat` will
|
|||||||
transparently copy that sheet to a writeable directory before opening it for
|
transparently copy that sheet to a writeable directory before opening it for
|
||||||
editing.
|
editing.
|
||||||
|
|
||||||
### Directory-scoped Cheatpaths ###
|
### Directory-scoped Cheatpaths
|
||||||
At times, it can be useful to closely associate cheatsheets with a directory on
|
At times, it can be useful to closely associate cheatsheets with a directory on
|
||||||
your filesystem. `cheat` facilitates this by searching for a `.cheat` directory
|
your filesystem. `cheat` facilitates this by searching for a `.cheat` directory
|
||||||
in the current working directory and its ancestors (similar to how `git` locates
|
in the current working directory and its ancestors (similar to how `git` locates
|
||||||
@@ -178,24 +174,39 @@ in the current working directory and its ancestors (similar to how `git` locates
|
|||||||
added to the cheatpaths. This means you can place a `.cheat` directory at your
|
added to the cheatpaths. This means you can place a `.cheat` directory at your
|
||||||
project root and it will be available from any subdirectory within that project.
|
project root and it will be available from any subdirectory within that project.
|
||||||
|
|
||||||
Autocompletion
|
## Autocompletion
|
||||||
--------------
|
`cheat` can generate shell completion scripts for `bash`, `zsh`, `fish`, and
|
||||||
Shell autocompletion is currently available for `bash`, `fish`, and `zsh`. Copy
|
`powershell` via the `--completion` flag:
|
||||||
the relevant [completion script][completions] into the appropriate directory on
|
|
||||||
your filesystem to enable autocompletion. (This directory will vary depending
|
|
||||||
on operating system and shell specifics.)
|
|
||||||
|
|
||||||
Additionally, `cheat` supports enhanced autocompletion via integration with
|
```sh
|
||||||
[fzf][]. To enable `fzf` integration:
|
cheat --completion bash
|
||||||
|
cheat --completion zsh
|
||||||
|
cheat --completion fish
|
||||||
|
cheat --completion powershell
|
||||||
|
```
|
||||||
|
|
||||||
1. Ensure that `fzf` is available on your `$PATH`
|
Pipe the output to the appropriate location for your shell. For example:
|
||||||
2. Set an envvar: `export CHEAT_USE_FZF=true`
|
|
||||||
|
```sh
|
||||||
|
# bash (user-local)
|
||||||
|
mkdir -p ~/.local/share/bash-completion/completions
|
||||||
|
cheat --completion bash > ~/.local/share/bash-completion/completions/cheat
|
||||||
|
|
||||||
|
# bash (system-wide)
|
||||||
|
cheat --completion bash > /etc/bash_completion.d/cheat
|
||||||
|
|
||||||
|
# zsh (ensure the directory is on your fpath)
|
||||||
|
cheat --completion zsh > "${fpath[1]}/_cheat"
|
||||||
|
|
||||||
|
# fish
|
||||||
|
cheat --completion fish > ~/.config/fish/completions/cheat.fish
|
||||||
|
```
|
||||||
|
|
||||||
|
Completions are dynamically generated and include cheatsheet names, tags, and
|
||||||
|
cheatpath names.
|
||||||
|
|
||||||
[INSTALLING.md]: INSTALLING.md
|
[INSTALLING.md]: INSTALLING.md
|
||||||
[Releases]: https://github.com/cheat/cheat/releases
|
[Releases]: https://github.com/cheat/cheat/releases
|
||||||
[cheatsheets]: https://github.com/cheat/cheatsheets
|
[cheatsheets]: https://github.com/cheat/cheatsheets
|
||||||
[completions]: https://github.com/cheat/cheat/tree/master/scripts
|
|
||||||
[Chroma]: https://github.com/alecthomas/chroma
|
[Chroma]: https://github.com/alecthomas/chroma
|
||||||
[supported languages]: https://github.com/alecthomas/chroma#supported-languages
|
[supported languages]: https://github.com/alecthomas/chroma#supported-languages
|
||||||
[fzf]: https://github.com/junegunn/fzf
|
|
||||||
[go]: https://golang.org
|
|
||||||
|
|||||||
@@ -38,10 +38,10 @@ The validation is performed at the application layer before any file operations
|
|||||||
|
|
||||||
### Validation Function
|
### Validation Function
|
||||||
|
|
||||||
The validation is implemented in `internal/cheatpath/validate.go`:
|
The validation is implemented in `internal/sheet/validate.go`:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
func ValidateSheetName(name string) error {
|
func Validate(name string) error {
|
||||||
// Reject empty names
|
// Reject empty names
|
||||||
if name == "" {
|
if name == "" {
|
||||||
return fmt.Errorf("cheatsheet name cannot be empty")
|
return fmt.Errorf("cheatsheet name cannot be empty")
|
||||||
@@ -133,7 +133,7 @@ The following patterns are explicitly allowed:
|
|||||||
|
|
||||||
Comprehensive tests ensure the validation works correctly:
|
Comprehensive tests ensure the validation works correctly:
|
||||||
|
|
||||||
1. **Unit tests** (`internal/cheatpath/validate_test.go`) verify the validation logic
|
1. **Unit tests** (`internal/sheet/validate_test.go`) verify the validation logic
|
||||||
2. **Integration tests** verify the actual binary blocks malicious inputs
|
2. **Integration tests** verify the actual binary blocks malicious inputs
|
||||||
3. **No system files are accessed** during testing - all tests use isolated directories
|
3. **No system files are accessed** during testing - all tests use isolated directories
|
||||||
|
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ Accepted
|
|||||||
|
|
||||||
## Context
|
## Context
|
||||||
|
|
||||||
In `cmd/cheat/main.go` lines 47-52, the code parses environment variables assuming they all contain an equals sign:
|
In the `EnvVars()` function in `internal/config/env.go`, the code parses environment variables assuming they all contain an equals sign:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
for _, e := range os.Environ() {
|
for _, e := range os.Environ() {
|
||||||
|
|||||||
@@ -100,5 +100,5 @@ The parallelization attempt was valuable as a learning exercise and definitively
|
|||||||
|
|
||||||
## References
|
## References
|
||||||
|
|
||||||
- Benchmark implementation: cmd/cheat/search_bench_test.go
|
- Benchmark implementation: test/integration/search_bench_test.go
|
||||||
- Reverted parallel implementation: see git history (commit 82eb918)
|
- Reverted parallel implementation: see git history (commit 82eb918)
|
||||||
@@ -77,4 +77,4 @@ multiple cheatpaths can configure them in `conf.yml`.
|
|||||||
## References
|
## References
|
||||||
|
|
||||||
- GitHub issue: #602
|
- GitHub issue: #602
|
||||||
- Implementation: `findLocalCheatpath()` in `internal/config/config.go`
|
- Implementation: `findLocalCheatpath()` in `internal/config/new.go`
|
||||||
@@ -3,9 +3,11 @@ package main
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
|
||||||
"github.com/cheat/cheat/internal/config"
|
"github.com/cheat/cheat/internal/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
func cmdConf(_ map[string]interface{}, conf config.Config) {
|
func cmdConf(_ *cobra.Command, _ []string, conf config.Config) {
|
||||||
fmt.Println(conf.Path)
|
fmt.Println(conf.Path)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,12 +5,14 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"text/tabwriter"
|
"text/tabwriter"
|
||||||
|
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
|
||||||
"github.com/cheat/cheat/internal/config"
|
"github.com/cheat/cheat/internal/config"
|
||||||
"github.com/cheat/cheat/internal/display"
|
"github.com/cheat/cheat/internal/display"
|
||||||
)
|
)
|
||||||
|
|
||||||
// cmdDirectories lists the configured cheatpaths.
|
// cmdDirectories lists the configured cheatpaths.
|
||||||
func cmdDirectories(_ map[string]interface{}, conf config.Config) {
|
func cmdDirectories(_ *cobra.Command, _ []string, conf config.Config) {
|
||||||
|
|
||||||
// initialize a tabwriter to produce cleanly columnized output
|
// initialize a tabwriter to produce cleanly columnized output
|
||||||
var out bytes.Buffer
|
var out bytes.Buffer
|
||||||
|
|||||||
@@ -7,18 +7,21 @@ import (
|
|||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
|
||||||
"github.com/cheat/cheat/internal/cheatpath"
|
"github.com/cheat/cheat/internal/cheatpath"
|
||||||
"github.com/cheat/cheat/internal/config"
|
"github.com/cheat/cheat/internal/config"
|
||||||
|
"github.com/cheat/cheat/internal/sheet"
|
||||||
"github.com/cheat/cheat/internal/sheets"
|
"github.com/cheat/cheat/internal/sheets"
|
||||||
)
|
)
|
||||||
|
|
||||||
// cmdEdit opens a cheatsheet for editing (or creates it if it doesn't exist).
|
// cmdEdit opens a cheatsheet for editing (or creates it if it doesn't exist).
|
||||||
func cmdEdit(opts map[string]interface{}, conf config.Config) {
|
func cmdEdit(cmd *cobra.Command, _ []string, conf config.Config) {
|
||||||
|
|
||||||
cheatsheet := opts["--edit"].(string)
|
cheatsheet, _ := cmd.Flags().GetString("edit")
|
||||||
|
|
||||||
// validate the cheatsheet name
|
// validate the cheatsheet name
|
||||||
if err := cheatpath.ValidateSheetName(cheatsheet); err != nil {
|
if err := sheet.Validate(cheatsheet); err != nil {
|
||||||
fmt.Fprintf(os.Stderr, "invalid cheatsheet name: %v\n", err)
|
fmt.Fprintf(os.Stderr, "invalid cheatsheet name: %v\n", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
@@ -29,12 +32,11 @@ func cmdEdit(opts map[string]interface{}, conf config.Config) {
|
|||||||
fmt.Fprintf(os.Stderr, "failed to list cheatsheets: %v\n", err)
|
fmt.Fprintf(os.Stderr, "failed to list cheatsheets: %v\n", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
if cmd.Flags().Changed("tag") {
|
||||||
// filter cheatcheats by tag if --tag was provided
|
tagVal, _ := cmd.Flags().GetString("tag")
|
||||||
if opts["--tag"] != nil {
|
|
||||||
cheatsheets = sheets.Filter(
|
cheatsheets = sheets.Filter(
|
||||||
cheatsheets,
|
cheatsheets,
|
||||||
strings.Split(opts["--tag"].(string), ","),
|
strings.Split(tagVal, ","),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -52,55 +54,36 @@ func cmdEdit(opts map[string]interface{}, conf config.Config) {
|
|||||||
// if the sheet exists and is not read-only, edit it in place
|
// if the sheet exists and is not read-only, edit it in place
|
||||||
if ok && !sheet.ReadOnly {
|
if ok && !sheet.ReadOnly {
|
||||||
editpath = sheet.Path
|
editpath = sheet.Path
|
||||||
|
} else {
|
||||||
// if the sheet exists but is read-only, copy it before editing
|
// for read-only or non-existent sheets, resolve a writeable path
|
||||||
} else if ok && sheet.ReadOnly {
|
|
||||||
// compute the new edit path
|
|
||||||
// begin by getting a writeable cheatpath
|
|
||||||
writepath, err := cheatpath.Writeable(conf.Cheatpaths)
|
writepath, err := cheatpath.Writeable(conf.Cheatpaths)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintf(os.Stderr, "failed to get writeable path: %v\n", err)
|
fmt.Fprintf(os.Stderr, "failed to get writeable path: %v\n", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
// compute the new edit path
|
// use the existing title for read-only copies, the requested name otherwise
|
||||||
editpath = filepath.Join(writepath.Path, sheet.Title)
|
title := cheatsheet
|
||||||
|
if ok {
|
||||||
|
title = sheet.Title
|
||||||
|
}
|
||||||
|
editpath = filepath.Join(writepath.Path, title)
|
||||||
|
|
||||||
// create any necessary subdirectories
|
if ok {
|
||||||
dirs := filepath.Dir(editpath)
|
// copy the read-only sheet to the writeable path
|
||||||
if dirs != "." {
|
// (Copy handles MkdirAll internally)
|
||||||
if err := os.MkdirAll(dirs, 0755); err != nil {
|
if err := sheet.Copy(editpath); err != nil {
|
||||||
fmt.Fprintf(os.Stderr, "failed to create directory: %s, %v\n", dirs, err)
|
fmt.Fprintf(os.Stderr, "failed to copy cheatsheet: %v\n", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
}
|
} else {
|
||||||
|
// create any necessary subdirectories for the new sheet
|
||||||
// copy the sheet to the new edit path
|
dirs := filepath.Dir(editpath)
|
||||||
err = sheet.Copy(editpath)
|
if dirs != "." {
|
||||||
if err != nil {
|
if err := os.MkdirAll(dirs, 0755); err != nil {
|
||||||
fmt.Fprintf(os.Stderr, "failed to copy cheatsheet: %v\n", err)
|
fmt.Fprintf(os.Stderr, "failed to create directory: %s, %v\n", dirs, err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
// if the sheet does not exist, create it
|
|
||||||
} else {
|
|
||||||
// compute the new edit path
|
|
||||||
// begin by getting a writeable cheatpath
|
|
||||||
writepath, err := cheatpath.Writeable(conf.Cheatpaths)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Fprintf(os.Stderr, "failed to get writeable path: %v\n", err)
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
|
|
||||||
// compute the new edit path
|
|
||||||
editpath = filepath.Join(writepath.Path, cheatsheet)
|
|
||||||
|
|
||||||
// create any necessary subdirectories
|
|
||||||
dirs := filepath.Dir(editpath)
|
|
||||||
if dirs != "." {
|
|
||||||
if err := os.MkdirAll(dirs, 0755); err != nil {
|
|
||||||
fmt.Fprintf(os.Stderr, "failed to create directory: %s, %v\n", dirs, err)
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -110,14 +93,14 @@ func cmdEdit(opts map[string]interface{}, conf config.Config) {
|
|||||||
// call to `exec.Command` will fail.
|
// call to `exec.Command` will fail.
|
||||||
parts := strings.Fields(conf.Editor)
|
parts := strings.Fields(conf.Editor)
|
||||||
editor := parts[0]
|
editor := parts[0]
|
||||||
args := append(parts[1:], editpath)
|
editorArgs := append(parts[1:], editpath)
|
||||||
|
|
||||||
// edit the cheatsheet
|
// edit the cheatsheet
|
||||||
cmd := exec.Command(editor, args...)
|
editorCmd := exec.Command(editor, editorArgs...)
|
||||||
cmd.Stdout = os.Stdout
|
editorCmd.Stdout = os.Stdout
|
||||||
cmd.Stdin = os.Stdin
|
editorCmd.Stdin = os.Stdin
|
||||||
cmd.Stderr = os.Stderr
|
editorCmd.Stderr = os.Stderr
|
||||||
if err := cmd.Run(); err != nil {
|
if err := editorCmd.Run(); err != nil {
|
||||||
fmt.Fprintf(os.Stderr, "failed to edit cheatsheet: %v\n", err)
|
fmt.Fprintf(os.Stderr, "failed to edit cheatsheet: %v\n", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,78 +3,27 @@ package main
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
|
||||||
"runtime"
|
"runtime"
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/mitchellh/go-homedir"
|
|
||||||
|
|
||||||
"github.com/cheat/cheat/internal/config"
|
"github.com/cheat/cheat/internal/config"
|
||||||
|
"github.com/cheat/cheat/internal/installer"
|
||||||
)
|
)
|
||||||
|
|
||||||
// cmdInit displays an example config file.
|
// cmdInit displays an example config file.
|
||||||
func cmdInit() {
|
func cmdInit(home string, envvars map[string]string) {
|
||||||
|
|
||||||
// get the user's home directory
|
// identify the os-specific paths at which configs may be located
|
||||||
home, err := homedir.Dir()
|
|
||||||
if err != nil {
|
|
||||||
fmt.Fprintf(os.Stderr, "failed to get user home directory: %v\n", err)
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
|
|
||||||
// read the envvars into a map of strings
|
|
||||||
envvars := map[string]string{}
|
|
||||||
for _, e := range os.Environ() {
|
|
||||||
pair := strings.SplitN(e, "=", 2)
|
|
||||||
envvars[pair[0]] = pair[1]
|
|
||||||
}
|
|
||||||
|
|
||||||
// load the config template
|
|
||||||
configs := configs()
|
|
||||||
|
|
||||||
// identify the os-specifc paths at which configs may be located
|
|
||||||
confpaths, err := config.Paths(runtime.GOOS, home, envvars)
|
confpaths, err := config.Paths(runtime.GOOS, home, envvars)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintf(os.Stderr, "failed to read config paths: %v\n", err)
|
fmt.Fprintf(os.Stderr, "failed to read config paths: %v\n", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
||||||
// determine the appropriate paths for config data and (optional) community
|
|
||||||
// cheatsheets based on the user's platform
|
|
||||||
confpath := confpaths[0]
|
confpath := confpaths[0]
|
||||||
confdir := filepath.Dir(confpath)
|
|
||||||
|
|
||||||
// create paths for community, personal, and work cheatsheets
|
// expand template placeholders and comment out community cheatpath
|
||||||
community := filepath.Join(confdir, "cheatsheets", "community")
|
configs := installer.ExpandTemplate(configs(), confpath)
|
||||||
personal := filepath.Join(confdir, "cheatsheets", "personal")
|
configs = installer.CommentCommunity(configs, confpath)
|
||||||
work := filepath.Join(confdir, "cheatsheets", "work")
|
|
||||||
|
|
||||||
// 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, "WORK_PATH", work, -1)
|
|
||||||
|
|
||||||
// locate and set a default pager
|
|
||||||
configs = strings.Replace(configs, "PAGER_PATH", config.Pager(), -1)
|
|
||||||
|
|
||||||
// locate and set a default editor
|
|
||||||
if editor, err := config.Editor(); err == nil {
|
|
||||||
configs = strings.Replace(configs, "EDITOR_PATH", editor, -1)
|
|
||||||
}
|
|
||||||
|
|
||||||
// comment out the community cheatpath by default, since the directory
|
|
||||||
// won't exist until the user clones it
|
|
||||||
configs = strings.Replace(configs,
|
|
||||||
" - name: community\n"+
|
|
||||||
" path: "+community+"\n"+
|
|
||||||
" tags: [ community ]\n"+
|
|
||||||
" readonly: true",
|
|
||||||
" #- name: community\n"+
|
|
||||||
" # path: "+community+"\n"+
|
|
||||||
" # tags: [ community ]\n"+
|
|
||||||
" # readonly: true",
|
|
||||||
-1,
|
|
||||||
)
|
|
||||||
|
|
||||||
// output the templated configs
|
// output the templated configs
|
||||||
fmt.Println(configs)
|
fmt.Println(configs)
|
||||||
|
|||||||
@@ -9,6 +9,8 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"text/tabwriter"
|
"text/tabwriter"
|
||||||
|
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
|
||||||
"github.com/cheat/cheat/internal/config"
|
"github.com/cheat/cheat/internal/config"
|
||||||
"github.com/cheat/cheat/internal/display"
|
"github.com/cheat/cheat/internal/display"
|
||||||
"github.com/cheat/cheat/internal/sheet"
|
"github.com/cheat/cheat/internal/sheet"
|
||||||
@@ -16,7 +18,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// cmdList lists all available cheatsheets.
|
// cmdList lists all available cheatsheets.
|
||||||
func cmdList(opts map[string]interface{}, conf config.Config) {
|
func cmdList(cmd *cobra.Command, args []string, conf config.Config) {
|
||||||
|
|
||||||
// load the cheatsheets
|
// load the cheatsheets
|
||||||
cheatsheets, err := sheets.Load(conf.Cheatpaths)
|
cheatsheets, err := sheets.Load(conf.Cheatpaths)
|
||||||
@@ -24,12 +26,11 @@ func cmdList(opts map[string]interface{}, conf config.Config) {
|
|||||||
fmt.Fprintf(os.Stderr, "failed to list cheatsheets: %v\n", err)
|
fmt.Fprintf(os.Stderr, "failed to list cheatsheets: %v\n", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
if cmd.Flags().Changed("tag") {
|
||||||
// filter cheatsheets by tag if --tag was provided
|
tagVal, _ := cmd.Flags().GetString("tag")
|
||||||
if opts["--tag"] != nil {
|
|
||||||
cheatsheets = sheets.Filter(
|
cheatsheets = sheets.Filter(
|
||||||
cheatsheets,
|
cheatsheets,
|
||||||
strings.Split(opts["--tag"].(string), ","),
|
strings.Split(tagVal, ","),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -49,16 +50,13 @@ func cmdList(opts map[string]interface{}, conf config.Config) {
|
|||||||
})
|
})
|
||||||
|
|
||||||
// filter if <cheatsheet> was specified
|
// filter if <cheatsheet> was specified
|
||||||
// NB: our docopt specification is misleading here. When used in conjunction
|
if len(args) > 0 {
|
||||||
// with `-l`, `<cheatsheet>` is really a pattern against which to filter
|
|
||||||
// sheet titles.
|
|
||||||
if opts["<cheatsheet>"] != nil {
|
|
||||||
|
|
||||||
// initialize a slice of filtered sheets
|
// initialize a slice of filtered sheets
|
||||||
filtered := []sheet.Sheet{}
|
filtered := []sheet.Sheet{}
|
||||||
|
|
||||||
// initialize our filter pattern
|
// initialize our filter pattern
|
||||||
pattern := "(?i)" + opts["<cheatsheet>"].(string)
|
pattern := "(?i)" + args[0]
|
||||||
|
|
||||||
// compile the regex
|
// compile the regex
|
||||||
reg, err := regexp.Compile(pattern)
|
reg, err := regexp.Compile(pattern)
|
||||||
@@ -87,12 +85,18 @@ func cmdList(opts map[string]interface{}, conf config.Config) {
|
|||||||
var out bytes.Buffer
|
var out bytes.Buffer
|
||||||
w := tabwriter.NewWriter(&out, 0, 0, 1, ' ', 0)
|
w := tabwriter.NewWriter(&out, 0, 0, 1, ' ', 0)
|
||||||
|
|
||||||
// write a header row
|
|
||||||
fmt.Fprintln(w, "title:\tfile:\ttags:")
|
|
||||||
|
|
||||||
// generate sorted, columnized output
|
// generate sorted, columnized output
|
||||||
for _, sheet := range flattened {
|
briefFlag, _ := cmd.Flags().GetBool("brief")
|
||||||
fmt.Fprintf(w, "%s\t%s\t%s\n", sheet.Title, sheet.Path, strings.Join(sheet.Tags, ","))
|
if briefFlag {
|
||||||
|
fmt.Fprintln(w, "title:\ttags:")
|
||||||
|
for _, sheet := range flattened {
|
||||||
|
fmt.Fprintf(w, "%s\t%s\n", sheet.Title, strings.Join(sheet.Tags, ","))
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
fmt.Fprintln(w, "title:\tfile:\ttags:")
|
||||||
|
for _, sheet := range flattened {
|
||||||
|
fmt.Fprintf(w, "%s\t%s\t%s\n", sheet.Title, sheet.Path, strings.Join(sheet.Tags, ","))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// write columnized output to stdout
|
// write columnized output to stdout
|
||||||
|
|||||||
@@ -5,18 +5,20 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/cheat/cheat/internal/cheatpath"
|
"github.com/spf13/cobra"
|
||||||
|
|
||||||
"github.com/cheat/cheat/internal/config"
|
"github.com/cheat/cheat/internal/config"
|
||||||
|
"github.com/cheat/cheat/internal/sheet"
|
||||||
"github.com/cheat/cheat/internal/sheets"
|
"github.com/cheat/cheat/internal/sheets"
|
||||||
)
|
)
|
||||||
|
|
||||||
// cmdRemove removes (deletes) a cheatsheet.
|
// cmdRemove removes (deletes) a cheatsheet.
|
||||||
func cmdRemove(opts map[string]interface{}, conf config.Config) {
|
func cmdRemove(cmd *cobra.Command, _ []string, conf config.Config) {
|
||||||
|
|
||||||
cheatsheet := opts["--rm"].(string)
|
cheatsheet, _ := cmd.Flags().GetString("rm")
|
||||||
|
|
||||||
// validate the cheatsheet name
|
// validate the cheatsheet name
|
||||||
if err := cheatpath.ValidateSheetName(cheatsheet); err != nil {
|
if err := sheet.Validate(cheatsheet); err != nil {
|
||||||
fmt.Fprintf(os.Stderr, "invalid cheatsheet name: %v\n", err)
|
fmt.Fprintf(os.Stderr, "invalid cheatsheet name: %v\n", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
@@ -27,12 +29,11 @@ func cmdRemove(opts map[string]interface{}, conf config.Config) {
|
|||||||
fmt.Fprintf(os.Stderr, "failed to list cheatsheets: %v\n", err)
|
fmt.Fprintf(os.Stderr, "failed to list cheatsheets: %v\n", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
if cmd.Flags().Changed("tag") {
|
||||||
// filter cheatcheats by tag if --tag was provided
|
tagVal, _ := cmd.Flags().GetString("tag")
|
||||||
if opts["--tag"] != nil {
|
|
||||||
cheatsheets = sheets.Filter(
|
cheatsheets = sheets.Filter(
|
||||||
cheatsheets,
|
cheatsheets,
|
||||||
strings.Split(opts["--tag"].(string), ","),
|
strings.Split(tagVal, ","),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -6,15 +6,19 @@ import (
|
|||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
|
||||||
"github.com/cheat/cheat/internal/config"
|
"github.com/cheat/cheat/internal/config"
|
||||||
"github.com/cheat/cheat/internal/display"
|
"github.com/cheat/cheat/internal/display"
|
||||||
"github.com/cheat/cheat/internal/sheets"
|
"github.com/cheat/cheat/internal/sheets"
|
||||||
)
|
)
|
||||||
|
|
||||||
// cmdSearch searches for strings in cheatsheets.
|
// cmdSearch searches for strings in cheatsheets.
|
||||||
func cmdSearch(opts map[string]interface{}, conf config.Config) {
|
func cmdSearch(cmd *cobra.Command, args []string, conf config.Config) {
|
||||||
|
|
||||||
phrase := opts["--search"].(string)
|
phrase, _ := cmd.Flags().GetString("search")
|
||||||
|
colorize, _ := cmd.Flags().GetBool("colorize")
|
||||||
|
useRegex, _ := cmd.Flags().GetBool("regex")
|
||||||
|
|
||||||
// load the cheatsheets
|
// load the cheatsheets
|
||||||
cheatsheets, err := sheets.Load(conf.Cheatpaths)
|
cheatsheets, err := sheets.Load(conf.Cheatpaths)
|
||||||
@@ -22,12 +26,11 @@ func cmdSearch(opts map[string]interface{}, conf config.Config) {
|
|||||||
fmt.Fprintf(os.Stderr, "failed to list cheatsheets: %v\n", err)
|
fmt.Fprintf(os.Stderr, "failed to list cheatsheets: %v\n", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
if cmd.Flags().Changed("tag") {
|
||||||
// filter cheatcheats by tag if --tag was provided
|
tagVal, _ := cmd.Flags().GetString("tag")
|
||||||
if opts["--tag"] != nil {
|
|
||||||
cheatsheets = sheets.Filter(
|
cheatsheets = sheets.Filter(
|
||||||
cheatsheets,
|
cheatsheets,
|
||||||
strings.Split(opts["--tag"].(string), ","),
|
strings.Split(tagVal, ","),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -35,7 +38,7 @@ func cmdSearch(opts map[string]interface{}, conf config.Config) {
|
|||||||
pattern := "(?i)" + phrase
|
pattern := "(?i)" + phrase
|
||||||
|
|
||||||
// unless --regex is provided, in which case we pass the regex unaltered
|
// unless --regex is provided, in which case we pass the regex unaltered
|
||||||
if opts["--regex"] == true {
|
if useRegex {
|
||||||
pattern = phrase
|
pattern = phrase
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -55,7 +58,7 @@ func cmdSearch(opts map[string]interface{}, conf config.Config) {
|
|||||||
|
|
||||||
// if <cheatsheet> was provided, constrain the search only to
|
// if <cheatsheet> was provided, constrain the search only to
|
||||||
// matching cheatsheets
|
// matching cheatsheets
|
||||||
if opts["<cheatsheet>"] != nil && sheet.Title != opts["<cheatsheet>"] {
|
if len(args) > 0 && sheet.Title != args[0] {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -70,7 +73,7 @@ func cmdSearch(opts map[string]interface{}, conf config.Config) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// if colorization was requested, apply it here
|
// if colorization was requested, apply it here
|
||||||
if conf.Color(opts) {
|
if conf.Color(colorize) {
|
||||||
sheet.Colorize(conf)
|
sheet.Colorize(conf)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -80,7 +83,7 @@ func cmdSearch(opts map[string]interface{}, conf config.Config) {
|
|||||||
// append the cheatsheet title
|
// append the cheatsheet title
|
||||||
sheet.Title,
|
sheet.Title,
|
||||||
// append the cheatsheet path
|
// append the cheatsheet path
|
||||||
display.Faint(fmt.Sprintf("(%s)", sheet.CheatPath), conf),
|
display.Faint(fmt.Sprintf("(%s)", sheet.CheatPath), conf.Color(colorize)),
|
||||||
// indent each line of content
|
// indent each line of content
|
||||||
display.Indent(sheet.Text),
|
display.Indent(sheet.Text),
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -4,13 +4,15 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
|
||||||
"github.com/cheat/cheat/internal/config"
|
"github.com/cheat/cheat/internal/config"
|
||||||
"github.com/cheat/cheat/internal/display"
|
"github.com/cheat/cheat/internal/display"
|
||||||
"github.com/cheat/cheat/internal/sheets"
|
"github.com/cheat/cheat/internal/sheets"
|
||||||
)
|
)
|
||||||
|
|
||||||
// cmdTags lists all tags in use.
|
// cmdTags lists all tags in use.
|
||||||
func cmdTags(_ map[string]interface{}, conf config.Config) {
|
func cmdTags(_ *cobra.Command, _ []string, conf config.Config) {
|
||||||
|
|
||||||
// load the cheatsheets
|
// load the cheatsheets
|
||||||
cheatsheets, err := sheets.Load(conf.Cheatpaths)
|
cheatsheets, err := sheets.Load(conf.Cheatpaths)
|
||||||
|
|||||||
42
cmd/cheat/cmd_update.go
Normal file
42
cmd/cheat/cmd_update.go
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"github.com/go-git/go-git/v5"
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
|
||||||
|
"github.com/cheat/cheat/internal/config"
|
||||||
|
"github.com/cheat/cheat/internal/repo"
|
||||||
|
)
|
||||||
|
|
||||||
|
// cmdUpdate updates git-backed cheatpaths.
|
||||||
|
func cmdUpdate(_ *cobra.Command, _ []string, conf config.Config) {
|
||||||
|
|
||||||
|
hasError := false
|
||||||
|
|
||||||
|
for _, path := range conf.Cheatpaths {
|
||||||
|
err := repo.Pull(path.Path)
|
||||||
|
|
||||||
|
switch {
|
||||||
|
case err == nil:
|
||||||
|
fmt.Printf("%s: ok\n", path.Name)
|
||||||
|
|
||||||
|
case errors.Is(err, git.ErrRepositoryNotExists):
|
||||||
|
fmt.Printf("%s: skipped (not a git repository)\n", path.Name)
|
||||||
|
|
||||||
|
case errors.Is(err, repo.ErrDirtyWorktree):
|
||||||
|
fmt.Printf("%s: skipped (dirty worktree)\n", path.Name)
|
||||||
|
|
||||||
|
default:
|
||||||
|
fmt.Fprintf(os.Stderr, "%s: error (%v)\n", path.Name, err)
|
||||||
|
hasError = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if hasError {
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -5,15 +5,19 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
|
||||||
"github.com/cheat/cheat/internal/config"
|
"github.com/cheat/cheat/internal/config"
|
||||||
"github.com/cheat/cheat/internal/display"
|
"github.com/cheat/cheat/internal/display"
|
||||||
"github.com/cheat/cheat/internal/sheets"
|
"github.com/cheat/cheat/internal/sheets"
|
||||||
)
|
)
|
||||||
|
|
||||||
// cmdView displays a cheatsheet for viewing.
|
// cmdView displays a cheatsheet for viewing.
|
||||||
func cmdView(opts map[string]interface{}, conf config.Config) {
|
func cmdView(cmd *cobra.Command, args []string, conf config.Config) {
|
||||||
|
|
||||||
cheatsheet := opts["<cheatsheet>"].(string)
|
cheatsheet := args[0]
|
||||||
|
|
||||||
|
colorize, _ := cmd.Flags().GetBool("colorize")
|
||||||
|
|
||||||
// load the cheatsheets
|
// load the cheatsheets
|
||||||
cheatsheets, err := sheets.Load(conf.Cheatpaths)
|
cheatsheets, err := sheets.Load(conf.Cheatpaths)
|
||||||
@@ -21,17 +25,17 @@ func cmdView(opts map[string]interface{}, conf config.Config) {
|
|||||||
fmt.Fprintf(os.Stderr, "failed to list cheatsheets: %v\n", err)
|
fmt.Fprintf(os.Stderr, "failed to list cheatsheets: %v\n", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
if cmd.Flags().Changed("tag") {
|
||||||
// filter cheatcheats by tag if --tag was provided
|
tagVal, _ := cmd.Flags().GetString("tag")
|
||||||
if opts["--tag"] != nil {
|
|
||||||
cheatsheets = sheets.Filter(
|
cheatsheets = sheets.Filter(
|
||||||
cheatsheets,
|
cheatsheets,
|
||||||
strings.Split(opts["--tag"].(string), ","),
|
strings.Split(tagVal, ","),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
// if --all was passed, display cheatsheets from all cheatpaths
|
// if --all was passed, display cheatsheets from all cheatpaths
|
||||||
if opts["--all"].(bool) {
|
allFlag, _ := cmd.Flags().GetBool("all")
|
||||||
|
if allFlag {
|
||||||
// iterate over the cheatpaths
|
// iterate over the cheatpaths
|
||||||
out := ""
|
out := ""
|
||||||
for _, cheatpath := range cheatsheets {
|
for _, cheatpath := range cheatsheets {
|
||||||
@@ -42,11 +46,11 @@ func cmdView(opts map[string]interface{}, conf config.Config) {
|
|||||||
// identify the matching cheatsheet
|
// identify the matching cheatsheet
|
||||||
out += fmt.Sprintf("%s %s\n",
|
out += fmt.Sprintf("%s %s\n",
|
||||||
sheet.Title,
|
sheet.Title,
|
||||||
display.Faint(fmt.Sprintf("(%s)", sheet.CheatPath), conf),
|
display.Faint(fmt.Sprintf("(%s)", sheet.CheatPath), conf.Color(colorize)),
|
||||||
)
|
)
|
||||||
|
|
||||||
// apply colorization if requested
|
// apply colorization if requested
|
||||||
if conf.Color(opts) {
|
if conf.Color(colorize) {
|
||||||
sheet.Colorize(conf)
|
sheet.Colorize(conf)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -73,7 +77,7 @@ func cmdView(opts map[string]interface{}, conf config.Config) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// apply colorization if requested
|
// apply colorization if requested
|
||||||
if conf.Color(opts) {
|
if conf.Color(colorize) {
|
||||||
sheet.Colorize(conf)
|
sheet.Colorize(conf)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -5,34 +5,144 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"runtime"
|
"runtime"
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/docopt/docopt-go"
|
|
||||||
"github.com/mitchellh/go-homedir"
|
"github.com/mitchellh/go-homedir"
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
|
||||||
"github.com/cheat/cheat/internal/cheatpath"
|
"github.com/cheat/cheat/internal/cheatpath"
|
||||||
|
"github.com/cheat/cheat/internal/completions"
|
||||||
"github.com/cheat/cheat/internal/config"
|
"github.com/cheat/cheat/internal/config"
|
||||||
"github.com/cheat/cheat/internal/installer"
|
"github.com/cheat/cheat/internal/installer"
|
||||||
)
|
)
|
||||||
|
|
||||||
const version = "4.6.0"
|
const version = "5.1.0"
|
||||||
|
|
||||||
|
var rootCmd = &cobra.Command{
|
||||||
|
Use: "cheat [cheatsheet]",
|
||||||
|
Short: "Create and view interactive cheatsheets on the command-line",
|
||||||
|
Long: `cheat allows you to create and view interactive cheatsheets on the
|
||||||
|
command-line. It was designed to help remind *nix system administrators of
|
||||||
|
options for commands that they use frequently, but not frequently enough to
|
||||||
|
remember.`,
|
||||||
|
Example: ` To initialize a config file:
|
||||||
|
mkdir -p ~/.config/cheat && cheat --init > ~/.config/cheat/conf.yml
|
||||||
|
|
||||||
|
To view the tar cheatsheet:
|
||||||
|
cheat tar
|
||||||
|
|
||||||
|
To edit (or create) the foo cheatsheet:
|
||||||
|
cheat -e foo
|
||||||
|
|
||||||
|
To edit (or create) the foo/bar cheatsheet on the "work" cheatpath:
|
||||||
|
cheat -p work -e foo/bar
|
||||||
|
|
||||||
|
To view all cheatsheet directories:
|
||||||
|
cheat -d
|
||||||
|
|
||||||
|
To list all available cheatsheets:
|
||||||
|
cheat -l
|
||||||
|
|
||||||
|
To briefly list all cheatsheets whose titles match "apt":
|
||||||
|
cheat -b apt
|
||||||
|
|
||||||
|
To list all tags in use:
|
||||||
|
cheat -T
|
||||||
|
|
||||||
|
To list available cheatsheets that are tagged as "personal":
|
||||||
|
cheat -l -t personal
|
||||||
|
|
||||||
|
To search for "ssh" among all cheatsheets, and colorize matches:
|
||||||
|
cheat -c -s ssh
|
||||||
|
|
||||||
|
To search (by regex) for cheatsheets that contain an IP address:
|
||||||
|
cheat -c -r -s '(?:[0-9]{1,3}\.){3}[0-9]{1,3}'
|
||||||
|
|
||||||
|
To remove (delete) the foo/bar cheatsheet:
|
||||||
|
cheat --rm foo/bar
|
||||||
|
|
||||||
|
To update all git-backed cheatpaths:
|
||||||
|
cheat --update
|
||||||
|
|
||||||
|
To view the configuration file path:
|
||||||
|
cheat --conf
|
||||||
|
|
||||||
|
To generate shell completions (bash, zsh, fish, powershell):
|
||||||
|
cheat --completion bash`,
|
||||||
|
RunE: run,
|
||||||
|
Args: cobra.MaximumNArgs(1),
|
||||||
|
SilenceErrors: true,
|
||||||
|
SilenceUsage: true,
|
||||||
|
ValidArgsFunction: completions.Cheatsheets,
|
||||||
|
CompletionOptions: cobra.CompletionOptions{
|
||||||
|
DisableDefaultCmd: true,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
f := rootCmd.Flags()
|
||||||
|
|
||||||
|
// bool flags
|
||||||
|
f.BoolP("all", "a", false, "Search among all cheatpaths")
|
||||||
|
f.BoolP("brief", "b", false, "List cheatsheets without file paths")
|
||||||
|
f.BoolP("colorize", "c", false, "Colorize output")
|
||||||
|
f.BoolP("directories", "d", false, "List cheatsheet directories")
|
||||||
|
f.Bool("init", false, "Write a default config file to stdout")
|
||||||
|
f.BoolP("list", "l", false, "List cheatsheets")
|
||||||
|
f.BoolP("regex", "r", false, "Treat search <phrase> as a regex")
|
||||||
|
f.BoolP("tags", "T", false, "List all tags in use")
|
||||||
|
f.BoolP("update", "u", false, "Update git-backed cheatpaths")
|
||||||
|
f.BoolP("version", "v", false, "Print the version number")
|
||||||
|
f.Bool("conf", false, "Display the config file path")
|
||||||
|
|
||||||
|
// string flags
|
||||||
|
f.StringP("edit", "e", "", "Edit `cheatsheet`")
|
||||||
|
f.StringP("path", "p", "", "Return only sheets found on cheatpath `name`")
|
||||||
|
f.StringP("search", "s", "", "Search cheatsheets for `phrase`")
|
||||||
|
f.StringP("tag", "t", "", "Return only sheets matching `tag`")
|
||||||
|
f.String("rm", "", "Remove (delete) `cheatsheet`")
|
||||||
|
f.String("completion", "", "Generate shell completion script (`shell`: bash, zsh, fish, powershell)")
|
||||||
|
|
||||||
|
// register flag completion functions
|
||||||
|
rootCmd.RegisterFlagCompletionFunc("tag", completions.Tags)
|
||||||
|
rootCmd.RegisterFlagCompletionFunc("path", completions.Paths)
|
||||||
|
rootCmd.RegisterFlagCompletionFunc("edit", completions.Cheatsheets)
|
||||||
|
rootCmd.RegisterFlagCompletionFunc("rm", completions.Cheatsheets)
|
||||||
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
if err := rootCmd.Execute(); err != nil {
|
||||||
|
fmt.Fprintln(os.Stderr, err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// initialize options
|
func run(cmd *cobra.Command, args []string) error {
|
||||||
opts, err := docopt.ParseArgs(usage(), nil, version)
|
f := cmd.Flags()
|
||||||
if err != nil {
|
|
||||||
// panic here, because this should never happen
|
// handle --init early (no config needed)
|
||||||
panic(fmt.Errorf("docopt failed to parse: %v", err))
|
if initFlag, _ := f.GetBool("init"); initFlag {
|
||||||
|
home, err := homedir.Dir()
|
||||||
|
if err != nil {
|
||||||
|
fmt.Fprintf(os.Stderr, "failed to get user home directory: %v\n", err)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
envvars := config.EnvVars()
|
||||||
|
cmdInit(home, envvars)
|
||||||
|
os.Exit(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
// if --init was passed, we don't want to attempt to load a config file.
|
// handle --version early
|
||||||
// Instead, just execute cmd_init and exit
|
if versionFlag, _ := f.GetBool("version"); versionFlag {
|
||||||
if opts["--init"] != nil && opts["--init"] == true {
|
fmt.Println(version)
|
||||||
cmdInit()
|
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// handle --completion early (no config needed)
|
||||||
|
if f.Changed("completion") {
|
||||||
|
shell, _ := f.GetString("completion")
|
||||||
|
return completions.Generate(cmd, shell, os.Stdout)
|
||||||
|
}
|
||||||
|
|
||||||
// get the user's home directory
|
// get the user's home directory
|
||||||
home, err := homedir.Dir()
|
home, err := homedir.Dir()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -41,17 +151,9 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// read the envvars into a map of strings
|
// read the envvars into a map of strings
|
||||||
envvars := map[string]string{}
|
envvars := config.EnvVars()
|
||||||
for _, e := range os.Environ() {
|
|
||||||
// os.Environ() guarantees "key=value" format (see ADR-002)
|
|
||||||
pair := strings.SplitN(e, "=", 2)
|
|
||||||
if runtime.GOOS == "windows" {
|
|
||||||
pair[0] = strings.ToUpper(pair[0])
|
|
||||||
}
|
|
||||||
envvars[pair[0]] = pair[1]
|
|
||||||
}
|
|
||||||
|
|
||||||
// identify the os-specifc paths at which configs may be located
|
// identify the os-specific paths at which configs may be located
|
||||||
confpaths, err := config.Paths(runtime.GOOS, home, envvars)
|
confpaths, err := config.Paths(runtime.GOOS, home, envvars)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintf(os.Stderr, "failed to load config: %v\n", err)
|
fmt.Fprintf(os.Stderr, "failed to load config: %v\n", err)
|
||||||
@@ -92,7 +194,7 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// initialize the configs
|
// initialize the configs
|
||||||
conf, err := config.New(opts, confpath, true)
|
conf, err := config.New(confpath, true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintf(os.Stderr, "failed to load config: %v\n", err)
|
fmt.Fprintf(os.Stderr, "failed to load config: %v\n", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
@@ -105,10 +207,11 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// filter the cheatpaths if --path was passed
|
// filter the cheatpaths if --path was passed
|
||||||
if opts["--path"] != nil {
|
if f.Changed("path") {
|
||||||
|
pathVal, _ := f.GetString("path")
|
||||||
conf.Cheatpaths, err = cheatpath.Filter(
|
conf.Cheatpaths, err = cheatpath.Filter(
|
||||||
conf.Cheatpaths,
|
conf.Cheatpaths,
|
||||||
opts["--path"].(string),
|
pathVal,
|
||||||
)
|
)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintf(os.Stderr, "invalid option --path: %v\n", err)
|
fmt.Fprintf(os.Stderr, "invalid option --path: %v\n", err)
|
||||||
@@ -117,41 +220,48 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// determine which command to execute
|
// determine which command to execute
|
||||||
var cmd func(map[string]interface{}, config.Config)
|
confFlag, _ := f.GetBool("conf")
|
||||||
|
dirFlag, _ := f.GetBool("directories")
|
||||||
|
listFlag, _ := f.GetBool("list")
|
||||||
|
briefFlag, _ := f.GetBool("brief")
|
||||||
|
tagsFlag, _ := f.GetBool("tags")
|
||||||
|
updateFlag, _ := f.GetBool("update")
|
||||||
|
tagVal, _ := f.GetString("tag")
|
||||||
|
|
||||||
switch {
|
switch {
|
||||||
case opts["--conf"].(bool):
|
case confFlag:
|
||||||
cmd = cmdConf
|
cmdConf(cmd, args, conf)
|
||||||
|
|
||||||
case opts["--directories"].(bool):
|
case dirFlag:
|
||||||
cmd = cmdDirectories
|
cmdDirectories(cmd, args, conf)
|
||||||
|
|
||||||
case opts["--edit"] != nil:
|
case f.Changed("edit"):
|
||||||
cmd = cmdEdit
|
cmdEdit(cmd, args, conf)
|
||||||
|
|
||||||
case opts["--list"].(bool):
|
case listFlag, briefFlag:
|
||||||
cmd = cmdList
|
cmdList(cmd, args, conf)
|
||||||
|
|
||||||
case opts["--tags"].(bool):
|
case tagsFlag:
|
||||||
cmd = cmdTags
|
cmdTags(cmd, args, conf)
|
||||||
|
|
||||||
case opts["--search"] != nil:
|
case updateFlag:
|
||||||
cmd = cmdSearch
|
cmdUpdate(cmd, args, conf)
|
||||||
|
|
||||||
case opts["--rm"] != nil:
|
case f.Changed("search"):
|
||||||
cmd = cmdRemove
|
cmdSearch(cmd, args, conf)
|
||||||
|
|
||||||
case opts["<cheatsheet>"] != nil:
|
case f.Changed("rm"):
|
||||||
cmd = cmdView
|
cmdRemove(cmd, args, conf)
|
||||||
|
|
||||||
case opts["--tag"] != nil && opts["--tag"].(string) != "":
|
case len(args) > 0:
|
||||||
cmd = cmdList
|
cmdView(cmd, args, conf)
|
||||||
|
|
||||||
|
case tagVal != "":
|
||||||
|
cmdList(cmd, args, conf)
|
||||||
|
|
||||||
default:
|
default:
|
||||||
fmt.Println(usage())
|
return cmd.Help()
|
||||||
os.Exit(0)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// execute the command
|
return nil
|
||||||
cmd(opts, conf)
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,64 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
// usage returns the usage text for the cheat command
|
|
||||||
func usage() string {
|
|
||||||
return `Usage:
|
|
||||||
cheat [options] [<cheatsheet>]
|
|
||||||
|
|
||||||
Options:
|
|
||||||
--init Write a default config file to stdout
|
|
||||||
-a --all Search among all cheatpaths
|
|
||||||
-c --colorize Colorize output
|
|
||||||
-d --directories List cheatsheet directories
|
|
||||||
-e --edit=<cheatsheet> Edit <cheatsheet>
|
|
||||||
-l --list List cheatsheets
|
|
||||||
-p --path=<name> Return only sheets found on cheatpath <name>
|
|
||||||
-r --regex Treat search <phrase> as a regex
|
|
||||||
-s --search=<phrase> Search cheatsheets for <phrase>
|
|
||||||
-t --tag=<tag> Return only sheets matching <tag>
|
|
||||||
-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:
|
|
||||||
|
|
||||||
To initialize a config file:
|
|
||||||
mkdir -p ~/.config/cheat && cheat --init > ~/.config/cheat/conf.yml
|
|
||||||
|
|
||||||
To view the tar cheatsheet:
|
|
||||||
cheat tar
|
|
||||||
|
|
||||||
To edit (or create) the foo cheatsheet:
|
|
||||||
cheat -e foo
|
|
||||||
|
|
||||||
To edit (or create) the foo/bar cheatsheet on the "work" cheatpath:
|
|
||||||
cheat -p work -e foo/bar
|
|
||||||
|
|
||||||
To view all cheatsheet directories:
|
|
||||||
cheat -d
|
|
||||||
|
|
||||||
To list all available cheatsheets:
|
|
||||||
cheat -l
|
|
||||||
|
|
||||||
To list all cheatsheets whose titles match "apt":
|
|
||||||
cheat -l apt
|
|
||||||
|
|
||||||
To list all tags in use:
|
|
||||||
cheat -T
|
|
||||||
|
|
||||||
To list available cheatsheets that are tagged as "personal":
|
|
||||||
cheat -l -t personal
|
|
||||||
|
|
||||||
To search for "ssh" among all cheatsheets, and colorize matches:
|
|
||||||
cheat -c -s ssh
|
|
||||||
|
|
||||||
To search (by regex) for cheatsheets that contain an IP address:
|
|
||||||
cheat -c -r -s '(?:[0-9]{1,3}\.){3}[0-9]{1,3}'
|
|
||||||
|
|
||||||
To remove (delete) the foo/bar cheatsheet:
|
|
||||||
cheat --rm foo/bar
|
|
||||||
|
|
||||||
To view the configuration file path:
|
|
||||||
cheat --conf`
|
|
||||||
}
|
|
||||||
96
doc/cheat.1
96
doc/cheat.1
@@ -23,6 +23,9 @@ Display the config file path.
|
|||||||
\-a, \[en]all
|
\-a, \[en]all
|
||||||
Search among all cheatpaths.
|
Search among all cheatpaths.
|
||||||
.TP
|
.TP
|
||||||
|
\-b, \[en]brief
|
||||||
|
List cheatsheets without file paths.
|
||||||
|
.TP
|
||||||
\-c, \[en]colorize
|
\-c, \[en]colorize
|
||||||
Colorize output.
|
Colorize output.
|
||||||
.TP
|
.TP
|
||||||
@@ -50,11 +53,19 @@ Filter only to sheets tagged with \f[I]TAG\f[R].
|
|||||||
\-T, \[en]tags
|
\-T, \[en]tags
|
||||||
List all tags in use.
|
List all tags in use.
|
||||||
.TP
|
.TP
|
||||||
|
\-u, \[en]update
|
||||||
|
Update git\-backed cheatpaths by pulling the latest changes.
|
||||||
|
.TP
|
||||||
\-v, \[en]version
|
\-v, \[en]version
|
||||||
Print the version number.
|
Print the version number.
|
||||||
.TP
|
.TP
|
||||||
\[en]rm=\f[I]CHEATSHEET\f[R]
|
\[en]rm=\f[I]CHEATSHEET\f[R]
|
||||||
Remove (deletes) \f[I]CHEATSHEET\f[R].
|
Remove (deletes) \f[I]CHEATSHEET\f[R].
|
||||||
|
.TP
|
||||||
|
\[en]completion=\f[I]SHELL\f[R]
|
||||||
|
Generate a shell completion script.
|
||||||
|
\f[I]SHELL\f[R] must be one of: \f[B]bash\f[R], \f[B]zsh\f[R],
|
||||||
|
\f[B]fish\f[R], \f[B]powershell\f[R].
|
||||||
.SH EXAMPLES
|
.SH EXAMPLES
|
||||||
.TP
|
.TP
|
||||||
To view the foo cheatsheet:
|
To view the foo cheatsheet:
|
||||||
@@ -72,8 +83,8 @@ cheat \-d
|
|||||||
To list all available cheatsheets:
|
To list all available cheatsheets:
|
||||||
cheat \-l
|
cheat \-l
|
||||||
.TP
|
.TP
|
||||||
To list all cheatsheets whose titles match `apt':
|
To briefly list all cheatsheets whose titles match `apt':
|
||||||
cheat \-l \f[I]apt\f[R]
|
cheat \-b \f[I]apt\f[R]
|
||||||
.TP
|
.TP
|
||||||
To list all tags in use:
|
To list all tags in use:
|
||||||
cheat \-T
|
cheat \-T
|
||||||
@@ -90,23 +101,31 @@ cheat \-c \-r \-s \f[I]`(?:[0\-9]{1,3}.){3}[0\-9]{1,3}'\f[R]
|
|||||||
To remove (delete) the foo/bar cheatsheet:
|
To remove (delete) the foo/bar cheatsheet:
|
||||||
cheat \[en]rm \f[I]foo/bar\f[R]
|
cheat \[en]rm \f[I]foo/bar\f[R]
|
||||||
.TP
|
.TP
|
||||||
|
To update all git\-backed cheatpaths:
|
||||||
|
cheat \[en]update
|
||||||
|
.TP
|
||||||
|
To update only the `community' cheatpath:
|
||||||
|
cheat \-u \-p \f[I]community\f[R]
|
||||||
|
.TP
|
||||||
To view the configuration file path:
|
To view the configuration file path:
|
||||||
cheat \[en]conf
|
cheat \[en]conf
|
||||||
.SH FILES
|
.SH FILES
|
||||||
.SS Configuration
|
.SS Configuration
|
||||||
\f[B]cheat\f[R] 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[R].
|
named \f[I]conf.yml\f[R].
|
||||||
\f[B]cheat\f[R] will search for \f[I]conf.yaml\f[R] in varying
|
\f[B]cheat\f[R] will search for \f[I]conf.yml\f[R] in varying locations,
|
||||||
locations, depending upon your platform:
|
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[R]
|
\f[B]CHEAT_CONFIG_PATH\f[R]
|
||||||
.IP "2." 3
|
.IP "2." 3
|
||||||
\f[B]XDG_CONFIG_HOME\f[R]/cheat/conf.yaml
|
\f[B]XDG_CONFIG_HOME\f[R]/cheat/conf.yml
|
||||||
.IP "3." 3
|
.IP "3." 3
|
||||||
\f[B]$HOME\f[R]/.config/cheat/conf.yml
|
\f[B]$HOME\f[R]/.config/cheat/conf.yml
|
||||||
.IP "4." 3
|
.IP "4." 3
|
||||||
\f[B]$HOME\f[R]/.cheat/conf.yml
|
\f[B]$HOME\f[R]/.cheat/conf.yml
|
||||||
|
.IP "5." 3
|
||||||
|
/etc/cheat/conf.yml
|
||||||
.SS Windows
|
.SS Windows
|
||||||
.IP "1." 3
|
.IP "1." 3
|
||||||
\f[B]CHEAT_CONFIG_PATH\f[R]
|
\f[B]CHEAT_CONFIG_PATH\f[R]
|
||||||
@@ -116,7 +135,7 @@ locations, depending upon your platform:
|
|||||||
\f[B]PROGRAMDATA\f[R]/cheat/conf.yml
|
\f[B]PROGRAMDATA\f[R]/cheat/conf.yml
|
||||||
.PP
|
.PP
|
||||||
\f[B]cheat\f[R] 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[R] encountered will be respected.
|
The first \f[I]conf.yml\f[R] encountered will be respected.
|
||||||
.PP
|
.PP
|
||||||
If \f[B]cheat\f[R] cannot locate a config file, it will ask if you\[cq]d
|
If \f[B]cheat\f[R] cannot locate a config file, it will ask if you\[cq]d
|
||||||
like to generate one automatically.
|
like to generate one automatically.
|
||||||
@@ -126,43 +145,58 @@ location for your platform.
|
|||||||
.SS Cheatpaths
|
.SS Cheatpaths
|
||||||
\f[B]cheat\f[R] 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[R], and viewed via
|
Cheatpaths may be configured in \f[I]conf.yml\f[R], and viewed via
|
||||||
\f[B]cheat \-d\f[R].
|
\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
|
||||||
Autocompletion scripts for \f[B]bash\f[R], \f[B]zsh\f[R], and
|
\f[B]cheat\f[R] can generate shell completion scripts for
|
||||||
\f[B]fish\f[R] are available for download:
|
\f[B]bash\f[R], \f[B]zsh\f[R], \f[B]fish\f[R], and \f[B]powershell\f[R]
|
||||||
.IP \[bu] 2
|
via the \f[B]\[en]completion\f[R] flag:
|
||||||
\c
|
.IP
|
||||||
.UR https://github.com/cheat/cheat/blob/master/scripts/cheat.bash
|
.EX
|
||||||
.UE \c
|
cheat \-\-completion bash
|
||||||
.IP \[bu] 2
|
cheat \-\-completion zsh
|
||||||
\c
|
cheat \-\-completion fish
|
||||||
.UR https://github.com/cheat/cheat/blob/master/scripts/cheat.fish
|
cheat \-\-completion powershell
|
||||||
.UE \c
|
.EE
|
||||||
.IP \[bu] 2
|
|
||||||
\c
|
|
||||||
.UR https://github.com/cheat/cheat/blob/master/scripts/cheat.zsh
|
|
||||||
.UE \c
|
|
||||||
.PP
|
.PP
|
||||||
The \f[B]bash\f[R] and \f[B]zsh\f[R] scripts provide optional
|
Completions are dynamically generated and include cheatsheet names,
|
||||||
integration with \f[B]fzf\f[R], if the latter is available on your
|
tags, and cheatpath names.
|
||||||
\f[B]PATH\f[R].
|
|
||||||
.PP
|
.PP
|
||||||
The installation process will vary per system and shell configuration,
|
To install completions, pipe the output to the appropriate location for
|
||||||
and thus will not be discussed here.
|
your shell.
|
||||||
|
For example, on \f[B]bash\f[R]:
|
||||||
|
.IP
|
||||||
|
.EX
|
||||||
|
cheat \-\-completion bash > /etc/bash_completion.d/cheat
|
||||||
|
.EE
|
||||||
|
.PP
|
||||||
|
Or for the current user only:
|
||||||
|
.IP
|
||||||
|
.EX
|
||||||
|
cheat \-\-completion bash > \[ti]/.local/share/bash\-completion/completions/cheat
|
||||||
|
.EE
|
||||||
|
.PP
|
||||||
|
For \f[B]zsh\f[R], you may need to add the completions directory to your
|
||||||
|
\f[B]fpath\f[R]:
|
||||||
|
.IP
|
||||||
|
.EX
|
||||||
|
cheat \-\-completion zsh > \[dq]${fpath[1]}/_cheat\[dq]
|
||||||
|
.EE
|
||||||
|
.PP
|
||||||
|
For \f[B]fish\f[R]:
|
||||||
|
.IP
|
||||||
|
.EX
|
||||||
|
cheat \-\-completion fish > \[ti]/.config/fish/completions/cheat.fish
|
||||||
|
.EE
|
||||||
.SH ENVIRONMENT
|
.SH ENVIRONMENT
|
||||||
.TP
|
.TP
|
||||||
\f[B]CHEAT_CONFIG_PATH\f[R]
|
\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[R] 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.
|
||||||
.TP
|
|
||||||
\f[B]CHEAT_USE_FZF\f[R]
|
|
||||||
If set, autocompletion scripts will attempt to integrate with
|
|
||||||
\f[B]fzf\f[R].
|
|
||||||
.SH RETURN VALUES
|
.SH RETURN VALUES
|
||||||
.IP "0." 3
|
.IP "0." 3
|
||||||
Successful termination
|
Successful termination
|
||||||
|
|||||||
@@ -29,6 +29,9 @@ OPTIONS
|
|||||||
-a, --all
|
-a, --all
|
||||||
: Search among all cheatpaths.
|
: Search among all cheatpaths.
|
||||||
|
|
||||||
|
-b, --brief
|
||||||
|
: List cheatsheets without file paths.
|
||||||
|
|
||||||
-c, --colorize
|
-c, --colorize
|
||||||
: Colorize output.
|
: Colorize output.
|
||||||
|
|
||||||
@@ -56,12 +59,19 @@ OPTIONS
|
|||||||
-T, --tags
|
-T, --tags
|
||||||
: List all tags in use.
|
: List all tags in use.
|
||||||
|
|
||||||
|
-u, --update
|
||||||
|
: Update git-backed cheatpaths by pulling the latest changes.
|
||||||
|
|
||||||
-v, --version
|
-v, --version
|
||||||
: Print the version number.
|
: Print the version number.
|
||||||
|
|
||||||
--rm=_CHEATSHEET_
|
--rm=_CHEATSHEET_
|
||||||
: Remove (deletes) _CHEATSHEET_.
|
: Remove (deletes) _CHEATSHEET_.
|
||||||
|
|
||||||
|
--completion=_SHELL_
|
||||||
|
: Generate a shell completion script. _SHELL_ must be one of: **bash**,
|
||||||
|
**zsh**, **fish**, **powershell**.
|
||||||
|
|
||||||
|
|
||||||
EXAMPLES
|
EXAMPLES
|
||||||
========
|
========
|
||||||
@@ -81,8 +91,8 @@ To view all cheatsheet directories:
|
|||||||
To list all available cheatsheets:
|
To list all available cheatsheets:
|
||||||
: cheat -l
|
: cheat -l
|
||||||
|
|
||||||
To list all cheatsheets whose titles match 'apt':
|
To briefly list all cheatsheets whose titles match 'apt':
|
||||||
: cheat -l _apt_
|
: cheat -b _apt_
|
||||||
|
|
||||||
To list all tags in use:
|
To list all tags in use:
|
||||||
: cheat -T
|
: cheat -T
|
||||||
@@ -99,6 +109,12 @@ To search (by regex) for cheatsheets that contain an IP address:
|
|||||||
To remove (delete) the foo/bar cheatsheet:
|
To remove (delete) the foo/bar cheatsheet:
|
||||||
: cheat --rm _foo/bar_
|
: cheat --rm _foo/bar_
|
||||||
|
|
||||||
|
To update all git-backed cheatpaths:
|
||||||
|
: cheat --update
|
||||||
|
|
||||||
|
To update only the 'community' cheatpath:
|
||||||
|
: cheat -u -p _community_
|
||||||
|
|
||||||
To view the configuration file path:
|
To view the configuration file path:
|
||||||
: cheat --conf
|
: cheat --conf
|
||||||
|
|
||||||
@@ -109,15 +125,16 @@ FILES
|
|||||||
Configuration
|
Configuration
|
||||||
-------------
|
-------------
|
||||||
**cheat** is configured via a YAML file that is conventionally named
|
**cheat** is configured via a YAML file that is conventionally named
|
||||||
_conf.yaml_. **cheat** will search for _conf.yaml_ in varying locations,
|
_conf.yml_. **cheat** will search for _conf.yml_ in varying locations,
|
||||||
depending upon your platform:
|
depending upon your platform:
|
||||||
|
|
||||||
### Linux, OSX, and other Unixes ###
|
### Linux, OSX, and other Unixes ###
|
||||||
|
|
||||||
1. **CHEAT_CONFIG_PATH**
|
1. **CHEAT_CONFIG_PATH**
|
||||||
2. **XDG_CONFIG_HOME**/cheat/conf.yaml
|
2. **XDG_CONFIG_HOME**/cheat/conf.yml
|
||||||
3. **$HOME**/.config/cheat/conf.yml
|
3. **$HOME**/.config/cheat/conf.yml
|
||||||
4. **$HOME**/.cheat/conf.yml
|
4. **$HOME**/.cheat/conf.yml
|
||||||
|
5. /etc/cheat/conf.yml
|
||||||
|
|
||||||
### Windows ###
|
### Windows ###
|
||||||
|
|
||||||
@@ -125,7 +142,7 @@ depending upon your platform:
|
|||||||
2. **APPDATA**/cheat/conf.yml
|
2. **APPDATA**/cheat/conf.yml
|
||||||
3. **PROGRAMDATA**/cheat/conf.yml
|
3. **PROGRAMDATA**/cheat/conf.yml
|
||||||
|
|
||||||
**cheat** will search in the order specified above. The first _conf.yaml_
|
**cheat** will search in the order specified above. The first _conf.yml_
|
||||||
encountered will be respected.
|
encountered will be respected.
|
||||||
|
|
||||||
If **cheat** cannot locate a config file, it will ask if you'd like to generate
|
If **cheat** cannot locate a config file, it will ask if you'd like to generate
|
||||||
@@ -137,7 +154,7 @@ for your platform.
|
|||||||
Cheatpaths
|
Cheatpaths
|
||||||
----------
|
----------
|
||||||
**cheat** reads its cheatsheets from "cheatpaths", which are the directories in
|
**cheat** reads its cheatsheets from "cheatpaths", which are the directories in
|
||||||
which cheatsheets are stored. Cheatpaths may be configured in _conf.yaml_, and
|
which cheatsheets are stored. Cheatpaths may be configured in _conf.yml_, and
|
||||||
viewed via **cheat -d**.
|
viewed via **cheat -d**.
|
||||||
|
|
||||||
For detailed instructions on how to configure cheatpaths, please refer to the
|
For detailed instructions on how to configure cheatpaths, please refer to the
|
||||||
@@ -146,18 +163,33 @@ comments in conf.yml.
|
|||||||
|
|
||||||
Autocompletion
|
Autocompletion
|
||||||
--------------
|
--------------
|
||||||
Autocompletion scripts for **bash**, **zsh**, and **fish** are available for
|
**cheat** can generate shell completion scripts for **bash**, **zsh**,
|
||||||
download:
|
**fish**, and **powershell** via the **--completion** flag:
|
||||||
|
|
||||||
- <https://github.com/cheat/cheat/blob/master/scripts/cheat.bash>
|
cheat --completion bash
|
||||||
- <https://github.com/cheat/cheat/blob/master/scripts/cheat.fish>
|
cheat --completion zsh
|
||||||
- <https://github.com/cheat/cheat/blob/master/scripts/cheat.zsh>
|
cheat --completion fish
|
||||||
|
cheat --completion powershell
|
||||||
|
|
||||||
The **bash** and **zsh** scripts provide optional integration with **fzf**, if
|
Completions are dynamically generated and include cheatsheet names, tags, and
|
||||||
the latter is available on your **PATH**.
|
cheatpath names.
|
||||||
|
|
||||||
The installation process will vary per system and shell configuration, and thus
|
To install completions, pipe the output to the appropriate location for your
|
||||||
will not be discussed here.
|
shell. For example, on **bash**:
|
||||||
|
|
||||||
|
cheat --completion bash > /etc/bash_completion.d/cheat
|
||||||
|
|
||||||
|
Or for the current user only:
|
||||||
|
|
||||||
|
cheat --completion bash > ~/.local/share/bash-completion/completions/cheat
|
||||||
|
|
||||||
|
For **zsh**, you may need to add the completions directory to your **fpath**:
|
||||||
|
|
||||||
|
cheat --completion zsh > "${fpath[1]}/_cheat"
|
||||||
|
|
||||||
|
For **fish**:
|
||||||
|
|
||||||
|
cheat --completion fish > ~/.config/fish/completions/cheat.fish
|
||||||
|
|
||||||
|
|
||||||
ENVIRONMENT
|
ENVIRONMENT
|
||||||
@@ -168,10 +200,6 @@ ENVIRONMENT
|
|||||||
: The path at which the config file is available. If **CHEAT_CONFIG_PATH** is
|
: The path at which the config file is available. If **CHEAT_CONFIG_PATH** is
|
||||||
set, all other config paths will be ignored.
|
set, all other config paths will be ignored.
|
||||||
|
|
||||||
**CHEAT_USE_FZF**
|
|
||||||
|
|
||||||
: If set, autocompletion scripts will attempt to integrate with **fzf**.
|
|
||||||
|
|
||||||
RETURN VALUES
|
RETURN VALUES
|
||||||
=============
|
=============
|
||||||
|
|
||||||
|
|||||||
4
go.mod
4
go.mod
@@ -5,10 +5,10 @@ go 1.26
|
|||||||
require (
|
require (
|
||||||
github.com/alecthomas/chroma/v2 v2.23.1
|
github.com/alecthomas/chroma/v2 v2.23.1
|
||||||
github.com/davecgh/go-spew v1.1.1
|
github.com/davecgh/go-spew v1.1.1
|
||||||
github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815
|
|
||||||
github.com/go-git/go-git/v5 v5.16.5
|
github.com/go-git/go-git/v5 v5.16.5
|
||||||
github.com/mattn/go-isatty v0.0.20
|
github.com/mattn/go-isatty v0.0.20
|
||||||
github.com/mitchellh/go-homedir v1.1.0
|
github.com/mitchellh/go-homedir v1.1.0
|
||||||
|
github.com/spf13/cobra v1.10.2
|
||||||
gopkg.in/yaml.v3 v3.0.1
|
gopkg.in/yaml.v3 v3.0.1
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -23,12 +23,14 @@ require (
|
|||||||
github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 // indirect
|
github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376 // indirect
|
||||||
github.com/go-git/go-billy/v5 v5.7.0 // indirect
|
github.com/go-git/go-billy/v5 v5.7.0 // indirect
|
||||||
github.com/golang/groupcache v0.0.0-20241129210726-2c02b8208cf8 // indirect
|
github.com/golang/groupcache v0.0.0-20241129210726-2c02b8208cf8 // indirect
|
||||||
|
github.com/inconshreveable/mousetrap v1.1.0 // indirect
|
||||||
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
|
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 // indirect
|
||||||
github.com/kevinburke/ssh_config v1.5.0 // indirect
|
github.com/kevinburke/ssh_config v1.5.0 // indirect
|
||||||
github.com/klauspost/cpuid/v2 v2.3.0 // indirect
|
github.com/klauspost/cpuid/v2 v2.3.0 // indirect
|
||||||
github.com/pjbgf/sha1cd v0.5.0 // indirect
|
github.com/pjbgf/sha1cd v0.5.0 // indirect
|
||||||
github.com/sergi/go-diff v1.4.0 // indirect
|
github.com/sergi/go-diff v1.4.0 // indirect
|
||||||
github.com/skeema/knownhosts v1.3.2 // indirect
|
github.com/skeema/knownhosts v1.3.2 // indirect
|
||||||
|
github.com/spf13/pflag v1.0.9 // indirect
|
||||||
github.com/xanzy/ssh-agent v0.3.3 // indirect
|
github.com/xanzy/ssh-agent v0.3.3 // indirect
|
||||||
golang.org/x/crypto v0.48.0 // indirect
|
golang.org/x/crypto v0.48.0 // indirect
|
||||||
golang.org/x/net v0.50.0 // indirect
|
golang.org/x/net v0.50.0 // indirect
|
||||||
|
|||||||
11
go.sum
11
go.sum
@@ -17,6 +17,7 @@ github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5 h1:0CwZNZbxp69SHPd
|
|||||||
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5/go.mod h1:wHh0iHkYZB8zMSxRWpUBQtwG5a7fFgvEO+odwuTv2gs=
|
github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5/go.mod h1:wHh0iHkYZB8zMSxRWpUBQtwG5a7fFgvEO+odwuTv2gs=
|
||||||
github.com/cloudflare/circl v1.6.3 h1:9GPOhQGF9MCYUeXyMYlqTR6a5gTrgR/fBLXvUgtVcg8=
|
github.com/cloudflare/circl v1.6.3 h1:9GPOhQGF9MCYUeXyMYlqTR6a5gTrgR/fBLXvUgtVcg8=
|
||||||
github.com/cloudflare/circl v1.6.3/go.mod h1:2eXP6Qfat4O/Yhh8BznvKnJ+uzEoTQ6jVKJRn81BiS4=
|
github.com/cloudflare/circl v1.6.3/go.mod h1:2eXP6Qfat4O/Yhh8BznvKnJ+uzEoTQ6jVKJRn81BiS4=
|
||||||
|
github.com/cpuguy83/go-md2man/v2 v2.0.6/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g=
|
||||||
github.com/cyphar/filepath-securejoin v0.6.1 h1:5CeZ1jPXEiYt3+Z6zqprSAgSWiggmpVyciv8syjIpVE=
|
github.com/cyphar/filepath-securejoin v0.6.1 h1:5CeZ1jPXEiYt3+Z6zqprSAgSWiggmpVyciv8syjIpVE=
|
||||||
github.com/cyphar/filepath-securejoin v0.6.1/go.mod h1:A8hd4EnAeyujCJRrICiOWqjS1AX0a9kM5XL+NwKoYSc=
|
github.com/cyphar/filepath-securejoin v0.6.1/go.mod h1:A8hd4EnAeyujCJRrICiOWqjS1AX0a9kM5XL+NwKoYSc=
|
||||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
@@ -24,8 +25,6 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
|
|||||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
github.com/dlclark/regexp2 v1.11.5 h1:Q/sSnsKerHeCkc/jSTNq1oCm7KiVgUMZRDUoRu0JQZQ=
|
github.com/dlclark/regexp2 v1.11.5 h1:Q/sSnsKerHeCkc/jSTNq1oCm7KiVgUMZRDUoRu0JQZQ=
|
||||||
github.com/dlclark/regexp2 v1.11.5/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8=
|
github.com/dlclark/regexp2 v1.11.5/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8=
|
||||||
github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815 h1:bWDMxwH3px2JBh6AyO7hdCn/PkvCZXii8TGj7sbtEbQ=
|
|
||||||
github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815/go.mod h1:WwZ+bS3ebgob9U8Nd0kOddGdZWjyMGR8Wziv+TBNwSE=
|
|
||||||
github.com/elazarl/goproxy v1.7.2 h1:Y2o6urb7Eule09PjlhQRGNsqRfPmYI3KKQLFpCAV3+o=
|
github.com/elazarl/goproxy v1.7.2 h1:Y2o6urb7Eule09PjlhQRGNsqRfPmYI3KKQLFpCAV3+o=
|
||||||
github.com/elazarl/goproxy v1.7.2/go.mod h1:82vkLNir0ALaW14Rc399OTTjyNREgmdL2cVoIbS6XaE=
|
github.com/elazarl/goproxy v1.7.2/go.mod h1:82vkLNir0ALaW14Rc399OTTjyNREgmdL2cVoIbS6XaE=
|
||||||
github.com/emirpasic/gods v1.18.1 h1:FXtiHYKDGKCW2KzwZKx0iC0PQmdlorYgdFG9jPXJ1Bc=
|
github.com/emirpasic/gods v1.18.1 h1:FXtiHYKDGKCW2KzwZKx0iC0PQmdlorYgdFG9jPXJ1Bc=
|
||||||
@@ -46,6 +45,8 @@ github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
|
|||||||
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
|
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
|
||||||
github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUqJM=
|
github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUqJM=
|
||||||
github.com/hexops/gotextdiff v1.0.3/go.mod h1:pSWU5MAI3yDq+fZBTazCSJysOMbxWL1BSow5/V2vxeg=
|
github.com/hexops/gotextdiff v1.0.3/go.mod h1:pSWU5MAI3yDq+fZBTazCSJysOMbxWL1BSow5/V2vxeg=
|
||||||
|
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
|
||||||
|
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
|
||||||
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 h1:BQSFePA1RWJOlocH6Fxy8MmwDt+yVQYULKfN0RoTN8A=
|
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 h1:BQSFePA1RWJOlocH6Fxy8MmwDt+yVQYULKfN0RoTN8A=
|
||||||
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99/go.mod h1:1lJo3i6rXxKeerYnT8Nvf0QmHCRC1n8sfWVwXF2Frvo=
|
github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99/go.mod h1:1lJo3i6rXxKeerYnT8Nvf0QmHCRC1n8sfWVwXF2Frvo=
|
||||||
github.com/kevinburke/ssh_config v1.5.0 h1:3cPZmE54xb5j3G5xQCjSvokqNwU2uW+3ry1+PRLSPpA=
|
github.com/kevinburke/ssh_config v1.5.0 h1:3cPZmE54xb5j3G5xQCjSvokqNwU2uW+3ry1+PRLSPpA=
|
||||||
@@ -73,11 +74,16 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
|
|||||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||||
github.com/rogpeppe/go-internal v1.14.1 h1:UQB4HGPB6osV0SQTLymcB4TgvyWu6ZyliaW0tI/otEQ=
|
github.com/rogpeppe/go-internal v1.14.1 h1:UQB4HGPB6osV0SQTLymcB4TgvyWu6ZyliaW0tI/otEQ=
|
||||||
github.com/rogpeppe/go-internal v1.14.1/go.mod h1:MaRKkUm5W0goXpeCfT7UZI6fk/L7L7so1lCWt35ZSgc=
|
github.com/rogpeppe/go-internal v1.14.1/go.mod h1:MaRKkUm5W0goXpeCfT7UZI6fk/L7L7so1lCWt35ZSgc=
|
||||||
|
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
|
||||||
github.com/sergi/go-diff v1.4.0 h1:n/SP9D5ad1fORl+llWyN+D6qoUETXNZARKjyY2/KVCw=
|
github.com/sergi/go-diff v1.4.0 h1:n/SP9D5ad1fORl+llWyN+D6qoUETXNZARKjyY2/KVCw=
|
||||||
github.com/sergi/go-diff v1.4.0/go.mod h1:A0bzQcvG0E7Rwjx0REVgAGH58e96+X0MeOfepqsbeW4=
|
github.com/sergi/go-diff v1.4.0/go.mod h1:A0bzQcvG0E7Rwjx0REVgAGH58e96+X0MeOfepqsbeW4=
|
||||||
github.com/sirupsen/logrus v1.7.0/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
|
github.com/sirupsen/logrus v1.7.0/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
|
||||||
github.com/skeema/knownhosts v1.3.2 h1:EDL9mgf4NzwMXCTfaxSD/o/a5fxDw/xL9nkU28JjdBg=
|
github.com/skeema/knownhosts v1.3.2 h1:EDL9mgf4NzwMXCTfaxSD/o/a5fxDw/xL9nkU28JjdBg=
|
||||||
github.com/skeema/knownhosts v1.3.2/go.mod h1:bEg3iQAuw+jyiw+484wwFJoKSLwcfd7fqRy+N0QTiow=
|
github.com/skeema/knownhosts v1.3.2/go.mod h1:bEg3iQAuw+jyiw+484wwFJoKSLwcfd7fqRy+N0QTiow=
|
||||||
|
github.com/spf13/cobra v1.10.2 h1:DMTTonx5m65Ic0GOoRY2c16WCbHxOOw6xxezuLaBpcU=
|
||||||
|
github.com/spf13/cobra v1.10.2/go.mod h1:7C1pvHqHw5A4vrJfjNwvOdzYu0Gml16OCs2GRiTUUS4=
|
||||||
|
github.com/spf13/pflag v1.0.9 h1:9exaQaMOCwffKiiiYk6/BndUBv+iRViNW+4lEMi0PvY=
|
||||||
|
github.com/spf13/pflag v1.0.9/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
|
||||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||||
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
|
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
|
||||||
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
|
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
|
||||||
@@ -85,6 +91,7 @@ github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu
|
|||||||
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
|
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
|
||||||
github.com/xanzy/ssh-agent v0.3.3 h1:+/15pJfg/RsTxqYcX6fHqOXZwwMP+2VyYWJeWM2qQFM=
|
github.com/xanzy/ssh-agent v0.3.3 h1:+/15pJfg/RsTxqYcX6fHqOXZwwMP+2VyYWJeWM2qQFM=
|
||||||
github.com/xanzy/ssh-agent v0.3.3/go.mod h1:6dzNDKs0J9rVPHPhaGCukekBHKqfl+L3KghI1Bc68Uw=
|
github.com/xanzy/ssh-agent v0.3.3/go.mod h1:6dzNDKs0J9rVPHPhaGCukekBHKqfl+L3KghI1Bc68Uw=
|
||||||
|
go.yaml.in/yaml/v3 v3.0.4/go.mod h1:DhzuOOF2ATzADvBadXxruRBLzYTpT36CKvDb3+aBEFg=
|
||||||
golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
|
golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
|
||||||
golang.org/x/crypto v0.48.0 h1:/VRzVqiRSggnhY7gNRxPauEQ5Drw9haKdM0jqfcCFts=
|
golang.org/x/crypto v0.48.0 h1:/VRzVqiRSggnhY7gNRxPauEQ5Drw9haKdM0jqfcCFts=
|
||||||
golang.org/x/crypto v0.48.0/go.mod h1:r0kV5h3qnFPlQnBSrULhlsRfryS2pmewsg+XfMgkVos=
|
golang.org/x/crypto v0.48.0/go.mod h1:r0kV5h3qnFPlQnBSrULhlsRfryS2pmewsg+XfMgkVos=
|
||||||
|
|||||||
@@ -2,27 +2,10 @@
|
|||||||
// management.
|
// management.
|
||||||
package cheatpath
|
package cheatpath
|
||||||
|
|
||||||
import "fmt"
|
// Path encapsulates cheatsheet path information
|
||||||
|
type Path struct {
|
||||||
// Cheatpath encapsulates cheatsheet path information
|
|
||||||
type Cheatpath struct {
|
|
||||||
Name string `yaml:"name"`
|
Name string `yaml:"name"`
|
||||||
Path string `yaml:"path"`
|
Path string `yaml:"path"`
|
||||||
ReadOnly bool `yaml:"readonly"`
|
ReadOnly bool `yaml:"readonly"`
|
||||||
Tags []string `yaml:"tags"`
|
Tags []string `yaml:"tags"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// Validate ensures that the Cheatpath is valid
|
|
||||||
func (c Cheatpath) Validate() error {
|
|
||||||
// Check that name is not empty
|
|
||||||
if c.Name == "" {
|
|
||||||
return fmt.Errorf("cheatpath name cannot be empty")
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check that path is not empty
|
|
||||||
if c.Path == "" {
|
|
||||||
return fmt.Errorf("cheatpath path cannot be empty")
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -8,13 +8,13 @@ import (
|
|||||||
func TestCheatpathValidate(t *testing.T) {
|
func TestCheatpathValidate(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
cheatpath Cheatpath
|
cheatpath Path
|
||||||
wantErr bool
|
wantErr bool
|
||||||
errMsg string
|
errMsg string
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "valid cheatpath",
|
name: "valid cheatpath",
|
||||||
cheatpath: Cheatpath{
|
cheatpath: Path{
|
||||||
Name: "personal",
|
Name: "personal",
|
||||||
Path: "/home/user/.config/cheat/personal",
|
Path: "/home/user/.config/cheat/personal",
|
||||||
ReadOnly: false,
|
ReadOnly: false,
|
||||||
@@ -24,7 +24,7 @@ func TestCheatpathValidate(t *testing.T) {
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "empty name",
|
name: "empty name",
|
||||||
cheatpath: Cheatpath{
|
cheatpath: Path{
|
||||||
Name: "",
|
Name: "",
|
||||||
Path: "/home/user/.config/cheat/personal",
|
Path: "/home/user/.config/cheat/personal",
|
||||||
ReadOnly: false,
|
ReadOnly: false,
|
||||||
@@ -35,7 +35,7 @@ func TestCheatpathValidate(t *testing.T) {
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "empty path",
|
name: "empty path",
|
||||||
cheatpath: Cheatpath{
|
cheatpath: Path{
|
||||||
Name: "personal",
|
Name: "personal",
|
||||||
Path: "",
|
Path: "",
|
||||||
ReadOnly: false,
|
ReadOnly: false,
|
||||||
@@ -46,7 +46,7 @@ func TestCheatpathValidate(t *testing.T) {
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "both empty",
|
name: "both empty",
|
||||||
cheatpath: Cheatpath{
|
cheatpath: Path{
|
||||||
Name: "",
|
Name: "",
|
||||||
Path: "",
|
Path: "",
|
||||||
ReadOnly: true,
|
ReadOnly: true,
|
||||||
@@ -57,7 +57,7 @@ func TestCheatpathValidate(t *testing.T) {
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "minimal valid",
|
name: "minimal valid",
|
||||||
cheatpath: Cheatpath{
|
cheatpath: Path{
|
||||||
Name: "x",
|
Name: "x",
|
||||||
Path: "/",
|
Path: "/",
|
||||||
},
|
},
|
||||||
@@ -65,7 +65,7 @@ func TestCheatpathValidate(t *testing.T) {
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "with readonly and tags",
|
name: "with readonly and tags",
|
||||||
cheatpath: Cheatpath{
|
cheatpath: Path{
|
||||||
Name: "community",
|
Name: "community",
|
||||||
Path: "/usr/share/cheat",
|
Path: "/usr/share/cheat",
|
||||||
ReadOnly: true,
|
ReadOnly: true,
|
||||||
@@ -88,26 +88,3 @@ func TestCheatpathValidate(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestCheatpathStruct(t *testing.T) {
|
|
||||||
// Test that the struct fields work as expected
|
|
||||||
cp := Cheatpath{
|
|
||||||
Name: "test",
|
|
||||||
Path: "/test/path",
|
|
||||||
ReadOnly: true,
|
|
||||||
Tags: []string{"tag1", "tag2"},
|
|
||||||
}
|
|
||||||
|
|
||||||
if cp.Name != "test" {
|
|
||||||
t.Errorf("expected Name to be 'test', got %q", cp.Name)
|
|
||||||
}
|
|
||||||
if cp.Path != "/test/path" {
|
|
||||||
t.Errorf("expected Path to be '/test/path', got %q", cp.Path)
|
|
||||||
}
|
|
||||||
if !cp.ReadOnly {
|
|
||||||
t.Error("expected ReadOnly to be true")
|
|
||||||
}
|
|
||||||
if len(cp.Tags) != 2 || cp.Tags[0] != "tag1" || cp.Tags[1] != "tag2" {
|
|
||||||
t.Errorf("expected Tags to be [tag1 tag2], got %v", cp.Tags)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,64 +0,0 @@
|
|||||||
// Package cheatpath manages collections of cheat sheets organized in filesystem directories.
|
|
||||||
//
|
|
||||||
// A Cheatpath represents a directory containing cheat sheets, with associated
|
|
||||||
// metadata such as tags and read-only status. Multiple cheatpaths can be
|
|
||||||
// configured to organize sheets from different sources (personal, community, work, etc.).
|
|
||||||
//
|
|
||||||
// # Cheatpath Structure
|
|
||||||
//
|
|
||||||
// Each cheatpath has:
|
|
||||||
// - Name: A friendly identifier (e.g., "personal", "community")
|
|
||||||
// - Path: The filesystem path to the directory
|
|
||||||
// - Tags: Tags automatically applied to all sheets in this path
|
|
||||||
// - ReadOnly: Whether sheets in this path can be modified
|
|
||||||
//
|
|
||||||
// Example configuration:
|
|
||||||
//
|
|
||||||
// cheatpaths:
|
|
||||||
// - name: personal
|
|
||||||
// path: ~/cheat
|
|
||||||
// tags: []
|
|
||||||
// readonly: false
|
|
||||||
// - name: community
|
|
||||||
// path: ~/cheat/community
|
|
||||||
// tags: [community]
|
|
||||||
// readonly: true
|
|
||||||
//
|
|
||||||
// # Directory-Scoped Cheatpaths
|
|
||||||
//
|
|
||||||
// The package supports directory-scoped cheatpaths via `.cheat` directories.
|
|
||||||
// When running cheat, the tool walks upward from the current working directory
|
|
||||||
// to the filesystem root, stopping at the first `.cheat` directory found. That
|
|
||||||
// directory is temporarily added to the available cheatpaths.
|
|
||||||
//
|
|
||||||
// # Precedence and Overrides
|
|
||||||
//
|
|
||||||
// When multiple cheatpaths contain a sheet with the same name, the sheet
|
|
||||||
// from the most "local" cheatpath takes precedence. This allows users to
|
|
||||||
// override community sheets with personal versions.
|
|
||||||
//
|
|
||||||
// Key Functions
|
|
||||||
//
|
|
||||||
// - Filter: Filters cheatpaths by name
|
|
||||||
// - Validate: Ensures cheatpath configuration is valid
|
|
||||||
// - Writeable: Returns the first writeable cheatpath
|
|
||||||
//
|
|
||||||
// Example Usage
|
|
||||||
//
|
|
||||||
// // Filter cheatpaths to only "personal"
|
|
||||||
// filtered, err := cheatpath.Filter(paths, "personal")
|
|
||||||
// if err != nil {
|
|
||||||
// log.Fatal(err)
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// // Find a writeable cheatpath
|
|
||||||
// writeable, err := cheatpath.Writeable(paths)
|
|
||||||
// if err != nil {
|
|
||||||
// log.Fatal(err)
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// // Validate cheatpath configuration
|
|
||||||
// if err := cheatpath.Validate(paths); err != nil {
|
|
||||||
// log.Fatal(err)
|
|
||||||
// }
|
|
||||||
package cheatpath
|
|
||||||
@@ -5,15 +5,15 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// Filter filters all cheatpaths that are not named `name`
|
// Filter filters all cheatpaths that are not named `name`
|
||||||
func Filter(paths []Cheatpath, name string) ([]Cheatpath, error) {
|
func Filter(paths []Path, name string) ([]Path, error) {
|
||||||
|
|
||||||
// if a path of the given name exists, return it
|
// if a path of the given name exists, return it
|
||||||
for _, path := range paths {
|
for _, path := range paths {
|
||||||
if path.Name == name {
|
if path.Name == name {
|
||||||
return []Cheatpath{path}, nil
|
return []Path{path}, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// otherwise, return an error
|
// otherwise, return an error
|
||||||
return []Cheatpath{}, fmt.Errorf("cheatpath does not exist: %s", name)
|
return []Path{}, fmt.Errorf("cheatpath does not exist: %s", name)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,10 +9,10 @@ import (
|
|||||||
func TestFilterSuccess(t *testing.T) {
|
func TestFilterSuccess(t *testing.T) {
|
||||||
|
|
||||||
// init cheatpaths
|
// init cheatpaths
|
||||||
paths := []Cheatpath{
|
paths := []Path{
|
||||||
Cheatpath{Name: "foo"},
|
Path{Name: "foo"},
|
||||||
Cheatpath{Name: "bar"},
|
Path{Name: "bar"},
|
||||||
Cheatpath{Name: "baz"},
|
Path{Name: "baz"},
|
||||||
}
|
}
|
||||||
|
|
||||||
// filter the paths
|
// filter the paths
|
||||||
@@ -39,10 +39,10 @@ func TestFilterSuccess(t *testing.T) {
|
|||||||
func TestFilterFailure(t *testing.T) {
|
func TestFilterFailure(t *testing.T) {
|
||||||
|
|
||||||
// init cheatpaths
|
// init cheatpaths
|
||||||
paths := []Cheatpath{
|
paths := []Path{
|
||||||
Cheatpath{Name: "foo"},
|
Path{Name: "foo"},
|
||||||
Cheatpath{Name: "bar"},
|
Path{Name: "bar"},
|
||||||
Cheatpath{Name: "baz"},
|
Path{Name: "baz"},
|
||||||
}
|
}
|
||||||
|
|
||||||
// filter the paths
|
// filter the paths
|
||||||
|
|||||||
@@ -2,39 +2,15 @@ package cheatpath
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"path/filepath"
|
|
||||||
"strings"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// ValidateSheetName ensures that a cheatsheet name does not contain
|
// Validate ensures that the Path is valid
|
||||||
// directory traversal sequences or other potentially dangerous patterns.
|
func (c Path) Validate() error {
|
||||||
func ValidateSheetName(name string) error {
|
if c.Name == "" {
|
||||||
// Reject empty names
|
return fmt.Errorf("cheatpath name cannot be empty")
|
||||||
if name == "" {
|
|
||||||
return fmt.Errorf("cheatsheet name cannot be empty")
|
|
||||||
}
|
}
|
||||||
|
if c.Path == "" {
|
||||||
// Reject names containing directory traversal
|
return fmt.Errorf("cheatpath path cannot be empty")
|
||||||
if strings.Contains(name, "..") {
|
|
||||||
return fmt.Errorf("cheatsheet name cannot contain '..'")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Reject absolute paths
|
|
||||||
if filepath.IsAbs(name) {
|
|
||||||
return fmt.Errorf("cheatsheet name cannot be an absolute path")
|
|
||||||
}
|
|
||||||
|
|
||||||
// Reject names that start with ~ (home directory expansion)
|
|
||||||
if strings.HasPrefix(name, "~") {
|
|
||||||
return fmt.Errorf("cheatsheet name cannot start with '~'")
|
|
||||||
}
|
|
||||||
|
|
||||||
// Reject hidden files (files that start with a dot)
|
|
||||||
// We don't display hidden files, so we shouldn't create them
|
|
||||||
filename := filepath.Base(name)
|
|
||||||
if strings.HasPrefix(filename, ".") {
|
|
||||||
return fmt.Errorf("cheatsheet name cannot start with '.' (hidden files are not supported)")
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,8 +4,8 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Writeable returns a writeable Cheatpath
|
// Writeable returns a writeable Path
|
||||||
func Writeable(cheatpaths []Cheatpath) (Cheatpath, error) {
|
func Writeable(cheatpaths []Path) (Path, error) {
|
||||||
|
|
||||||
// iterate backwards over the cheatpaths
|
// iterate backwards over the cheatpaths
|
||||||
// NB: we're going backwards because we assume that the most "local"
|
// NB: we're going backwards because we assume that the most "local"
|
||||||
@@ -18,5 +18,5 @@ func Writeable(cheatpaths []Cheatpath) (Cheatpath, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// otherwise, return an error
|
// otherwise, return an error
|
||||||
return Cheatpath{}, fmt.Errorf("no writeable cheatpaths found")
|
return Path{}, fmt.Errorf("no writeable cheatpaths found")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -9,10 +9,10 @@ import (
|
|||||||
func TestWriteableOK(t *testing.T) {
|
func TestWriteableOK(t *testing.T) {
|
||||||
|
|
||||||
// initialize some cheatpaths
|
// initialize some cheatpaths
|
||||||
cheatpaths := []Cheatpath{
|
cheatpaths := []Path{
|
||||||
Cheatpath{Path: "/foo", ReadOnly: true},
|
Path{Path: "/foo", ReadOnly: true},
|
||||||
Cheatpath{Path: "/bar", ReadOnly: false},
|
Path{Path: "/bar", ReadOnly: false},
|
||||||
Cheatpath{Path: "/baz", ReadOnly: true},
|
Path{Path: "/baz", ReadOnly: true},
|
||||||
}
|
}
|
||||||
|
|
||||||
// get the writeable cheatpath
|
// get the writeable cheatpath
|
||||||
@@ -34,10 +34,10 @@ func TestWriteableOK(t *testing.T) {
|
|||||||
func TestWriteableNotOK(t *testing.T) {
|
func TestWriteableNotOK(t *testing.T) {
|
||||||
|
|
||||||
// initialize some cheatpaths
|
// initialize some cheatpaths
|
||||||
cheatpaths := []Cheatpath{
|
cheatpaths := []Path{
|
||||||
Cheatpath{Path: "/foo", ReadOnly: true},
|
Path{Path: "/foo", ReadOnly: true},
|
||||||
Cheatpath{Path: "/bar", ReadOnly: true},
|
Path{Path: "/bar", ReadOnly: true},
|
||||||
Cheatpath{Path: "/baz", ReadOnly: true},
|
Path{Path: "/baz", ReadOnly: true},
|
||||||
}
|
}
|
||||||
|
|
||||||
// get the writeable cheatpath
|
// get the writeable cheatpath
|
||||||
|
|||||||
43
internal/completions/cheatsheets.go
Normal file
43
internal/completions/cheatsheets.go
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
// Package completions provides dynamic shell completion functions and
|
||||||
|
// completion script generation for the cheat CLI.
|
||||||
|
package completions
|
||||||
|
|
||||||
|
import (
|
||||||
|
"sort"
|
||||||
|
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
|
||||||
|
"github.com/cheat/cheat/internal/sheets"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Cheatsheets provides completion for cheatsheet names.
|
||||||
|
func Cheatsheets(
|
||||||
|
_ *cobra.Command,
|
||||||
|
args []string,
|
||||||
|
_ string,
|
||||||
|
) ([]string, cobra.ShellCompDirective) {
|
||||||
|
|
||||||
|
if len(args) > 0 {
|
||||||
|
return nil, cobra.ShellCompDirectiveNoFileComp
|
||||||
|
}
|
||||||
|
|
||||||
|
conf, err := loadConfig()
|
||||||
|
if err != nil {
|
||||||
|
return nil, cobra.ShellCompDirectiveNoFileComp
|
||||||
|
}
|
||||||
|
|
||||||
|
cheatsheets, err := sheets.Load(conf.Cheatpaths)
|
||||||
|
if err != nil {
|
||||||
|
return nil, cobra.ShellCompDirectiveNoFileComp
|
||||||
|
}
|
||||||
|
|
||||||
|
consolidated := sheets.Consolidate(cheatsheets)
|
||||||
|
|
||||||
|
names := make([]string, 0, len(consolidated))
|
||||||
|
for name := range consolidated {
|
||||||
|
names = append(names, name)
|
||||||
|
}
|
||||||
|
sort.Strings(names)
|
||||||
|
|
||||||
|
return names, cobra.ShellCompDirectiveNoFileComp
|
||||||
|
}
|
||||||
38
internal/completions/config.go
Normal file
38
internal/completions/config.go
Normal file
@@ -0,0 +1,38 @@
|
|||||||
|
package completions
|
||||||
|
|
||||||
|
import (
|
||||||
|
"runtime"
|
||||||
|
|
||||||
|
"github.com/mitchellh/go-homedir"
|
||||||
|
|
||||||
|
"github.com/cheat/cheat/internal/config"
|
||||||
|
)
|
||||||
|
|
||||||
|
// loadConfig loads the cheat configuration for use in completion functions.
|
||||||
|
// It returns an error rather than exiting, since completions should degrade
|
||||||
|
// gracefully.
|
||||||
|
func loadConfig() (config.Config, error) {
|
||||||
|
home, err := homedir.Dir()
|
||||||
|
if err != nil {
|
||||||
|
return config.Config{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
envvars := config.EnvVars()
|
||||||
|
|
||||||
|
confpaths, err := config.Paths(runtime.GOOS, home, envvars)
|
||||||
|
if err != nil {
|
||||||
|
return config.Config{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
confpath, err := config.Path(confpaths)
|
||||||
|
if err != nil {
|
||||||
|
return config.Config{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
conf, err := config.New(confpath, true)
|
||||||
|
if err != nil {
|
||||||
|
return config.Config{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return conf, nil
|
||||||
|
}
|
||||||
24
internal/completions/generate.go
Normal file
24
internal/completions/generate.go
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
package completions
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Generate writes a shell completion script to the given writer.
|
||||||
|
func Generate(cmd *cobra.Command, shell string, w io.Writer) error {
|
||||||
|
switch shell {
|
||||||
|
case "bash":
|
||||||
|
return cmd.Root().GenBashCompletionV2(w, true)
|
||||||
|
case "zsh":
|
||||||
|
return cmd.Root().GenZshCompletion(w)
|
||||||
|
case "fish":
|
||||||
|
return cmd.Root().GenFishCompletion(w, true)
|
||||||
|
case "powershell":
|
||||||
|
return cmd.Root().GenPowerShellCompletionWithDesc(w)
|
||||||
|
default:
|
||||||
|
return fmt.Errorf("unsupported shell: %s (valid: bash, zsh, fish, powershell)", shell)
|
||||||
|
}
|
||||||
|
}
|
||||||
25
internal/completions/paths.go
Normal file
25
internal/completions/paths.go
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
package completions
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Paths provides completion for the --path flag.
|
||||||
|
func Paths(
|
||||||
|
_ *cobra.Command,
|
||||||
|
_ []string,
|
||||||
|
_ string,
|
||||||
|
) ([]string, cobra.ShellCompDirective) {
|
||||||
|
|
||||||
|
conf, err := loadConfig()
|
||||||
|
if err != nil {
|
||||||
|
return nil, cobra.ShellCompDirectiveNoFileComp
|
||||||
|
}
|
||||||
|
|
||||||
|
names := make([]string, 0, len(conf.Cheatpaths))
|
||||||
|
for _, cp := range conf.Cheatpaths {
|
||||||
|
names = append(names, cp.Name)
|
||||||
|
}
|
||||||
|
|
||||||
|
return names, cobra.ShellCompDirectiveNoFileComp
|
||||||
|
}
|
||||||
27
internal/completions/tags.go
Normal file
27
internal/completions/tags.go
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
package completions
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
|
||||||
|
"github.com/cheat/cheat/internal/sheets"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Tags provides completion for the --tag flag.
|
||||||
|
func Tags(
|
||||||
|
_ *cobra.Command,
|
||||||
|
_ []string,
|
||||||
|
_ string,
|
||||||
|
) ([]string, cobra.ShellCompDirective) {
|
||||||
|
|
||||||
|
conf, err := loadConfig()
|
||||||
|
if err != nil {
|
||||||
|
return nil, cobra.ShellCompDirectiveNoFileComp
|
||||||
|
}
|
||||||
|
|
||||||
|
cheatsheets, err := sheets.Load(conf.Cheatpaths)
|
||||||
|
if err != nil {
|
||||||
|
return nil, cobra.ShellCompDirectiveNoFileComp
|
||||||
|
}
|
||||||
|
|
||||||
|
return sheets.Tags(cheatsheets), cobra.ShellCompDirectiveNoFileComp
|
||||||
|
}
|
||||||
@@ -7,7 +7,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// Color indicates whether colorization should be applied to the output
|
// Color indicates whether colorization should be applied to the output
|
||||||
func (c *Config) Color(opts map[string]interface{}) bool {
|
func (c *Config) Color(forceColorize bool) bool {
|
||||||
|
|
||||||
// default to the colorization specified in the configs...
|
// default to the colorization specified in the configs...
|
||||||
colorize := c.Colorize
|
colorize := c.Colorize
|
||||||
@@ -18,7 +18,7 @@ func (c *Config) Color(opts map[string]interface{}) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ... *unless* the --colorize flag was passed
|
// ... *unless* the --colorize flag was passed
|
||||||
if opts["--colorize"] == true {
|
if forceColorize {
|
||||||
colorize = true
|
colorize = true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -10,13 +10,11 @@ func TestColor(t *testing.T) {
|
|||||||
// mock a config
|
// mock a config
|
||||||
conf := Config{}
|
conf := Config{}
|
||||||
|
|
||||||
opts := map[string]interface{}{"--colorize": false}
|
if conf.Color(false) {
|
||||||
if conf.Color(opts) {
|
t.Errorf("failed to respect forceColorize (false)")
|
||||||
t.Errorf("failed to respect --colorize (false)")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
opts = map[string]interface{}{"--colorize": true}
|
if !conf.Color(true) {
|
||||||
if !conf.Color(opts) {
|
t.Errorf("failed to respect forceColorize (true)")
|
||||||
t.Errorf("failed to respect --colorize (true)")
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,158 +2,16 @@
|
|||||||
package config
|
package config
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"os"
|
|
||||||
"path/filepath"
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
cp "github.com/cheat/cheat/internal/cheatpath"
|
cp "github.com/cheat/cheat/internal/cheatpath"
|
||||||
|
|
||||||
"github.com/mitchellh/go-homedir"
|
|
||||||
"gopkg.in/yaml.v3"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Config encapsulates configuration parameters
|
// Config encapsulates configuration parameters
|
||||||
type Config struct {
|
type Config struct {
|
||||||
Colorize bool `yaml:"colorize"`
|
Colorize bool `yaml:"colorize"`
|
||||||
Editor string `yaml:"editor"`
|
Editor string `yaml:"editor"`
|
||||||
Cheatpaths []cp.Cheatpath `yaml:"cheatpaths"`
|
Cheatpaths []cp.Path `yaml:"cheatpaths"`
|
||||||
Style string `yaml:"style"`
|
Style string `yaml:"style"`
|
||||||
Formatter string `yaml:"formatter"`
|
Formatter string `yaml:"formatter"`
|
||||||
Pager string `yaml:"pager"`
|
Pager string `yaml:"pager"`
|
||||||
Path string
|
Path string
|
||||||
}
|
}
|
||||||
|
|
||||||
// New returns a new Config struct
|
|
||||||
func New(_ map[string]interface{}, confPath string, resolve bool) (Config, error) {
|
|
||||||
|
|
||||||
// read the config file
|
|
||||||
buf, err := os.ReadFile(confPath)
|
|
||||||
if err != nil {
|
|
||||||
return Config{}, fmt.Errorf("could not read config file: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// initialize a config object
|
|
||||||
conf := Config{}
|
|
||||||
|
|
||||||
// store the config path
|
|
||||||
conf.Path = confPath
|
|
||||||
|
|
||||||
// unmarshal the yaml
|
|
||||||
err = yaml.Unmarshal(buf, &conf)
|
|
||||||
if err != nil {
|
|
||||||
return Config{}, fmt.Errorf("could not unmarshal yaml: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// if a .cheat directory exists in the current directory or any ancestor,
|
|
||||||
// append it to the cheatpaths
|
|
||||||
cwd, err := os.Getwd()
|
|
||||||
if err != nil {
|
|
||||||
return Config{}, fmt.Errorf("failed to get cwd: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if local := findLocalCheatpath(cwd); local != "" {
|
|
||||||
path := cp.Cheatpath{
|
|
||||||
Name: "cwd",
|
|
||||||
Path: local,
|
|
||||||
ReadOnly: false,
|
|
||||||
Tags: []string{},
|
|
||||||
}
|
|
||||||
conf.Cheatpaths = append(conf.Cheatpaths, path)
|
|
||||||
}
|
|
||||||
|
|
||||||
// process cheatpaths
|
|
||||||
var validPaths []cp.Cheatpath
|
|
||||||
for _, cheatpath := range conf.Cheatpaths {
|
|
||||||
|
|
||||||
// expand ~ in config paths
|
|
||||||
expanded, err := homedir.Expand(cheatpath.Path)
|
|
||||||
if err != nil {
|
|
||||||
return Config{}, fmt.Errorf("failed to expand ~: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// follow symlinks
|
|
||||||
//
|
|
||||||
// NB: `resolve` is an ugly kludge that exists for the sake of unit-tests.
|
|
||||||
// It's necessary because `EvalSymlinks` will error if the symlink points
|
|
||||||
// to a non-existent location on the filesystem. When unit-testing,
|
|
||||||
// however, we don't want to have dependencies on the filesystem. As such,
|
|
||||||
// `resolve` is a switch that allows us to turn off symlink resolution when
|
|
||||||
// running the config tests.
|
|
||||||
if resolve {
|
|
||||||
evaled, err := filepath.EvalSymlinks(expanded)
|
|
||||||
if err != nil {
|
|
||||||
// if the path simply doesn't exist, warn and skip it
|
|
||||||
if os.IsNotExist(err) {
|
|
||||||
fmt.Fprintf(os.Stderr,
|
|
||||||
"WARNING: cheatpath '%s' does not exist, skipping\n",
|
|
||||||
expanded,
|
|
||||||
)
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
return Config{}, fmt.Errorf(
|
|
||||||
"failed to resolve symlink: %s: %v",
|
|
||||||
expanded,
|
|
||||||
err,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
expanded = evaled
|
|
||||||
}
|
|
||||||
|
|
||||||
cheatpath.Path = expanded
|
|
||||||
validPaths = append(validPaths, cheatpath)
|
|
||||||
}
|
|
||||||
conf.Cheatpaths = validPaths
|
|
||||||
|
|
||||||
// determine the editor: env vars override the config file value,
|
|
||||||
// following standard Unix convention (see #589)
|
|
||||||
if v := os.Getenv("VISUAL"); v != "" {
|
|
||||||
conf.Editor = v
|
|
||||||
} else if v := os.Getenv("EDITOR"); v != "" {
|
|
||||||
conf.Editor = v
|
|
||||||
} else {
|
|
||||||
conf.Editor = strings.TrimSpace(conf.Editor)
|
|
||||||
}
|
|
||||||
|
|
||||||
// if an editor was still not determined, attempt to choose one
|
|
||||||
// that's appropriate for the environment
|
|
||||||
if conf.Editor == "" {
|
|
||||||
if conf.Editor, err = Editor(); err != nil {
|
|
||||||
return Config{}, err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// if a chroma style was not provided, set a default
|
|
||||||
if conf.Style == "" {
|
|
||||||
conf.Style = "bw"
|
|
||||||
}
|
|
||||||
|
|
||||||
// if a chroma formatter was not provided, set a default
|
|
||||||
if conf.Formatter == "" {
|
|
||||||
conf.Formatter = "terminal"
|
|
||||||
}
|
|
||||||
|
|
||||||
// load the pager
|
|
||||||
conf.Pager = strings.TrimSpace(conf.Pager)
|
|
||||||
|
|
||||||
return conf, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// findLocalCheatpath walks upward from dir looking for a .cheat directory.
|
|
||||||
// It returns the path to the first .cheat directory found, or an empty string
|
|
||||||
// if none exists. This mirrors the discovery pattern used by git for .git
|
|
||||||
// directories.
|
|
||||||
func findLocalCheatpath(dir string) string {
|
|
||||||
for {
|
|
||||||
candidate := filepath.Join(dir, ".cheat")
|
|
||||||
if info, err := os.Stat(candidate); err == nil && info.IsDir() {
|
|
||||||
return candidate
|
|
||||||
}
|
|
||||||
parent := filepath.Dir(dir)
|
|
||||||
if parent == dir {
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
dir = parent
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -3,10 +3,9 @@ package config
|
|||||||
import (
|
import (
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"runtime"
|
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/cheat/cheat/internal/mock"
|
"github.com/cheat/cheat/mocks"
|
||||||
)
|
)
|
||||||
|
|
||||||
// TestConfigYAMLErrors tests YAML parsing errors
|
// TestConfigYAMLErrors tests YAML parsing errors
|
||||||
@@ -19,258 +18,22 @@ func TestConfigYAMLErrors(t *testing.T) {
|
|||||||
defer os.RemoveAll(tempDir)
|
defer os.RemoveAll(tempDir)
|
||||||
|
|
||||||
invalidYAML := filepath.Join(tempDir, "invalid.yml")
|
invalidYAML := filepath.Join(tempDir, "invalid.yml")
|
||||||
err = os.WriteFile(invalidYAML, []byte("invalid: yaml: content:\n - no closing"), 0644)
|
err = os.WriteFile(invalidYAML, []byte("cheatpaths: [{unclosed\n"), 0644)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("failed to write invalid yaml: %v", err)
|
t.Fatalf("failed to write invalid yaml: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Attempt to load invalid YAML
|
// Attempt to load invalid YAML
|
||||||
_, err = New(map[string]interface{}{}, invalidYAML, false)
|
_, err = New(invalidYAML, false)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Error("expected error for invalid YAML, got nil")
|
t.Error("expected error for invalid YAML, got nil")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TestConfigLocalCheatpath tests local .cheat directory detection
|
|
||||||
func TestConfigLocalCheatpath(t *testing.T) {
|
|
||||||
// Create a temporary directory to act as working directory
|
|
||||||
tempDir, err := os.MkdirTemp("", "cheat-config-test-*")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("failed to create temp dir: %v", err)
|
|
||||||
}
|
|
||||||
defer os.RemoveAll(tempDir)
|
|
||||||
|
|
||||||
// Resolve symlinks in temp dir path (macOS /var -> /private/var)
|
|
||||||
tempDir, err = filepath.EvalSymlinks(tempDir)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("failed to resolve temp dir symlinks: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Save current working directory
|
|
||||||
oldCwd, err := os.Getwd()
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("failed to get cwd: %v", err)
|
|
||||||
}
|
|
||||||
defer os.Chdir(oldCwd)
|
|
||||||
|
|
||||||
// Change to temp directory
|
|
||||||
err = os.Chdir(tempDir)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("failed to change dir: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create .cheat directory
|
|
||||||
localCheat := filepath.Join(tempDir, ".cheat")
|
|
||||||
err = os.Mkdir(localCheat, 0755)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("failed to create .cheat dir: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Load config
|
|
||||||
conf, err := New(map[string]interface{}{}, mock.Path("conf/empty.yml"), false)
|
|
||||||
if err != nil {
|
|
||||||
t.Errorf("failed to load config: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check that local cheatpath was added
|
|
||||||
found := false
|
|
||||||
for _, cp := range conf.Cheatpaths {
|
|
||||||
if cp.Name == "cwd" && cp.Path == localCheat {
|
|
||||||
found = true
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if !found {
|
|
||||||
t.Error("local .cheat directory was not added to cheatpaths")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// TestConfigLocalCheatpathInParent tests that .cheat in a parent directory is found
|
|
||||||
func TestConfigLocalCheatpathInParent(t *testing.T) {
|
|
||||||
tempDir, err := os.MkdirTemp("", "cheat-config-test-*")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("failed to create temp dir: %v", err)
|
|
||||||
}
|
|
||||||
defer os.RemoveAll(tempDir)
|
|
||||||
|
|
||||||
// Resolve symlinks in temp dir path (macOS /var -> /private/var)
|
|
||||||
tempDir, err = filepath.EvalSymlinks(tempDir)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("failed to resolve temp dir symlinks: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
oldCwd, err := os.Getwd()
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("failed to get cwd: %v", err)
|
|
||||||
}
|
|
||||||
defer os.Chdir(oldCwd)
|
|
||||||
|
|
||||||
// Create .cheat in the root of the temp dir
|
|
||||||
localCheat := filepath.Join(tempDir, ".cheat")
|
|
||||||
if err := os.Mkdir(localCheat, 0755); err != nil {
|
|
||||||
t.Fatalf("failed to create .cheat dir: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create a subdirectory and cd into it
|
|
||||||
subDir := filepath.Join(tempDir, "sub")
|
|
||||||
if err := os.Mkdir(subDir, 0755); err != nil {
|
|
||||||
t.Fatalf("failed to create sub dir: %v", err)
|
|
||||||
}
|
|
||||||
if err := os.Chdir(subDir); err != nil {
|
|
||||||
t.Fatalf("failed to chdir: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
conf, err := New(map[string]interface{}{}, mock.Path("conf/empty.yml"), false)
|
|
||||||
if err != nil {
|
|
||||||
t.Errorf("failed to load config: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
found := false
|
|
||||||
for _, cp := range conf.Cheatpaths {
|
|
||||||
if cp.Name == "cwd" && cp.Path == localCheat {
|
|
||||||
found = true
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if !found {
|
|
||||||
t.Error("parent .cheat directory was not added to cheatpaths")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// TestConfigLocalCheatpathNearestWins tests that the nearest .cheat wins
|
|
||||||
func TestConfigLocalCheatpathNearestWins(t *testing.T) {
|
|
||||||
tempDir, err := os.MkdirTemp("", "cheat-config-test-*")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("failed to create temp dir: %v", err)
|
|
||||||
}
|
|
||||||
defer os.RemoveAll(tempDir)
|
|
||||||
|
|
||||||
// Resolve symlinks in temp dir path (macOS /var -> /private/var)
|
|
||||||
tempDir, err = filepath.EvalSymlinks(tempDir)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("failed to resolve temp dir symlinks: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
oldCwd, err := os.Getwd()
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("failed to get cwd: %v", err)
|
|
||||||
}
|
|
||||||
defer os.Chdir(oldCwd)
|
|
||||||
|
|
||||||
// Create .cheat at root
|
|
||||||
if err := os.Mkdir(filepath.Join(tempDir, ".cheat"), 0755); err != nil {
|
|
||||||
t.Fatalf("failed to create root .cheat dir: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create sub/.cheat (the nearer one)
|
|
||||||
subDir := filepath.Join(tempDir, "sub")
|
|
||||||
if err := os.Mkdir(subDir, 0755); err != nil {
|
|
||||||
t.Fatalf("failed to create sub dir: %v", err)
|
|
||||||
}
|
|
||||||
nearCheat := filepath.Join(subDir, ".cheat")
|
|
||||||
if err := os.Mkdir(nearCheat, 0755); err != nil {
|
|
||||||
t.Fatalf("failed to create near .cheat dir: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// cd into sub/deep/
|
|
||||||
deepDir := filepath.Join(subDir, "deep")
|
|
||||||
if err := os.Mkdir(deepDir, 0755); err != nil {
|
|
||||||
t.Fatalf("failed to create deep dir: %v", err)
|
|
||||||
}
|
|
||||||
if err := os.Chdir(deepDir); err != nil {
|
|
||||||
t.Fatalf("failed to chdir: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
conf, err := New(map[string]interface{}{}, mock.Path("conf/empty.yml"), false)
|
|
||||||
if err != nil {
|
|
||||||
t.Errorf("failed to load config: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
found := false
|
|
||||||
for _, cp := range conf.Cheatpaths {
|
|
||||||
if cp.Name == "cwd" {
|
|
||||||
if cp.Path != nearCheat {
|
|
||||||
t.Errorf("expected nearest .cheat %s, got %s", nearCheat, cp.Path)
|
|
||||||
}
|
|
||||||
found = true
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if !found {
|
|
||||||
t.Error("no cwd cheatpath found")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// TestConfigNoLocalCheatpath tests that no cwd cheatpath is added when no .cheat exists
|
|
||||||
func TestConfigNoLocalCheatpath(t *testing.T) {
|
|
||||||
tempDir, err := os.MkdirTemp("", "cheat-config-test-*")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("failed to create temp dir: %v", err)
|
|
||||||
}
|
|
||||||
defer os.RemoveAll(tempDir)
|
|
||||||
|
|
||||||
oldCwd, err := os.Getwd()
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("failed to get cwd: %v", err)
|
|
||||||
}
|
|
||||||
defer os.Chdir(oldCwd)
|
|
||||||
|
|
||||||
if err := os.Chdir(tempDir); err != nil {
|
|
||||||
t.Fatalf("failed to chdir: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
conf, err := New(map[string]interface{}{}, mock.Path("conf/empty.yml"), false)
|
|
||||||
if err != nil {
|
|
||||||
t.Errorf("failed to load config: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, cp := range conf.Cheatpaths {
|
|
||||||
if cp.Name == "cwd" {
|
|
||||||
t.Error("cwd cheatpath should not be added when no .cheat exists")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// TestConfigLocalCheatpathFileSkipped tests that a .cheat file (not dir) is skipped
|
|
||||||
func TestConfigLocalCheatpathFileSkipped(t *testing.T) {
|
|
||||||
tempDir, err := os.MkdirTemp("", "cheat-config-test-*")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("failed to create temp dir: %v", err)
|
|
||||||
}
|
|
||||||
defer os.RemoveAll(tempDir)
|
|
||||||
|
|
||||||
oldCwd, err := os.Getwd()
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("failed to get cwd: %v", err)
|
|
||||||
}
|
|
||||||
defer os.Chdir(oldCwd)
|
|
||||||
|
|
||||||
// Create .cheat as a file, not a directory
|
|
||||||
if err := os.WriteFile(filepath.Join(tempDir, ".cheat"), []byte("not a dir"), 0644); err != nil {
|
|
||||||
t.Fatalf("failed to create .cheat file: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := os.Chdir(tempDir); err != nil {
|
|
||||||
t.Fatalf("failed to chdir: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
conf, err := New(map[string]interface{}{}, mock.Path("conf/empty.yml"), false)
|
|
||||||
if err != nil {
|
|
||||||
t.Errorf("failed to load config: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, cp := range conf.Cheatpaths {
|
|
||||||
if cp.Name == "cwd" {
|
|
||||||
t.Error("cwd cheatpath should not be added for a .cheat file")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// TestConfigDefaults tests default values
|
// TestConfigDefaults tests default values
|
||||||
func TestConfigDefaults(t *testing.T) {
|
func TestConfigDefaults(t *testing.T) {
|
||||||
// Load empty config
|
// Load empty config
|
||||||
conf, err := New(map[string]interface{}{}, mock.Path("conf/empty.yml"), false)
|
conf, err := New(mocks.Path("conf/empty.yml"), false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("failed to load config: %v", err)
|
t.Errorf("failed to load config: %v", err)
|
||||||
}
|
}
|
||||||
@@ -329,13 +92,16 @@ cheatpaths:
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Load config with symlink resolution
|
// Load config with symlink resolution
|
||||||
conf, err := New(map[string]interface{}{}, configFile, true)
|
conf, err := New(configFile, true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("failed to load config: %v", err)
|
t.Errorf("failed to load config: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Verify symlink was resolved
|
// Verify symlink was resolved
|
||||||
if len(conf.Cheatpaths) > 0 && conf.Cheatpaths[0].Path != targetDir {
|
if len(conf.Cheatpaths) == 0 {
|
||||||
|
t.Fatal("expected at least one cheatpath, got none")
|
||||||
|
}
|
||||||
|
if conf.Cheatpaths[0].Path != targetDir {
|
||||||
t.Errorf("expected symlink to be resolved to %s, got %s", targetDir, conf.Cheatpaths[0].Path)
|
t.Errorf("expected symlink to be resolved to %s, got %s", targetDir, conf.Cheatpaths[0].Path)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -372,7 +138,7 @@ cheatpaths:
|
|||||||
|
|
||||||
// Load config with symlink resolution should skip the broken cheatpath
|
// Load config with symlink resolution should skip the broken cheatpath
|
||||||
// (warn to stderr) rather than hard-error
|
// (warn to stderr) rather than hard-error
|
||||||
conf, err := New(map[string]interface{}{}, configFile, true)
|
conf, err := New(configFile, true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("expected no error for broken symlink (should skip), got: %v", err)
|
t.Errorf("expected no error for broken symlink (should skip), got: %v", err)
|
||||||
}
|
}
|
||||||
@@ -380,70 +146,3 @@ cheatpaths:
|
|||||||
t.Errorf("expected broken cheatpath to be filtered out, got %d cheatpaths", len(conf.Cheatpaths))
|
t.Errorf("expected broken cheatpath to be filtered out, got %d cheatpaths", len(conf.Cheatpaths))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TestConfigTildeExpansionError tests tilde expansion error handling
|
|
||||||
func TestConfigTildeExpansionError(t *testing.T) {
|
|
||||||
// This is tricky to test without mocking homedir.Expand
|
|
||||||
// We'll create a config with an invalid home reference
|
|
||||||
tempDir, err := os.MkdirTemp("", "cheat-config-test-*")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("failed to create temp dir: %v", err)
|
|
||||||
}
|
|
||||||
defer os.RemoveAll(tempDir)
|
|
||||||
|
|
||||||
// Create config with user that likely doesn't exist
|
|
||||||
configContent := `---
|
|
||||||
editor: vim
|
|
||||||
cheatpaths:
|
|
||||||
- name: test
|
|
||||||
path: ~nonexistentuser12345/cheat
|
|
||||||
readonly: true
|
|
||||||
`
|
|
||||||
configFile := filepath.Join(tempDir, "config.yml")
|
|
||||||
err = os.WriteFile(configFile, []byte(configContent), 0644)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("failed to write config: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Load config - this may or may not fail depending on the system
|
|
||||||
// but we're testing that it doesn't panic
|
|
||||||
_, _ = New(map[string]interface{}{}, configFile, false)
|
|
||||||
}
|
|
||||||
|
|
||||||
// TestConfigGetCwdError tests error handling when os.Getwd fails
|
|
||||||
func TestConfigGetCwdError(t *testing.T) {
|
|
||||||
if runtime.GOOS == "windows" {
|
|
||||||
t.Skip("Windows does not allow removing the current directory")
|
|
||||||
}
|
|
||||||
|
|
||||||
// This is difficult to test without being able to break os.Getwd
|
|
||||||
// We'll create a scenario where the current directory is removed
|
|
||||||
|
|
||||||
// Create and enter a temp directory
|
|
||||||
tempDir, err := os.MkdirTemp("", "cheat-config-test-*")
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("failed to create temp dir: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
oldCwd, err := os.Getwd()
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("failed to get cwd: %v", err)
|
|
||||||
}
|
|
||||||
defer os.Chdir(oldCwd)
|
|
||||||
|
|
||||||
err = os.Chdir(tempDir)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("failed to change dir: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Remove the directory we're in
|
|
||||||
err = os.RemoveAll(tempDir)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("failed to remove temp dir: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Now os.Getwd should fail
|
|
||||||
_, err = New(map[string]interface{}{}, mock.Path("conf/empty.yml"), false)
|
|
||||||
// This might not fail on all systems, so we just ensure no panic
|
|
||||||
_ = err
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -65,58 +65,3 @@ func FuzzFindLocalCheatpath(f *testing.F) {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// FuzzFindLocalCheatpathNearestWins verifies that when two .cheat directories
|
|
||||||
// exist at different levels of the ancestor chain, the nearest one is returned.
|
|
||||||
func FuzzFindLocalCheatpathNearestWins(f *testing.F) {
|
|
||||||
f.Add(uint8(5), uint8(1), uint8(3))
|
|
||||||
f.Add(uint8(8), uint8(0), uint8(7))
|
|
||||||
f.Add(uint8(3), uint8(0), uint8(2))
|
|
||||||
f.Add(uint8(10), uint8(2), uint8(8))
|
|
||||||
|
|
||||||
f.Fuzz(func(t *testing.T, totalDepth, shallowRaw, deepRaw uint8) {
|
|
||||||
depth := int(totalDepth%12) + 2 // 2..13 (need room for two placements)
|
|
||||||
s := int(shallowRaw) % depth
|
|
||||||
d := int(deepRaw) % depth
|
|
||||||
|
|
||||||
// Need two distinct levels
|
|
||||||
if s == d {
|
|
||||||
d = (d + 1) % depth
|
|
||||||
}
|
|
||||||
// Ensure s < d (shallow is higher in tree, deep is closer to search dir)
|
|
||||||
if s > d {
|
|
||||||
s, d = d, s
|
|
||||||
}
|
|
||||||
|
|
||||||
tempDir := t.TempDir()
|
|
||||||
|
|
||||||
// Build chain
|
|
||||||
dirs := make([]string, 0, depth+1)
|
|
||||||
dirs = append(dirs, tempDir)
|
|
||||||
current := tempDir
|
|
||||||
for i := 0; i < depth; i++ {
|
|
||||||
current = filepath.Join(current, fmt.Sprintf("d%d", i))
|
|
||||||
if err := os.Mkdir(current, 0755); err != nil {
|
|
||||||
t.Fatalf("mkdir: %v", err)
|
|
||||||
}
|
|
||||||
dirs = append(dirs, current)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Place .cheat at both levels
|
|
||||||
shallowCheat := filepath.Join(dirs[s], ".cheat")
|
|
||||||
deepCheat := filepath.Join(dirs[d], ".cheat")
|
|
||||||
if err := os.Mkdir(shallowCheat, 0755); err != nil {
|
|
||||||
t.Fatalf("mkdir shallow .cheat: %v", err)
|
|
||||||
}
|
|
||||||
if err := os.Mkdir(deepCheat, 0755); err != nil {
|
|
||||||
t.Fatalf("mkdir deep .cheat: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Search from the deepest directory — should find the deeper (nearer) .cheat
|
|
||||||
result := findLocalCheatpath(current)
|
|
||||||
if result != deepCheat {
|
|
||||||
t.Errorf("depth=%d shallow=%d deep=%d: expected nearest %s, got %s",
|
|
||||||
depth, s, d, deepCheat, result)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ import (
|
|||||||
"github.com/mitchellh/go-homedir"
|
"github.com/mitchellh/go-homedir"
|
||||||
|
|
||||||
"github.com/cheat/cheat/internal/cheatpath"
|
"github.com/cheat/cheat/internal/cheatpath"
|
||||||
"github.com/cheat/cheat/internal/mock"
|
"github.com/cheat/cheat/mocks"
|
||||||
)
|
)
|
||||||
|
|
||||||
// TestFindLocalCheatpathInCurrentDir tests that .cheat in the given dir is found
|
// TestFindLocalCheatpathInCurrentDir tests that .cheat in the given dir is found
|
||||||
@@ -286,7 +286,7 @@ func TestConfigSuccessful(t *testing.T) {
|
|||||||
}()
|
}()
|
||||||
|
|
||||||
// initialize a config
|
// initialize a config
|
||||||
conf, err := New(map[string]interface{}{}, mock.Path("conf/conf.yml"), false)
|
conf, err := New(mocks.Path("conf/conf.yml"), false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("failed to parse config file: %v", err)
|
t.Errorf("failed to parse config file: %v", err)
|
||||||
}
|
}
|
||||||
@@ -306,18 +306,18 @@ func TestConfigSuccessful(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// assert that the cheatpaths are correct
|
// assert that the cheatpaths are correct
|
||||||
want := []cheatpath.Cheatpath{
|
want := []cheatpath.Path{
|
||||||
cheatpath.Cheatpath{
|
cheatpath.Path{
|
||||||
Path: filepath.Join(home, ".dotfiles", "cheat", "community"),
|
Path: filepath.Join(home, ".dotfiles", "cheat", "community"),
|
||||||
ReadOnly: true,
|
ReadOnly: true,
|
||||||
Tags: []string{"community"},
|
Tags: []string{"community"},
|
||||||
},
|
},
|
||||||
cheatpath.Cheatpath{
|
cheatpath.Path{
|
||||||
Path: filepath.Join(home, ".dotfiles", "cheat", "work"),
|
Path: filepath.Join(home, ".dotfiles", "cheat", "work"),
|
||||||
ReadOnly: false,
|
ReadOnly: false,
|
||||||
Tags: []string{"work"},
|
Tags: []string{"work"},
|
||||||
},
|
},
|
||||||
cheatpath.Cheatpath{
|
cheatpath.Path{
|
||||||
Path: filepath.Join(home, ".dotfiles", "cheat", "personal"),
|
Path: filepath.Join(home, ".dotfiles", "cheat", "personal"),
|
||||||
ReadOnly: false,
|
ReadOnly: false,
|
||||||
Tags: []string{"personal"},
|
Tags: []string{"personal"},
|
||||||
@@ -338,7 +338,7 @@ func TestConfigSuccessful(t *testing.T) {
|
|||||||
func TestConfigFailure(t *testing.T) {
|
func TestConfigFailure(t *testing.T) {
|
||||||
|
|
||||||
// attempt to read a non-existent config file
|
// attempt to read a non-existent config file
|
||||||
_, err := New(map[string]interface{}{}, "/does-not-exit", false)
|
_, err := New("/does-not-exit", false)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Errorf("failed to error on unreadable config")
|
t.Errorf("failed to error on unreadable config")
|
||||||
}
|
}
|
||||||
@@ -358,7 +358,7 @@ func TestEditorEnvOverride(t *testing.T) {
|
|||||||
// with no env vars, the config file value should be used
|
// with no env vars, the config file value should be used
|
||||||
os.Unsetenv("VISUAL")
|
os.Unsetenv("VISUAL")
|
||||||
os.Unsetenv("EDITOR")
|
os.Unsetenv("EDITOR")
|
||||||
conf, err := New(map[string]interface{}{}, mock.Path("conf/conf.yml"), false)
|
conf, err := New(mocks.Path("conf/conf.yml"), false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("failed to init configs: %v", err)
|
t.Fatalf("failed to init configs: %v", err)
|
||||||
}
|
}
|
||||||
@@ -368,7 +368,7 @@ func TestEditorEnvOverride(t *testing.T) {
|
|||||||
|
|
||||||
// $EDITOR should override the config file value
|
// $EDITOR should override the config file value
|
||||||
os.Setenv("EDITOR", "nano")
|
os.Setenv("EDITOR", "nano")
|
||||||
conf, err = New(map[string]interface{}{}, mock.Path("conf/conf.yml"), false)
|
conf, err = New(mocks.Path("conf/conf.yml"), false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("failed to init configs: %v", err)
|
t.Fatalf("failed to init configs: %v", err)
|
||||||
}
|
}
|
||||||
@@ -378,7 +378,7 @@ func TestEditorEnvOverride(t *testing.T) {
|
|||||||
|
|
||||||
// $VISUAL should override both $EDITOR and the config file value
|
// $VISUAL should override both $EDITOR and the config file value
|
||||||
os.Setenv("VISUAL", "emacs")
|
os.Setenv("VISUAL", "emacs")
|
||||||
conf, err = New(map[string]interface{}{}, mock.Path("conf/conf.yml"), false)
|
conf, err = New(mocks.Path("conf/conf.yml"), false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("failed to init configs: %v", err)
|
t.Fatalf("failed to init configs: %v", err)
|
||||||
}
|
}
|
||||||
@@ -401,7 +401,7 @@ func TestEditorEnvFallback(t *testing.T) {
|
|||||||
// set $EDITOR and assert it's used when config has no editor
|
// set $EDITOR and assert it's used when config has no editor
|
||||||
os.Unsetenv("VISUAL")
|
os.Unsetenv("VISUAL")
|
||||||
os.Setenv("EDITOR", "foo")
|
os.Setenv("EDITOR", "foo")
|
||||||
conf, err := New(map[string]interface{}{}, mock.Path("conf/empty.yml"), false)
|
conf, err := New(mocks.Path("conf/empty.yml"), false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("failed to init configs: %v", err)
|
t.Fatalf("failed to init configs: %v", err)
|
||||||
}
|
}
|
||||||
@@ -411,7 +411,7 @@ func TestEditorEnvFallback(t *testing.T) {
|
|||||||
|
|
||||||
// set $VISUAL and assert it takes precedence over $EDITOR
|
// set $VISUAL and assert it takes precedence over $EDITOR
|
||||||
os.Setenv("VISUAL", "bar")
|
os.Setenv("VISUAL", "bar")
|
||||||
conf, err = New(map[string]interface{}{}, mock.Path("conf/empty.yml"), false)
|
conf, err = New(mocks.Path("conf/empty.yml"), false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("failed to init configs: %v", err)
|
t.Fatalf("failed to init configs: %v", err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,52 +0,0 @@
|
|||||||
// Package config manages application configuration and settings.
|
|
||||||
//
|
|
||||||
// The config package provides functionality to:
|
|
||||||
// - Load configuration from YAML files
|
|
||||||
// - Validate configuration values
|
|
||||||
// - Manage platform-specific configuration paths
|
|
||||||
// - Handle editor and pager settings
|
|
||||||
// - Configure colorization and formatting options
|
|
||||||
//
|
|
||||||
// # Configuration Structure
|
|
||||||
//
|
|
||||||
// The main configuration file (conf.yml) contains:
|
|
||||||
// - Editor preferences
|
|
||||||
// - Pager settings
|
|
||||||
// - Colorization options
|
|
||||||
// - Cheatpath definitions
|
|
||||||
// - Formatting preferences
|
|
||||||
//
|
|
||||||
// Example configuration:
|
|
||||||
//
|
|
||||||
// ---
|
|
||||||
// editor: vim
|
|
||||||
// colorize: true
|
|
||||||
// style: monokai
|
|
||||||
// formatter: terminal256
|
|
||||||
// pager: less -FRX
|
|
||||||
// cheatpaths:
|
|
||||||
// - name: personal
|
|
||||||
// path: ~/cheat
|
|
||||||
// tags: []
|
|
||||||
// readonly: false
|
|
||||||
// - name: community
|
|
||||||
// path: ~/cheat/.cheat
|
|
||||||
// tags: [community]
|
|
||||||
// readonly: true
|
|
||||||
//
|
|
||||||
// # Platform-Specific Paths
|
|
||||||
//
|
|
||||||
// The package automatically detects configuration paths based on the operating system:
|
|
||||||
// - Linux/Unix: $XDG_CONFIG_HOME/cheat/conf.yml or ~/.config/cheat/conf.yml
|
|
||||||
// - macOS: ~/Library/Application Support/cheat/conf.yml
|
|
||||||
// - Windows: %APPDATA%\cheat\conf.yml
|
|
||||||
//
|
|
||||||
// # Environment Variables
|
|
||||||
//
|
|
||||||
// The following environment variables are respected:
|
|
||||||
// - CHEAT_CONFIG_PATH: Override the configuration file location
|
|
||||||
// - CHEAT_USE_FZF: Enable fzf integration when set to "true"
|
|
||||||
// - EDITOR: Default editor if not specified in config
|
|
||||||
// - VISUAL: Fallback editor if EDITOR is not set
|
|
||||||
// - PAGER: Default pager if not specified in config
|
|
||||||
package config
|
|
||||||
20
internal/config/env.go
Normal file
20
internal/config/env.go
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
package config
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"runtime"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
// EnvVars reads environment variables into a map of strings.
|
||||||
|
func EnvVars() map[string]string {
|
||||||
|
envvars := map[string]string{}
|
||||||
|
for _, e := range os.Environ() {
|
||||||
|
pair := strings.SplitN(e, "=", 2)
|
||||||
|
if runtime.GOOS == "windows" {
|
||||||
|
pair[0] = strings.ToUpper(pair[0])
|
||||||
|
}
|
||||||
|
envvars[pair[0]] = pair[1]
|
||||||
|
}
|
||||||
|
return envvars
|
||||||
|
}
|
||||||
@@ -4,7 +4,6 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"runtime"
|
"runtime"
|
||||||
"strings"
|
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -90,9 +89,6 @@ func TestInitWriteError(t *testing.T) {
|
|||||||
if err == nil {
|
if err == nil {
|
||||||
t.Error("expected error when writing to invalid path, got nil")
|
t.Error("expected error when writing to invalid path, got nil")
|
||||||
}
|
}
|
||||||
if err != nil && !strings.Contains(err.Error(), "failed to create") {
|
|
||||||
t.Errorf("expected 'failed to create' error, got: %v", err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// TestInitExistingFile tests that Init overwrites existing files
|
// TestInitExistingFile tests that Init overwrites existing files
|
||||||
|
|||||||
147
internal/config/new.go
Normal file
147
internal/config/new.go
Normal file
@@ -0,0 +1,147 @@
|
|||||||
|
package config
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
cp "github.com/cheat/cheat/internal/cheatpath"
|
||||||
|
|
||||||
|
"github.com/mitchellh/go-homedir"
|
||||||
|
"gopkg.in/yaml.v3"
|
||||||
|
)
|
||||||
|
|
||||||
|
// New returns a new Config struct
|
||||||
|
func New(confPath string, resolve bool) (Config, error) {
|
||||||
|
|
||||||
|
// read the config file
|
||||||
|
buf, err := os.ReadFile(confPath)
|
||||||
|
if err != nil {
|
||||||
|
return Config{}, fmt.Errorf("could not read config file: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// initialize a config object
|
||||||
|
conf := Config{}
|
||||||
|
|
||||||
|
// store the config path
|
||||||
|
conf.Path = confPath
|
||||||
|
|
||||||
|
// unmarshal the yaml
|
||||||
|
err = yaml.Unmarshal(buf, &conf)
|
||||||
|
if err != nil {
|
||||||
|
return Config{}, fmt.Errorf("could not unmarshal yaml: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// if a .cheat directory exists in the current directory or any ancestor,
|
||||||
|
// append it to the cheatpaths
|
||||||
|
cwd, err := os.Getwd()
|
||||||
|
if err != nil {
|
||||||
|
return Config{}, fmt.Errorf("failed to get cwd: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if local := findLocalCheatpath(cwd); local != "" {
|
||||||
|
path := cp.Path{
|
||||||
|
Name: "cwd",
|
||||||
|
Path: local,
|
||||||
|
ReadOnly: false,
|
||||||
|
Tags: []string{},
|
||||||
|
}
|
||||||
|
conf.Cheatpaths = append(conf.Cheatpaths, path)
|
||||||
|
}
|
||||||
|
|
||||||
|
// process cheatpaths
|
||||||
|
var validPaths []cp.Path
|
||||||
|
for _, cheatpath := range conf.Cheatpaths {
|
||||||
|
|
||||||
|
// expand ~ in config paths
|
||||||
|
expanded, err := homedir.Expand(cheatpath.Path)
|
||||||
|
if err != nil {
|
||||||
|
return Config{}, fmt.Errorf("failed to expand ~: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// follow symlinks
|
||||||
|
//
|
||||||
|
// NB: `resolve` is an ugly kludge that exists for the sake of unit-tests.
|
||||||
|
// It's necessary because `EvalSymlinks` will error if the symlink points
|
||||||
|
// to a non-existent location on the filesystem. When unit-testing,
|
||||||
|
// however, we don't want to have dependencies on the filesystem. As such,
|
||||||
|
// `resolve` is a switch that allows us to turn off symlink resolution when
|
||||||
|
// running the config tests.
|
||||||
|
if resolve {
|
||||||
|
evaled, err := filepath.EvalSymlinks(expanded)
|
||||||
|
if err != nil {
|
||||||
|
// if the path simply doesn't exist, warn and skip it
|
||||||
|
if os.IsNotExist(err) {
|
||||||
|
fmt.Fprintf(os.Stderr,
|
||||||
|
"WARNING: cheatpath '%s' does not exist, skipping\n",
|
||||||
|
expanded,
|
||||||
|
)
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
return Config{}, fmt.Errorf(
|
||||||
|
"failed to resolve symlink: %s: %v",
|
||||||
|
expanded,
|
||||||
|
err,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
expanded = evaled
|
||||||
|
}
|
||||||
|
|
||||||
|
cheatpath.Path = expanded
|
||||||
|
validPaths = append(validPaths, cheatpath)
|
||||||
|
}
|
||||||
|
conf.Cheatpaths = validPaths
|
||||||
|
|
||||||
|
// determine the editor: env vars override the config file value,
|
||||||
|
// following standard Unix convention (see #589)
|
||||||
|
if v := os.Getenv("VISUAL"); v != "" {
|
||||||
|
conf.Editor = v
|
||||||
|
} else if v := os.Getenv("EDITOR"); v != "" {
|
||||||
|
conf.Editor = v
|
||||||
|
} else {
|
||||||
|
conf.Editor = strings.TrimSpace(conf.Editor)
|
||||||
|
}
|
||||||
|
|
||||||
|
// if an editor was still not determined, attempt to choose one
|
||||||
|
// that's appropriate for the environment
|
||||||
|
if conf.Editor == "" {
|
||||||
|
if conf.Editor, err = Editor(); err != nil {
|
||||||
|
return Config{}, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// if a chroma style was not provided, set a default
|
||||||
|
if conf.Style == "" {
|
||||||
|
conf.Style = "bw"
|
||||||
|
}
|
||||||
|
|
||||||
|
// if a chroma formatter was not provided, set a default
|
||||||
|
if conf.Formatter == "" {
|
||||||
|
conf.Formatter = "terminal"
|
||||||
|
}
|
||||||
|
|
||||||
|
// load the pager
|
||||||
|
conf.Pager = strings.TrimSpace(conf.Pager)
|
||||||
|
|
||||||
|
return conf, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// findLocalCheatpath walks upward from dir looking for a .cheat directory.
|
||||||
|
// It returns the path to the first .cheat directory found, or an empty string
|
||||||
|
// if none exists. This mirrors the discovery pattern used by git for .git
|
||||||
|
// directories.
|
||||||
|
func findLocalCheatpath(dir string) string {
|
||||||
|
for {
|
||||||
|
candidate := filepath.Join(dir, ".cheat")
|
||||||
|
if info, err := os.Stat(candidate); err == nil && info.IsDir() {
|
||||||
|
return candidate
|
||||||
|
}
|
||||||
|
parent := filepath.Dir(dir)
|
||||||
|
if parent == dir {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
dir = parent
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -38,7 +38,7 @@ cheatpaths:
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Load the config
|
// Load the config
|
||||||
conf, err := New(map[string]interface{}{}, configPath, false)
|
conf, err := New(configPath, false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("failed to load config: %v", err)
|
t.Fatalf("failed to load config: %v", err)
|
||||||
}
|
}
|
||||||
@@ -88,7 +88,7 @@ cheatpaths:
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Load the config
|
// Load the config
|
||||||
conf, err := New(map[string]interface{}{}, configPath, false)
|
conf, err := New(configPath, false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// It's OK if this fails due to no editor being found
|
// It's OK if this fails due to no editor being found
|
||||||
// The important thing is it doesn't panic
|
// The important thing is it doesn't panic
|
||||||
@@ -123,7 +123,7 @@ cheatpaths:
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Load the config
|
// Load the config
|
||||||
conf, err := New(map[string]interface{}{}, configPath, false)
|
conf, err := New(configPath, false)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("failed to load config: %v", err)
|
t.Fatalf("failed to load config: %v", err)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package config
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
"os"
|
||||||
|
"path/filepath"
|
||||||
"runtime"
|
"runtime"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
@@ -44,29 +45,20 @@ func TestPager(t *testing.T) {
|
|||||||
os.Setenv("PAGER", "")
|
os.Setenv("PAGER", "")
|
||||||
pager := Pager()
|
pager := Pager()
|
||||||
|
|
||||||
// Should find one of the fallback pagers or return empty string
|
if pager == "" {
|
||||||
|
return // no pager found is acceptable
|
||||||
|
}
|
||||||
|
|
||||||
|
// Should find one of the known fallback pagers
|
||||||
validPagers := map[string]bool{
|
validPagers := map[string]bool{
|
||||||
"": true, // no pager found
|
|
||||||
"pager": true,
|
"pager": true,
|
||||||
"less": true,
|
"less": true,
|
||||||
"more": true,
|
"more": true,
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if it's a path to one of these
|
base := filepath.Base(pager)
|
||||||
found := false
|
if !validPagers[base] {
|
||||||
for p := range validPagers {
|
t.Errorf("unexpected pager value: %s (base: %s)", pager, base)
|
||||||
if p == "" && pager == "" {
|
|
||||||
found = true
|
|
||||||
break
|
|
||||||
}
|
|
||||||
if p != "" && (pager == p || len(pager) >= len(p) && pager[len(pager)-len(p):] == p) {
|
|
||||||
found = true
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if !found {
|
|
||||||
t.Errorf("unexpected pager value: %s", pager)
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
@@ -14,8 +14,8 @@ func TestValidateCorrect(t *testing.T) {
|
|||||||
Colorize: true,
|
Colorize: true,
|
||||||
Editor: "vim",
|
Editor: "vim",
|
||||||
Formatter: "terminal16m",
|
Formatter: "terminal16m",
|
||||||
Cheatpaths: []cheatpath.Cheatpath{
|
Cheatpaths: []cheatpath.Path{
|
||||||
cheatpath.Cheatpath{
|
cheatpath.Path{
|
||||||
Name: "foo",
|
Name: "foo",
|
||||||
Path: "/foo",
|
Path: "/foo",
|
||||||
ReadOnly: false,
|
ReadOnly: false,
|
||||||
@@ -38,8 +38,8 @@ func TestInvalidateMissingEditor(t *testing.T) {
|
|||||||
conf := Config{
|
conf := Config{
|
||||||
Colorize: true,
|
Colorize: true,
|
||||||
Formatter: "terminal16m",
|
Formatter: "terminal16m",
|
||||||
Cheatpaths: []cheatpath.Cheatpath{
|
Cheatpaths: []cheatpath.Path{
|
||||||
cheatpath.Cheatpath{
|
cheatpath.Path{
|
||||||
Name: "foo",
|
Name: "foo",
|
||||||
Path: "/foo",
|
Path: "/foo",
|
||||||
ReadOnly: false,
|
ReadOnly: false,
|
||||||
@@ -71,19 +71,28 @@ func TestInvalidateMissingCheatpaths(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TestMissingInvalidFormatters asserts that configs which contain invalid
|
// TestInvalidateInvalidFormatter asserts that configs which contain invalid
|
||||||
// formatters are invalidated
|
// formatters are invalidated
|
||||||
func TestMissingInvalidFormatters(t *testing.T) {
|
func TestInvalidateInvalidFormatter(t *testing.T) {
|
||||||
|
|
||||||
// mock a config
|
// mock a config with a valid editor and cheatpaths but invalid formatter
|
||||||
conf := Config{
|
conf := Config{
|
||||||
Colorize: true,
|
Colorize: true,
|
||||||
Editor: "vim",
|
Editor: "vim",
|
||||||
|
Formatter: "html",
|
||||||
|
Cheatpaths: []cheatpath.Path{
|
||||||
|
cheatpath.Path{
|
||||||
|
Name: "foo",
|
||||||
|
Path: "/foo",
|
||||||
|
ReadOnly: false,
|
||||||
|
Tags: []string{},
|
||||||
|
},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
// assert that no errors are returned
|
// assert that the config is invalidated due to the formatter
|
||||||
if err := conf.Validate(); err == nil {
|
if err := conf.Validate(); err == nil {
|
||||||
t.Errorf("failed to invalidate config without formatter")
|
t.Errorf("failed to invalidate config with invalid formatter")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -96,14 +105,14 @@ func TestInvalidateDuplicateCheatpathNames(t *testing.T) {
|
|||||||
Colorize: true,
|
Colorize: true,
|
||||||
Editor: "vim",
|
Editor: "vim",
|
||||||
Formatter: "terminal16m",
|
Formatter: "terminal16m",
|
||||||
Cheatpaths: []cheatpath.Cheatpath{
|
Cheatpaths: []cheatpath.Path{
|
||||||
cheatpath.Cheatpath{
|
cheatpath.Path{
|
||||||
Name: "foo",
|
Name: "foo",
|
||||||
Path: "/foo",
|
Path: "/foo",
|
||||||
ReadOnly: false,
|
ReadOnly: false,
|
||||||
Tags: []string{},
|
Tags: []string{},
|
||||||
},
|
},
|
||||||
cheatpath.Cheatpath{
|
cheatpath.Path{
|
||||||
Name: "foo",
|
Name: "foo",
|
||||||
Path: "/bar",
|
Path: "/bar",
|
||||||
ReadOnly: false,
|
ReadOnly: false,
|
||||||
@@ -127,14 +136,14 @@ func TestInvalidateDuplicateCheatpathPaths(t *testing.T) {
|
|||||||
Colorize: true,
|
Colorize: true,
|
||||||
Editor: "vim",
|
Editor: "vim",
|
||||||
Formatter: "terminal16m",
|
Formatter: "terminal16m",
|
||||||
Cheatpaths: []cheatpath.Cheatpath{
|
Cheatpaths: []cheatpath.Path{
|
||||||
cheatpath.Cheatpath{
|
cheatpath.Path{
|
||||||
Name: "foo",
|
Name: "foo",
|
||||||
Path: "/foo",
|
Path: "/foo",
|
||||||
ReadOnly: false,
|
ReadOnly: false,
|
||||||
Tags: []string{},
|
Tags: []string{},
|
||||||
},
|
},
|
||||||
cheatpath.Cheatpath{
|
cheatpath.Path{
|
||||||
Name: "bar",
|
Name: "bar",
|
||||||
Path: "/foo",
|
Path: "/foo",
|
||||||
ReadOnly: false,
|
ReadOnly: false,
|
||||||
@@ -148,3 +157,28 @@ func TestInvalidateDuplicateCheatpathPaths(t *testing.T) {
|
|||||||
t.Errorf("failed to invalidate config with cheatpaths with duplicate paths")
|
t.Errorf("failed to invalidate config with cheatpaths with duplicate paths")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TestInvalidateInvalidCheatpath asserts that configs containing a cheatpath
|
||||||
|
// with an empty name are invalidated
|
||||||
|
func TestInvalidateInvalidCheatpath(t *testing.T) {
|
||||||
|
|
||||||
|
// mock a config with a cheatpath that has an empty name
|
||||||
|
conf := Config{
|
||||||
|
Colorize: true,
|
||||||
|
Editor: "vim",
|
||||||
|
Formatter: "terminal16m",
|
||||||
|
Cheatpaths: []cheatpath.Path{
|
||||||
|
cheatpath.Path{
|
||||||
|
Name: "",
|
||||||
|
Path: "/foo",
|
||||||
|
ReadOnly: false,
|
||||||
|
Tags: []string{},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
// assert that an error is returned
|
||||||
|
if err := conf.Validate(); err == nil {
|
||||||
|
t.Errorf("failed to invalidate config with invalid cheatpath (empty name)")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,45 +0,0 @@
|
|||||||
// Package display handles output formatting and presentation for the cheat application.
|
|
||||||
//
|
|
||||||
// The display package provides utilities for:
|
|
||||||
// - Writing output to stdout or a pager
|
|
||||||
// - Formatting text with indentation
|
|
||||||
// - Creating faint (dimmed) text for de-emphasis
|
|
||||||
// - Managing colored output
|
|
||||||
//
|
|
||||||
// # Pager Integration
|
|
||||||
//
|
|
||||||
// The package integrates with system pagers (less, more, etc.) to handle
|
|
||||||
// long output. If a pager is configured and the output is to a terminal,
|
|
||||||
// content is automatically piped through the pager.
|
|
||||||
//
|
|
||||||
// # Text Formatting
|
|
||||||
//
|
|
||||||
// Various formatting utilities are provided:
|
|
||||||
// - Faint: Creates dimmed text using ANSI escape codes
|
|
||||||
// - Indent: Adds consistent indentation to text blocks
|
|
||||||
// - Write: Intelligent output that uses stdout or pager as appropriate
|
|
||||||
//
|
|
||||||
// Example Usage
|
|
||||||
//
|
|
||||||
// // Write output, using pager if configured
|
|
||||||
// if err := display.Write(output, config); err != nil {
|
|
||||||
// log.Fatal(err)
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// // Create faint text for de-emphasis
|
|
||||||
// fainted := display.Faint("(read-only)", config)
|
|
||||||
//
|
|
||||||
// // Indent a block of text
|
|
||||||
// indented := display.Indent(text, " ")
|
|
||||||
//
|
|
||||||
// # Color Support
|
|
||||||
//
|
|
||||||
// The package respects the colorization settings from the config.
|
|
||||||
// When colorization is disabled, formatting functions like Faint
|
|
||||||
// return unmodified text.
|
|
||||||
//
|
|
||||||
// # Terminal Detection
|
|
||||||
//
|
|
||||||
// The package uses isatty to detect if output is to a terminal,
|
|
||||||
// which affects decisions about using a pager and applying colors.
|
|
||||||
package display
|
|
||||||
@@ -2,17 +2,13 @@
|
|||||||
// cheatsheet content to stdout, or alternatively the system pager.
|
// cheatsheet content to stdout, or alternatively the system pager.
|
||||||
package display
|
package display
|
||||||
|
|
||||||
import (
|
import "fmt"
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"github.com/cheat/cheat/internal/config"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Faint returns a faintly-colored string that's used to de-prioritize text
|
// Faint returns a faintly-colored string that's used to de-prioritize text
|
||||||
// written to stdout
|
// written to stdout
|
||||||
func Faint(str string, conf config.Config) string {
|
func Faint(str string, colorize bool) string {
|
||||||
// make `str` faint only if colorization has been requested
|
// make `str` faint only if colorization has been requested
|
||||||
if conf.Colorize {
|
if colorize {
|
||||||
return fmt.Sprintf("\033[2m%s\033[0m", str)
|
return fmt.Sprintf("\033[2m%s\033[0m", str)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,26 +1,20 @@
|
|||||||
package display
|
package display
|
||||||
|
|
||||||
import (
|
import "testing"
|
||||||
"testing"
|
|
||||||
|
|
||||||
"github.com/cheat/cheat/internal/config"
|
|
||||||
)
|
|
||||||
|
|
||||||
// TestFaint asserts that Faint applies faint formatting
|
// TestFaint asserts that Faint applies faint formatting
|
||||||
func TestFaint(t *testing.T) {
|
func TestFaint(t *testing.T) {
|
||||||
|
|
||||||
// case: apply colorization
|
// case: apply colorization
|
||||||
conf := config.Config{Colorize: true}
|
|
||||||
want := "\033[2mfoo\033[0m"
|
want := "\033[2mfoo\033[0m"
|
||||||
got := Faint("foo", conf)
|
got := Faint("foo", true)
|
||||||
if want != got {
|
if want != got {
|
||||||
t.Errorf("failed to faint: want: %s, got: %s", want, got)
|
t.Errorf("failed to faint: want: %s, got: %s", want, got)
|
||||||
}
|
}
|
||||||
|
|
||||||
// case: do not apply colorization
|
// case: do not apply colorization
|
||||||
conf.Colorize = false
|
|
||||||
want = "foo"
|
want = "foo"
|
||||||
got = Faint("foo", conf)
|
got = Faint("foo", false)
|
||||||
if want != got {
|
if want != got {
|
||||||
t.Errorf("failed to faint: want: %s, got: %s", want, got)
|
t.Errorf("failed to faint: want: %s, got: %s", want, got)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,3 +10,13 @@ func TestIndent(t *testing.T) {
|
|||||||
t.Errorf("failed to indent: want: %s, got: %s", want, got)
|
t.Errorf("failed to indent: want: %s, got: %s", want, got)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TestIndentTrimsWhitespace asserts that Indent trims leading and trailing
|
||||||
|
// whitespace before indenting
|
||||||
|
func TestIndentTrimsWhitespace(t *testing.T) {
|
||||||
|
got := Indent(" foo\nbar\nbaz \n")
|
||||||
|
want := "\tfoo\n\tbar\n\tbaz\n"
|
||||||
|
if got != want {
|
||||||
|
t.Errorf("failed to trim and indent: want: %q, got: %q", want, got)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -24,7 +24,7 @@ func Write(out string, conf config.Config) {
|
|||||||
|
|
||||||
// writeToPager writes output through a pager command
|
// writeToPager writes output through a pager command
|
||||||
func writeToPager(out string, conf config.Config) {
|
func writeToPager(out string, conf config.Config) {
|
||||||
parts := strings.Split(conf.Pager, " ")
|
parts := strings.Fields(conf.Pager)
|
||||||
pager := parts[0]
|
pager := parts[0]
|
||||||
args := parts[1:]
|
args := parts[1:]
|
||||||
|
|
||||||
|
|||||||
@@ -2,7 +2,6 @@ package installer
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"fmt"
|
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -158,23 +157,3 @@ func TestPromptError(t *testing.T) {
|
|||||||
t.Errorf("expected 'failed to prompt' error, got: %v", err)
|
t.Errorf("expected 'failed to prompt' error, got: %v", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TestPromptIntegration provides a simple integration test
|
|
||||||
func TestPromptIntegration(t *testing.T) {
|
|
||||||
// This demonstrates how the prompt would be used in practice
|
|
||||||
// It's skipped by default since it requires actual user input
|
|
||||||
if os.Getenv("TEST_INTERACTIVE") != "1" {
|
|
||||||
t.Skip("Skipping interactive test - set TEST_INTERACTIVE=1 to run")
|
|
||||||
}
|
|
||||||
|
|
||||||
fmt.Println("\n=== Interactive Prompt Test ===")
|
|
||||||
fmt.Println("You will be prompted to answer a question.")
|
|
||||||
fmt.Println("Try different inputs: y, n, Y, N, empty (just press Enter)")
|
|
||||||
|
|
||||||
result, err := Prompt("Would you like to continue? [Y/n]", true)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("Prompt failed: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
fmt.Printf("You answered: %v\n", result)
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -3,8 +3,6 @@ package installer
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/cheat/cheat/internal/config"
|
"github.com/cheat/cheat/internal/config"
|
||||||
"github.com/cheat/cheat/internal/repo"
|
"github.com/cheat/cheat/internal/repo"
|
||||||
@@ -13,27 +11,11 @@ import (
|
|||||||
// Run runs the installer
|
// Run runs the installer
|
||||||
func Run(configs string, confpath string) error {
|
func Run(configs string, confpath string) error {
|
||||||
|
|
||||||
// determine the appropriate paths for config data and (optional) community
|
// expand template placeholders with platform-appropriate paths
|
||||||
// cheatsheets based on the user's platform
|
configs = ExpandTemplate(configs, confpath)
|
||||||
confdir := filepath.Dir(confpath)
|
|
||||||
|
|
||||||
// create paths for community, personal, and work cheatsheets
|
// determine cheatsheet directory paths
|
||||||
community := filepath.Join(confdir, "cheatsheets", "community")
|
community, personal, work := cheatsheetDirs(confpath)
|
||||||
personal := filepath.Join(confdir, "cheatsheets", "personal")
|
|
||||||
work := filepath.Join(confdir, "cheatsheets", "work")
|
|
||||||
|
|
||||||
// set default cheatpaths
|
|
||||||
configs = strings.Replace(configs, "COMMUNITY_PATH", community, -1)
|
|
||||||
configs = strings.Replace(configs, "PERSONAL_PATH", personal, -1)
|
|
||||||
configs = strings.Replace(configs, "WORK_PATH", work, -1)
|
|
||||||
|
|
||||||
// locate and set a default pager
|
|
||||||
configs = strings.Replace(configs, "PAGER_PATH", config.Pager(), -1)
|
|
||||||
|
|
||||||
// locate and set a default editor
|
|
||||||
if editor, err := config.Editor(); err == nil {
|
|
||||||
configs = strings.Replace(configs, "EDITOR_PATH", editor, -1)
|
|
||||||
}
|
|
||||||
|
|
||||||
// prompt the user to download the community cheatsheets
|
// prompt the user to download the community cheatsheets
|
||||||
yes, err := Prompt(
|
yes, err := Prompt(
|
||||||
@@ -51,19 +33,7 @@ func Run(configs string, confpath string) error {
|
|||||||
return fmt.Errorf("failed to clone cheatsheets: %v", err)
|
return fmt.Errorf("failed to clone cheatsheets: %v", err)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
// comment out the community cheatpath in the config since
|
configs = CommentCommunity(configs, confpath)
|
||||||
// the directory won't exist
|
|
||||||
configs = strings.Replace(configs,
|
|
||||||
" - name: community\n"+
|
|
||||||
" path: "+community+"\n"+
|
|
||||||
" tags: [ community ]\n"+
|
|
||||||
" readonly: true",
|
|
||||||
" #- name: community\n"+
|
|
||||||
" # path: "+community+"\n"+
|
|
||||||
" # tags: [ community ]\n"+
|
|
||||||
" # readonly: true",
|
|
||||||
-1,
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// always create personal and work directories
|
// always create personal and work directories
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
package installer
|
package installer
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
@@ -245,10 +244,10 @@ cheatpaths:
|
|||||||
if strings.Contains(contentStr, "PERSONAL_PATH") {
|
if strings.Contains(contentStr, "PERSONAL_PATH") {
|
||||||
t.Error("PERSONAL_PATH was not replaced")
|
t.Error("PERSONAL_PATH was not replaced")
|
||||||
}
|
}
|
||||||
if strings.Contains(contentStr, "EDITOR_PATH") && !strings.Contains(contentStr, fmt.Sprintf("editor: %s", "")) {
|
if strings.Contains(contentStr, "EDITOR_PATH") {
|
||||||
t.Error("EDITOR_PATH was not replaced")
|
t.Error("EDITOR_PATH was not replaced")
|
||||||
}
|
}
|
||||||
if strings.Contains(contentStr, "PAGER_PATH") && !strings.Contains(contentStr, fmt.Sprintf("pager: %s", "")) {
|
if strings.Contains(contentStr, "PAGER_PATH") {
|
||||||
t.Error("PAGER_PATH was not replaced")
|
t.Error("PAGER_PATH was not replaced")
|
||||||
}
|
}
|
||||||
if strings.Contains(contentStr, "WORK_PATH") {
|
if strings.Contains(contentStr, "WORK_PATH") {
|
||||||
|
|||||||
58
internal/installer/template.go
Normal file
58
internal/installer/template.go
Normal file
@@ -0,0 +1,58 @@
|
|||||||
|
package installer
|
||||||
|
|
||||||
|
import (
|
||||||
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/cheat/cheat/internal/config"
|
||||||
|
)
|
||||||
|
|
||||||
|
// cheatsheetDirs returns the community, personal, and work cheatsheet
|
||||||
|
// directory paths derived from a config file path.
|
||||||
|
func cheatsheetDirs(confpath string) (community, personal, work string) {
|
||||||
|
confdir := filepath.Dir(confpath)
|
||||||
|
community = filepath.Join(confdir, "cheatsheets", "community")
|
||||||
|
personal = filepath.Join(confdir, "cheatsheets", "personal")
|
||||||
|
work = filepath.Join(confdir, "cheatsheets", "work")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// ExpandTemplate replaces placeholder tokens in the config template with
|
||||||
|
// platform-appropriate paths derived from confpath.
|
||||||
|
func ExpandTemplate(configs string, confpath string) string {
|
||||||
|
community, personal, work := cheatsheetDirs(confpath)
|
||||||
|
|
||||||
|
// substitute paths
|
||||||
|
configs = strings.ReplaceAll(configs, "COMMUNITY_PATH", community)
|
||||||
|
configs = strings.ReplaceAll(configs, "PERSONAL_PATH", personal)
|
||||||
|
configs = strings.ReplaceAll(configs, "WORK_PATH", work)
|
||||||
|
|
||||||
|
// locate and set a default pager
|
||||||
|
configs = strings.ReplaceAll(configs, "PAGER_PATH", config.Pager())
|
||||||
|
|
||||||
|
// locate and set a default editor
|
||||||
|
if editor, err := config.Editor(); err == nil {
|
||||||
|
configs = strings.ReplaceAll(configs, "EDITOR_PATH", editor)
|
||||||
|
}
|
||||||
|
|
||||||
|
return configs
|
||||||
|
}
|
||||||
|
|
||||||
|
// CommentCommunity comments out the community cheatpath block in the config
|
||||||
|
// template. This is used when the community cheatsheets directory won't exist
|
||||||
|
// (either because the user declined to download them, or because the config
|
||||||
|
// is being output as an example).
|
||||||
|
func CommentCommunity(configs string, confpath string) string {
|
||||||
|
community, _, _ := cheatsheetDirs(confpath)
|
||||||
|
|
||||||
|
return strings.ReplaceAll(configs,
|
||||||
|
" - name: community\n"+
|
||||||
|
" path: "+community+"\n"+
|
||||||
|
" tags: [ community ]\n"+
|
||||||
|
" readonly: true",
|
||||||
|
" #- name: community\n"+
|
||||||
|
" # path: "+community+"\n"+
|
||||||
|
" # tags: [ community ]\n"+
|
||||||
|
" # readonly: true",
|
||||||
|
)
|
||||||
|
}
|
||||||
@@ -1,29 +0,0 @@
|
|||||||
// Package mock implements mock functions used in unit-tests.
|
|
||||||
package mock
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"path/filepath"
|
|
||||||
"runtime"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Path returns the absolute path to the specified mock file.
|
|
||||||
func Path(filename string) string {
|
|
||||||
|
|
||||||
// determine the path of this file during runtime
|
|
||||||
_, thisfile, _, _ := runtime.Caller(0)
|
|
||||||
|
|
||||||
// compute the mock path
|
|
||||||
file, err := filepath.Abs(
|
|
||||||
filepath.Join(
|
|
||||||
filepath.Dir(thisfile),
|
|
||||||
"../../mocks",
|
|
||||||
filename,
|
|
||||||
),
|
|
||||||
)
|
|
||||||
if err != nil {
|
|
||||||
panic(fmt.Errorf("failed to resolve mock path: %v", err))
|
|
||||||
}
|
|
||||||
|
|
||||||
return file
|
|
||||||
}
|
|
||||||
130
internal/repo/pull.go
Normal file
130
internal/repo/pull.go
Normal file
@@ -0,0 +1,130 @@
|
|||||||
|
package repo
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
|
||||||
|
"github.com/go-git/go-git/v5"
|
||||||
|
"github.com/go-git/go-git/v5/plumbing/transport"
|
||||||
|
gitssh "github.com/go-git/go-git/v5/plumbing/transport/ssh"
|
||||||
|
"github.com/mitchellh/go-homedir"
|
||||||
|
)
|
||||||
|
|
||||||
|
// ErrDirtyWorktree indicates that the worktree has uncommitted changes.
|
||||||
|
var ErrDirtyWorktree = errors.New("dirty worktree")
|
||||||
|
|
||||||
|
// Pull performs a git pull on the repository at path. It returns
|
||||||
|
// ErrDirtyWorktree if the worktree has uncommitted changes, and
|
||||||
|
// git.ErrRepositoryNotExists if path is not a git repository.
|
||||||
|
func Pull(path string) error {
|
||||||
|
|
||||||
|
// open the repository
|
||||||
|
r, err := git.PlainOpen(path)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// get the worktree
|
||||||
|
wt, err := r.Worktree()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// check if the worktree is clean
|
||||||
|
status, err := wt.Status()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if !status.IsClean() {
|
||||||
|
return ErrDirtyWorktree
|
||||||
|
}
|
||||||
|
|
||||||
|
// build pull options, using SSH auth when the remote is SSH
|
||||||
|
opts := &git.PullOptions{}
|
||||||
|
if auth, err := sshAuth(r); err == nil && auth != nil {
|
||||||
|
opts.Auth = auth
|
||||||
|
}
|
||||||
|
|
||||||
|
// pull
|
||||||
|
err = wt.Pull(opts)
|
||||||
|
if errors.Is(err, git.NoErrAlreadyUpToDate) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// defaultKeyFiles are the SSH key filenames tried in order, matching the
|
||||||
|
// default behavior of OpenSSH.
|
||||||
|
var defaultKeyFiles = []string{
|
||||||
|
"id_rsa",
|
||||||
|
"id_ecdsa",
|
||||||
|
"id_ecdsa_sk",
|
||||||
|
"id_ed25519",
|
||||||
|
"id_ed25519_sk",
|
||||||
|
"id_dsa",
|
||||||
|
}
|
||||||
|
|
||||||
|
// sshAuth returns an appropriate SSH auth method if the origin remote uses
|
||||||
|
// the SSH protocol, or nil if it does not. It tries the SSH agent first, then
|
||||||
|
// falls back to default key files in ~/.ssh/.
|
||||||
|
func sshAuth(r *git.Repository) (transport.AuthMethod, error) {
|
||||||
|
remote, err := r.Remote("origin")
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
urls := remote.Config().URLs
|
||||||
|
if len(urls) == 0 {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
ep, err := transport.NewEndpoint(urls[0])
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if ep.Protocol != "ssh" {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
user := ep.User
|
||||||
|
if user == "" {
|
||||||
|
user = "git"
|
||||||
|
}
|
||||||
|
|
||||||
|
// try default key files first — this is more reliable than the SSH
|
||||||
|
// agent, which may report success even when no keys are loaded
|
||||||
|
home, err := homedir.Dir()
|
||||||
|
if err == nil {
|
||||||
|
if auth := findKeyFile(filepath.Join(home, ".ssh"), user); auth != nil {
|
||||||
|
return auth, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// fall back to SSH agent
|
||||||
|
if auth, err := gitssh.NewSSHAgentAuth(user); err == nil {
|
||||||
|
return auth, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// findKeyFile looks for a usable SSH private key in sshDir, trying the
|
||||||
|
// standard OpenSSH default filenames in order. Returns nil if no usable key
|
||||||
|
// is found.
|
||||||
|
func findKeyFile(sshDir, user string) transport.AuthMethod {
|
||||||
|
for _, name := range defaultKeyFiles {
|
||||||
|
keyPath := filepath.Join(sshDir, name)
|
||||||
|
if _, err := os.Stat(keyPath); err != nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
auth, err := gitssh.NewPublicKeysFromFile(user, keyPath, "")
|
||||||
|
if err != nil {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
return auth
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
315
internal/repo/pull_test.go
Normal file
315
internal/repo/pull_test.go
Normal file
@@ -0,0 +1,315 @@
|
|||||||
|
package repo
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/ed25519"
|
||||||
|
"crypto/x509"
|
||||||
|
"encoding/pem"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/go-git/go-git/v5"
|
||||||
|
gitconfig "github.com/go-git/go-git/v5/config"
|
||||||
|
"github.com/go-git/go-git/v5/plumbing/object"
|
||||||
|
)
|
||||||
|
|
||||||
|
// testCommitOpts returns a CommitOptions suitable for test commits.
|
||||||
|
func testCommitOpts() *git.CommitOptions {
|
||||||
|
return &git.CommitOptions{
|
||||||
|
Author: &object.Signature{
|
||||||
|
Name: "test",
|
||||||
|
Email: "test@test",
|
||||||
|
When: time.Now(),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// initBareRepoWithCommit creates a bare git repository at dir with an initial
|
||||||
|
// commit, suitable for use as a remote.
|
||||||
|
func initBareRepoWithCommit(t *testing.T, dir string) {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
|
// init a non-bare repo to make the commit, then we'll clone it as bare
|
||||||
|
tmpWork := t.TempDir()
|
||||||
|
r, err := git.PlainInit(tmpWork, false)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to init repo: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
f := filepath.Join(tmpWork, "README")
|
||||||
|
if err := os.WriteFile(f, []byte("hello\n"), 0644); err != nil {
|
||||||
|
t.Fatalf("failed to write file: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
wt, err := r.Worktree()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to get worktree: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := wt.Add("README"); err != nil {
|
||||||
|
t.Fatalf("failed to stage file: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err = wt.Commit("initial commit", testCommitOpts()); err != nil {
|
||||||
|
t.Fatalf("failed to commit: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// clone as bare into the target dir
|
||||||
|
if _, err = git.PlainClone(dir, true, &git.CloneOptions{URL: tmpWork}); err != nil {
|
||||||
|
t.Fatalf("failed to create bare clone: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// cloneLocal clones the bare repo at bareDir into a new working directory and
|
||||||
|
// returns the path.
|
||||||
|
func cloneLocal(t *testing.T, bareDir string) string {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
|
dir := t.TempDir()
|
||||||
|
_, err := git.PlainClone(dir, false, &git.CloneOptions{
|
||||||
|
URL: bareDir,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to clone: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
return dir
|
||||||
|
}
|
||||||
|
|
||||||
|
// pushNewCommit clones bareDir into a temporary working copy, commits a new
|
||||||
|
// file, and pushes back to the bare repo.
|
||||||
|
func pushNewCommit(t *testing.T, bareDir string) {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
|
tmpWork := t.TempDir()
|
||||||
|
r, err := git.PlainClone(tmpWork, false, &git.CloneOptions{URL: bareDir})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to clone for push: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := os.WriteFile(filepath.Join(tmpWork, "new.txt"), []byte("new\n"), 0644); err != nil {
|
||||||
|
t.Fatalf("failed to write file: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
wt, err := r.Worktree()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to get worktree: %v", err)
|
||||||
|
}
|
||||||
|
if _, err := wt.Add("new.txt"); err != nil {
|
||||||
|
t.Fatalf("failed to stage file: %v", err)
|
||||||
|
}
|
||||||
|
if _, err := wt.Commit("add new file", testCommitOpts()); err != nil {
|
||||||
|
t.Fatalf("failed to commit: %v", err)
|
||||||
|
}
|
||||||
|
if err := r.Push(&git.PushOptions{}); err != nil {
|
||||||
|
t.Fatalf("failed to push: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// generateTestKey creates an unencrypted ed25519 PEM private key file at path.
|
||||||
|
func generateTestKey(t *testing.T, path string) {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
|
_, priv, err := ed25519.GenerateKey(nil)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to generate key: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
der, err := x509.MarshalPKCS8PrivateKey(priv)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to marshal key: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
pemBytes := pem.EncodeToMemory(&pem.Block{Type: "PRIVATE KEY", Bytes: der})
|
||||||
|
if err := os.WriteFile(path, pemBytes, 0600); err != nil {
|
||||||
|
t.Fatalf("failed to write key file: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- Pull tests ---
|
||||||
|
|
||||||
|
func TestPull_NotARepo(t *testing.T) {
|
||||||
|
dir := t.TempDir()
|
||||||
|
|
||||||
|
err := Pull(dir)
|
||||||
|
if err != git.ErrRepositoryNotExists {
|
||||||
|
t.Fatalf("expected ErrRepositoryNotExists, got: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPull_CleanAlreadyUpToDate(t *testing.T) {
|
||||||
|
bare := t.TempDir()
|
||||||
|
initBareRepoWithCommit(t, bare)
|
||||||
|
clone := cloneLocal(t, bare)
|
||||||
|
|
||||||
|
err := Pull(clone)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("expected nil (already up-to-date), got: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPull_NewUpstreamChanges(t *testing.T) {
|
||||||
|
bare := t.TempDir()
|
||||||
|
initBareRepoWithCommit(t, bare)
|
||||||
|
clone := cloneLocal(t, bare)
|
||||||
|
|
||||||
|
// push a new commit to the bare repo after the clone
|
||||||
|
pushNewCommit(t, bare)
|
||||||
|
|
||||||
|
err := Pull(clone)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("expected nil (successful pull), got: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// verify the new file was pulled
|
||||||
|
if _, err := os.Stat(filepath.Join(clone, "new.txt")); err != nil {
|
||||||
|
t.Fatalf("expected new.txt to exist after pull: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPull_DirtyWorktree(t *testing.T) {
|
||||||
|
bare := t.TempDir()
|
||||||
|
initBareRepoWithCommit(t, bare)
|
||||||
|
clone := cloneLocal(t, bare)
|
||||||
|
|
||||||
|
// make the worktree dirty with a modified tracked file
|
||||||
|
if err := os.WriteFile(filepath.Join(clone, "README"), []byte("changed\n"), 0644); err != nil {
|
||||||
|
t.Fatalf("failed to modify file: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
err := Pull(clone)
|
||||||
|
if err != ErrDirtyWorktree {
|
||||||
|
t.Fatalf("expected ErrDirtyWorktree, got: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPull_DirtyWorktreeUntracked(t *testing.T) {
|
||||||
|
bare := t.TempDir()
|
||||||
|
initBareRepoWithCommit(t, bare)
|
||||||
|
clone := cloneLocal(t, bare)
|
||||||
|
|
||||||
|
// make the worktree dirty with an untracked file
|
||||||
|
if err := os.WriteFile(filepath.Join(clone, "untracked.txt"), []byte("new\n"), 0644); err != nil {
|
||||||
|
t.Fatalf("failed to create file: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
err := Pull(clone)
|
||||||
|
if err != ErrDirtyWorktree {
|
||||||
|
t.Fatalf("expected ErrDirtyWorktree, got: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- sshAuth tests ---
|
||||||
|
|
||||||
|
func TestSshAuth_NonSSHRemote(t *testing.T) {
|
||||||
|
bare := t.TempDir()
|
||||||
|
initBareRepoWithCommit(t, bare)
|
||||||
|
clone := cloneLocal(t, bare)
|
||||||
|
|
||||||
|
r, err := git.PlainOpen(clone)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to open repo: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// the clone's origin is a local file:// path, not SSH
|
||||||
|
auth, err := sshAuth(r)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("expected nil error, got: %v", err)
|
||||||
|
}
|
||||||
|
if auth != nil {
|
||||||
|
t.Fatalf("expected nil auth for non-SSH remote, got: %v", auth)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSshAuth_NoRemote(t *testing.T) {
|
||||||
|
dir := t.TempDir()
|
||||||
|
r, err := git.PlainInit(dir, false)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to init repo: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// repo has no remotes
|
||||||
|
auth, err := sshAuth(r)
|
||||||
|
if err == nil {
|
||||||
|
t.Fatalf("expected error for missing remote, got auth: %v", auth)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestSshAuth_SSHRemote(t *testing.T) {
|
||||||
|
dir := t.TempDir()
|
||||||
|
r, err := git.PlainInit(dir, false)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to init repo: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// add an SSH remote
|
||||||
|
_, err = r.CreateRemote(&gitconfig.RemoteConfig{
|
||||||
|
Name: "origin",
|
||||||
|
URLs: []string{"git@github.com:example/repo.git"},
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("failed to create remote: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// sshAuth should not return an error — even if no key is found, it
|
||||||
|
// returns (nil, nil) rather than an error
|
||||||
|
auth, err := sshAuth(r)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("expected nil error, got: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// we can't predict whether auth is nil or non-nil here because it
|
||||||
|
// depends on whether the test runner has SSH keys or an agent; just
|
||||||
|
// verify it didn't error
|
||||||
|
_ = auth
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- findKeyFile tests ---
|
||||||
|
|
||||||
|
func TestFindKeyFile_ValidKey(t *testing.T) {
|
||||||
|
sshDir := t.TempDir()
|
||||||
|
generateTestKey(t, filepath.Join(sshDir, "id_ed25519"))
|
||||||
|
|
||||||
|
auth := findKeyFile(sshDir, "git")
|
||||||
|
if auth == nil {
|
||||||
|
t.Fatal("expected non-nil auth for valid key file")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFindKeyFile_NoKeys(t *testing.T) {
|
||||||
|
sshDir := t.TempDir()
|
||||||
|
|
||||||
|
auth := findKeyFile(sshDir, "git")
|
||||||
|
if auth != nil {
|
||||||
|
t.Fatalf("expected nil auth for empty directory, got: %v", auth)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFindKeyFile_InvalidKey(t *testing.T) {
|
||||||
|
sshDir := t.TempDir()
|
||||||
|
// write garbage into a file named like a key
|
||||||
|
if err := os.WriteFile(filepath.Join(sshDir, "id_ed25519"), []byte("not a key"), 0600); err != nil {
|
||||||
|
t.Fatalf("failed to write file: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
auth := findKeyFile(sshDir, "git")
|
||||||
|
if auth != nil {
|
||||||
|
t.Fatalf("expected nil auth for invalid key file, got: %v", auth)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFindKeyFile_SkipsInvalidFindsValid(t *testing.T) {
|
||||||
|
sshDir := t.TempDir()
|
||||||
|
|
||||||
|
// put garbage in id_rsa (tried first), valid key in id_ed25519 (tried later)
|
||||||
|
if err := os.WriteFile(filepath.Join(sshDir, "id_rsa"), []byte("not a key"), 0600); err != nil {
|
||||||
|
t.Fatalf("failed to write file: %v", err)
|
||||||
|
}
|
||||||
|
generateTestKey(t, filepath.Join(sshDir, "id_ed25519"))
|
||||||
|
|
||||||
|
auth := findKeyFile(sshDir, "git")
|
||||||
|
if auth == nil {
|
||||||
|
t.Fatal("expected non-nil auth; should skip invalid id_rsa and find id_ed25519")
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1 +0,0 @@
|
|||||||
package repo
|
|
||||||
@@ -1,6 +1,7 @@
|
|||||||
package sheet
|
package sheet
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/cheat/cheat/internal/config"
|
"github.com/cheat/cheat/internal/config"
|
||||||
@@ -16,45 +17,78 @@ func TestColorize(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// mock a sheet
|
// mock a sheet
|
||||||
|
original := "echo 'foo'"
|
||||||
s := Sheet{
|
s := Sheet{
|
||||||
Text: "echo 'foo'",
|
Text: original,
|
||||||
}
|
}
|
||||||
|
|
||||||
// colorize the sheet text
|
// colorize the sheet text
|
||||||
s.Colorize(conf)
|
s.Colorize(conf)
|
||||||
|
|
||||||
// initialize expectations
|
// assert that the text was modified (colorization applied)
|
||||||
want := "[38;2;181;137;0mecho[0m[38;2;147;161;161m"
|
if s.Text == original {
|
||||||
want += " [0m[38;2;42;161;152m'foo'[0m"
|
t.Error("Colorize did not modify sheet text")
|
||||||
|
}
|
||||||
|
|
||||||
// assert
|
// assert that ANSI escape codes are present
|
||||||
if s.Text != want {
|
if !strings.Contains(s.Text, "\x1b[") && !strings.Contains(s.Text, "[0m") {
|
||||||
t.Errorf("failed to colorize sheet: want: %s, got: %s", want, s.Text)
|
t.Errorf("colorized text does not contain ANSI escape codes: %q", s.Text)
|
||||||
|
}
|
||||||
|
|
||||||
|
// assert that the original content is still present within the colorized output
|
||||||
|
if !strings.Contains(s.Text, "echo") || !strings.Contains(s.Text, "foo") {
|
||||||
|
t.Errorf("colorized text lost original content: %q", s.Text)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TestColorizeError tests the error handling in Colorize
|
// TestColorizeDefaultSyntax asserts that when no syntax is specified, the
|
||||||
func TestColorizeError(_ *testing.T) {
|
// default ("bash") is used and produces the same output as an explicit "bash"
|
||||||
// Create a sheet with content
|
func TestColorizeDefaultSyntax(t *testing.T) {
|
||||||
sheet := Sheet{
|
|
||||||
Text: "some text",
|
|
||||||
Syntax: "invalidlexer12345", // Use an invalid lexer that might cause issues
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create a config with invalid formatter/style
|
|
||||||
conf := config.Config{
|
conf := config.Config{
|
||||||
Formatter: "invalidformatter",
|
Formatter: "terminal16m",
|
||||||
Style: "invalidstyle",
|
Style: "monokai",
|
||||||
}
|
}
|
||||||
|
|
||||||
// Store original text
|
// use bash-specific content that tokenizes differently across lexers
|
||||||
originalText := sheet.Text
|
code := "if [[ -f /etc/passwd ]]; then\n echo \"found\" | grep -o found\nfi"
|
||||||
|
|
||||||
// Colorize should not panic even with invalid settings
|
// colorize with empty syntax (should default to "bash")
|
||||||
sheet.Colorize(conf)
|
noSyntax := Sheet{Text: code}
|
||||||
|
noSyntax.Colorize(conf)
|
||||||
|
|
||||||
// The text might be unchanged if there was an error, or it might be colorized
|
// colorize with explicit "bash" syntax
|
||||||
// We're mainly testing that it doesn't panic
|
bashSyntax := Sheet{Text: code, Syntax: "bash"}
|
||||||
_ = sheet.Text
|
bashSyntax.Colorize(conf)
|
||||||
_ = originalText
|
|
||||||
|
// both should produce the same output
|
||||||
|
if noSyntax.Text != bashSyntax.Text {
|
||||||
|
t.Errorf(
|
||||||
|
"default syntax does not match explicit bash:\ndefault: %q\nexplicit: %q",
|
||||||
|
noSyntax.Text,
|
||||||
|
bashSyntax.Text,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestColorizeExplicitSyntax asserts that a specified syntax is used
|
||||||
|
func TestColorizeExplicitSyntax(t *testing.T) {
|
||||||
|
|
||||||
|
conf := config.Config{
|
||||||
|
Formatter: "terminal16m",
|
||||||
|
Style: "monokai",
|
||||||
|
}
|
||||||
|
|
||||||
|
// colorize as bash
|
||||||
|
bashSheet := Sheet{Text: "def hello():\n pass", Syntax: "bash"}
|
||||||
|
bashSheet.Colorize(conf)
|
||||||
|
|
||||||
|
// colorize as python
|
||||||
|
pySheet := Sheet{Text: "def hello():\n pass", Syntax: "python"}
|
||||||
|
pySheet.Colorize(conf)
|
||||||
|
|
||||||
|
// different lexers should produce different output for Python code
|
||||||
|
if bashSheet.Text == pySheet.Text {
|
||||||
|
t.Error("bash and python syntax produced identical output")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,15 +10,12 @@ import (
|
|||||||
// TestCopyErrors tests error cases for the Copy method
|
// TestCopyErrors tests error cases for the Copy method
|
||||||
func TestCopyErrors(t *testing.T) {
|
func TestCopyErrors(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
setup func() (*Sheet, string, func())
|
setup func() (*Sheet, string, func())
|
||||||
wantErr bool
|
|
||||||
errMsg string
|
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "source file does not exist",
|
name: "source file does not exist",
|
||||||
setup: func() (*Sheet, string, func()) {
|
setup: func() (*Sheet, string, func()) {
|
||||||
// Create a sheet with non-existent path
|
|
||||||
sheet := &Sheet{
|
sheet := &Sheet{
|
||||||
Title: "test",
|
Title: "test",
|
||||||
Path: "/non/existent/file.txt",
|
Path: "/non/existent/file.txt",
|
||||||
@@ -30,13 +27,10 @@ func TestCopyErrors(t *testing.T) {
|
|||||||
}
|
}
|
||||||
return sheet, dest, cleanup
|
return sheet, dest, cleanup
|
||||||
},
|
},
|
||||||
wantErr: true,
|
|
||||||
errMsg: "failed to open cheatsheet",
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "destination directory creation fails",
|
name: "destination directory creation fails",
|
||||||
setup: func() (*Sheet, string, func()) {
|
setup: func() (*Sheet, string, func()) {
|
||||||
// Create a source file
|
|
||||||
src, err := os.CreateTemp("", "copy-test-src-*")
|
src, err := os.CreateTemp("", "copy-test-src-*")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("failed to create temp file: %v", err)
|
t.Fatalf("failed to create temp file: %v", err)
|
||||||
@@ -50,13 +44,11 @@ func TestCopyErrors(t *testing.T) {
|
|||||||
CheatPath: "test",
|
CheatPath: "test",
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create a file where we want a directory
|
|
||||||
blockerFile := filepath.Join(os.TempDir(), "copy-blocker-file")
|
blockerFile := filepath.Join(os.TempDir(), "copy-blocker-file")
|
||||||
if err := os.WriteFile(blockerFile, []byte("blocker"), 0644); err != nil {
|
if err := os.WriteFile(blockerFile, []byte("blocker"), 0644); err != nil {
|
||||||
t.Fatalf("failed to create blocker file: %v", err)
|
t.Fatalf("failed to create blocker file: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Try to create dest under the blocker file (will fail)
|
|
||||||
dest := filepath.Join(blockerFile, "subdir", "dest.txt")
|
dest := filepath.Join(blockerFile, "subdir", "dest.txt")
|
||||||
|
|
||||||
cleanup := func() {
|
cleanup := func() {
|
||||||
@@ -65,13 +57,10 @@ func TestCopyErrors(t *testing.T) {
|
|||||||
}
|
}
|
||||||
return sheet, dest, cleanup
|
return sheet, dest, cleanup
|
||||||
},
|
},
|
||||||
wantErr: true,
|
|
||||||
errMsg: "failed to create directory",
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "destination file creation fails",
|
name: "destination file creation fails",
|
||||||
setup: func() (*Sheet, string, func()) {
|
setup: func() (*Sheet, string, func()) {
|
||||||
// Create a source file
|
|
||||||
src, err := os.CreateTemp("", "copy-test-src-*")
|
src, err := os.CreateTemp("", "copy-test-src-*")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("failed to create temp file: %v", err)
|
t.Fatalf("failed to create temp file: %v", err)
|
||||||
@@ -85,7 +74,6 @@ func TestCopyErrors(t *testing.T) {
|
|||||||
CheatPath: "test",
|
CheatPath: "test",
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create a directory where we want the file
|
|
||||||
destDir := filepath.Join(os.TempDir(), "copy-test-dir")
|
destDir := filepath.Join(os.TempDir(), "copy-test-dir")
|
||||||
if err := os.Mkdir(destDir, 0755); err != nil && !os.IsExist(err) {
|
if err := os.Mkdir(destDir, 0755); err != nil && !os.IsExist(err) {
|
||||||
t.Fatalf("failed to create dest dir: %v", err)
|
t.Fatalf("failed to create dest dir: %v", err)
|
||||||
@@ -97,8 +85,6 @@ func TestCopyErrors(t *testing.T) {
|
|||||||
}
|
}
|
||||||
return sheet, destDir, cleanup
|
return sheet, destDir, cleanup
|
||||||
},
|
},
|
||||||
wantErr: true,
|
|
||||||
errMsg: "failed to create outfile",
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -108,43 +94,27 @@ func TestCopyErrors(t *testing.T) {
|
|||||||
defer cleanup()
|
defer cleanup()
|
||||||
|
|
||||||
err := sheet.Copy(dest)
|
err := sheet.Copy(dest)
|
||||||
if (err != nil) != tt.wantErr {
|
if err == nil {
|
||||||
t.Errorf("Copy() error = %v, wantErr %v", err, tt.wantErr)
|
t.Error("Copy() expected error, got nil")
|
||||||
return
|
|
||||||
}
|
|
||||||
if err != nil && tt.errMsg != "" {
|
|
||||||
if !contains(err.Error(), tt.errMsg) {
|
|
||||||
t.Errorf("Copy() error = %v, want error containing %q", err, tt.errMsg)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TestCopyIOError tests the io.Copy error case
|
// TestCopyUnreadableSource verifies that Copy returns an error when the source
|
||||||
func TestCopyIOError(t *testing.T) {
|
// file cannot be opened (e.g., permission denied).
|
||||||
// This is difficult to test without mocking io.Copy
|
func TestCopyUnreadableSource(t *testing.T) {
|
||||||
// The error case would occur if the source file is modified
|
|
||||||
// or removed after opening but before copying
|
|
||||||
t.Skip("Skipping io.Copy error test - requires file system race condition")
|
|
||||||
}
|
|
||||||
|
|
||||||
// TestCopyCleanupOnError verifies that partially written files are cleaned up on error
|
|
||||||
func TestCopyCleanupOnError(t *testing.T) {
|
|
||||||
if runtime.GOOS == "windows" {
|
if runtime.GOOS == "windows" {
|
||||||
t.Skip("chmod does not restrict reads on Windows")
|
t.Skip("chmod does not restrict reads on Windows")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create a source file that we'll make unreadable after opening
|
src, err := os.CreateTemp("", "copy-test-unreadable-*")
|
||||||
src, err := os.CreateTemp("", "copy-test-cleanup-*")
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("failed to create temp file: %v", err)
|
t.Fatalf("failed to create temp file: %v", err)
|
||||||
}
|
}
|
||||||
defer os.Remove(src.Name())
|
defer os.Remove(src.Name())
|
||||||
|
|
||||||
// Write some content
|
if _, err := src.WriteString("test content"); err != nil {
|
||||||
content := "test content for cleanup"
|
|
||||||
if _, err := src.WriteString(content); err != nil {
|
|
||||||
t.Fatalf("failed to write content: %v", err)
|
t.Fatalf("failed to write content: %v", err)
|
||||||
}
|
}
|
||||||
src.Close()
|
src.Close()
|
||||||
@@ -155,38 +125,21 @@ func TestCopyCleanupOnError(t *testing.T) {
|
|||||||
CheatPath: "test",
|
CheatPath: "test",
|
||||||
}
|
}
|
||||||
|
|
||||||
// Destination path
|
dest := filepath.Join(os.TempDir(), "copy-unreadable-test.txt")
|
||||||
dest := filepath.Join(os.TempDir(), "copy-cleanup-test.txt")
|
defer os.Remove(dest)
|
||||||
defer os.Remove(dest) // Clean up if test fails
|
|
||||||
|
|
||||||
// Make the source file unreadable (simulating a read error during copy)
|
|
||||||
// This is platform-specific, but should work on Unix-like systems
|
|
||||||
if err := os.Chmod(src.Name(), 0000); err != nil {
|
if err := os.Chmod(src.Name(), 0000); err != nil {
|
||||||
t.Skip("Cannot change file permissions on this platform")
|
t.Skip("Cannot change file permissions on this platform")
|
||||||
}
|
}
|
||||||
defer os.Chmod(src.Name(), 0644) // Restore permissions for cleanup
|
defer os.Chmod(src.Name(), 0644)
|
||||||
|
|
||||||
// Attempt to copy - this should fail during io.Copy
|
|
||||||
err = sheet.Copy(dest)
|
err = sheet.Copy(dest)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Error("Expected Copy to fail with permission error")
|
t.Error("expected Copy to fail with permission error")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Verify the destination file was cleaned up
|
// Destination should not exist since the error occurs before it is created
|
||||||
if _, err := os.Stat(dest); !os.IsNotExist(err) {
|
if _, err := os.Stat(dest); !os.IsNotExist(err) {
|
||||||
t.Error("Destination file should have been removed after copy failure")
|
t.Error("destination file should not exist after open failure")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func contains(s, substr string) bool {
|
|
||||||
return len(s) >= len(substr) && (s == substr || len(s) > 0 && containsHelper(s, substr))
|
|
||||||
}
|
|
||||||
|
|
||||||
func containsHelper(s, substr string) bool {
|
|
||||||
for i := 0; i <= len(s)-len(substr); i++ {
|
|
||||||
if s[i:i+len(substr)] == substr {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,65 +0,0 @@
|
|||||||
// Package sheet provides functionality for parsing and managing individual cheat sheets.
|
|
||||||
//
|
|
||||||
// A sheet represents a single cheatsheet file containing helpful commands, notes,
|
|
||||||
// or documentation. Sheets can include optional YAML frontmatter for metadata
|
|
||||||
// such as tags and syntax highlighting preferences.
|
|
||||||
//
|
|
||||||
// # Sheet Format
|
|
||||||
//
|
|
||||||
// Sheets are plain text files that may begin with YAML frontmatter:
|
|
||||||
//
|
|
||||||
// ---
|
|
||||||
// syntax: bash
|
|
||||||
// tags: [networking, linux, ssh]
|
|
||||||
// ---
|
|
||||||
// # Connect to remote server
|
|
||||||
// ssh user@hostname
|
|
||||||
//
|
|
||||||
// # Copy files over SSH
|
|
||||||
// scp local_file user@hostname:/remote/path
|
|
||||||
//
|
|
||||||
// The frontmatter is optional. If omitted, the sheet will use default values.
|
|
||||||
//
|
|
||||||
// # Core Types
|
|
||||||
//
|
|
||||||
// The Sheet type contains:
|
|
||||||
// - Title: The sheet's name (derived from filename)
|
|
||||||
// - Path: Full filesystem path to the sheet
|
|
||||||
// - Text: The content of the sheet (without frontmatter)
|
|
||||||
// - Tags: Categories assigned to the sheet
|
|
||||||
// - Syntax: Language hint for syntax highlighting
|
|
||||||
// - ReadOnly: Whether the sheet can be modified
|
|
||||||
//
|
|
||||||
// Key Functions
|
|
||||||
//
|
|
||||||
// - New: Creates a new Sheet from a file path
|
|
||||||
// - Parse: Extracts frontmatter and content from sheet text
|
|
||||||
// - Search: Searches sheet content using regular expressions
|
|
||||||
// - Colorize: Applies syntax highlighting to sheet content
|
|
||||||
//
|
|
||||||
// # Syntax Highlighting
|
|
||||||
//
|
|
||||||
// The package integrates with the Chroma library to provide syntax highlighting.
|
|
||||||
// Supported languages include bash, python, go, javascript, and many others.
|
|
||||||
// The syntax can be specified in the frontmatter or auto-detected.
|
|
||||||
//
|
|
||||||
// Example Usage
|
|
||||||
//
|
|
||||||
// // Load a sheet from disk
|
|
||||||
// s, err := sheet.New("/path/to/sheet", []string{"personal"}, false)
|
|
||||||
// if err != nil {
|
|
||||||
// log.Fatal(err)
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// // Search for content
|
|
||||||
// matches, err := s.Search("ssh", false)
|
|
||||||
// if err != nil {
|
|
||||||
// log.Fatal(err)
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// // Apply syntax highlighting
|
|
||||||
// colorized, err := s.Colorize(config)
|
|
||||||
// if err != nil {
|
|
||||||
// log.Fatal(err)
|
|
||||||
// }
|
|
||||||
package sheet
|
|
||||||
@@ -27,22 +27,3 @@ func TestParseWindowsLineEndings(t *testing.T) {
|
|||||||
t.Errorf("failed to parse syntax: want: %s, got: %s", want, fm.Syntax)
|
t.Errorf("failed to parse syntax: want: %s, got: %s", want, fm.Syntax)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TestParseInvalidYAML tests parsing with invalid YAML in frontmatter
|
|
||||||
func TestParseInvalidYAML(t *testing.T) {
|
|
||||||
// stub our cheatsheet content with invalid YAML
|
|
||||||
markdown := `---
|
|
||||||
syntax: go
|
|
||||||
tags: [ test
|
|
||||||
unclosed bracket
|
|
||||||
---
|
|
||||||
To foo the bar: baz`
|
|
||||||
|
|
||||||
// parse the frontmatter
|
|
||||||
_, _, err := parse(markdown)
|
|
||||||
|
|
||||||
// assert that an error was returned for invalid YAML
|
|
||||||
if err == nil {
|
|
||||||
t.Error("expected error for invalid YAML, got nil")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -38,7 +38,7 @@ To foo the bar: baz`
|
|||||||
t.Errorf("failed to parse tags: want: %s, got: %s", want, fm.Tags[0])
|
t.Errorf("failed to parse tags: want: %s, got: %s", want, fm.Tags[0])
|
||||||
}
|
}
|
||||||
if len(fm.Tags) != 1 {
|
if len(fm.Tags) != 1 {
|
||||||
t.Errorf("failed to parse tags: want: len 0, got: len %d", len(fm.Tags))
|
t.Errorf("failed to parse tags: want: len 1, got: len %d", len(fm.Tags))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -93,3 +93,20 @@ To foo the bar: baz`
|
|||||||
t.Errorf("failed to parse text: want: %s, got: %s", markdown, text)
|
t.Errorf("failed to parse text: want: %s, got: %s", markdown, text)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TestHasMalformedYAML asserts that an error is returned when the frontmatter
|
||||||
|
// contains invalid YAML that cannot be unmarshalled
|
||||||
|
func TestHasMalformedYAML(t *testing.T) {
|
||||||
|
|
||||||
|
// stub cheatsheet content with syntactically invalid YAML between the
|
||||||
|
// delimiters (a bare tab character followed by unquoted colon)
|
||||||
|
markdown := "---\n\t:\t:\n---\nBody text here"
|
||||||
|
|
||||||
|
// parse the frontmatter
|
||||||
|
_, _, err := parse(markdown)
|
||||||
|
|
||||||
|
// assert that an error was returned due to YAML unmarshal failure
|
||||||
|
if err == nil {
|
||||||
|
t.Error("failed to error on malformed YAML frontmatter")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -122,69 +122,3 @@ func FuzzSearchRegex(f *testing.F) {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// FuzzSearchCatastrophicBacktracking specifically tests for regex patterns
|
|
||||||
// that could cause performance issues
|
|
||||||
func FuzzSearchCatastrophicBacktracking(f *testing.F) {
|
|
||||||
// Seed with patterns known to potentially cause issues
|
|
||||||
f.Add("a", 10, 5)
|
|
||||||
f.Add("x", 20, 3)
|
|
||||||
|
|
||||||
f.Fuzz(func(t *testing.T, char string, repeats int, groups int) {
|
|
||||||
// Limit the size to avoid memory issues in the test
|
|
||||||
if repeats > 30 || repeats < 0 || groups > 10 || groups < 0 || len(char) > 5 {
|
|
||||||
t.Skip("Skipping invalid or overly large test case")
|
|
||||||
}
|
|
||||||
|
|
||||||
// Construct patterns that might cause backtracking
|
|
||||||
patterns := []string{
|
|
||||||
strings.Repeat(char, repeats),
|
|
||||||
"(" + char + "+)+",
|
|
||||||
"(" + char + "*)*",
|
|
||||||
"(" + char + "|" + char + ")*",
|
|
||||||
}
|
|
||||||
|
|
||||||
// Add nested groups
|
|
||||||
if groups > 0 && groups < 10 {
|
|
||||||
nested := char
|
|
||||||
for i := 0; i < groups; i++ {
|
|
||||||
nested = "(" + nested + ")+"
|
|
||||||
}
|
|
||||||
patterns = append(patterns, nested)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Test text that might trigger backtracking
|
|
||||||
testText := strings.Repeat(char, repeats) + "x"
|
|
||||||
|
|
||||||
for _, pattern := range patterns {
|
|
||||||
// Try to compile the pattern
|
|
||||||
reg, err := regexp.Compile(pattern)
|
|
||||||
if err != nil {
|
|
||||||
// Invalid pattern, skip
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
|
|
||||||
// Test with timeout
|
|
||||||
done := make(chan bool, 1)
|
|
||||||
|
|
||||||
go func() {
|
|
||||||
defer func() {
|
|
||||||
if r := recover(); r != nil {
|
|
||||||
t.Errorf("Search panicked with backtracking pattern %q: %v", pattern, r)
|
|
||||||
}
|
|
||||||
done <- true
|
|
||||||
}()
|
|
||||||
|
|
||||||
sheet := Sheet{Text: testText}
|
|
||||||
_ = sheet.Search(reg)
|
|
||||||
}()
|
|
||||||
|
|
||||||
select {
|
|
||||||
case <-done:
|
|
||||||
// Completed successfully
|
|
||||||
case <-time.After(50 * time.Millisecond):
|
|
||||||
t.Logf("Warning: potential backtracking issue with pattern %q (completed slowly)", pattern)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ import (
|
|||||||
"reflect"
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/cheat/cheat/internal/mock"
|
"github.com/cheat/cheat/mocks"
|
||||||
)
|
)
|
||||||
|
|
||||||
// TestSheetSuccess asserts that sheets initialize properly
|
// TestSheetSuccess asserts that sheets initialize properly
|
||||||
@@ -14,7 +14,7 @@ func TestSheetSuccess(t *testing.T) {
|
|||||||
sheet, err := New(
|
sheet, err := New(
|
||||||
"foo",
|
"foo",
|
||||||
"community",
|
"community",
|
||||||
mock.Path("sheet/foo"),
|
mocks.Path("sheet/foo"),
|
||||||
[]string{"alpha", "bravo"},
|
[]string{"alpha", "bravo"},
|
||||||
false,
|
false,
|
||||||
)
|
)
|
||||||
@@ -27,10 +27,10 @@ func TestSheetSuccess(t *testing.T) {
|
|||||||
t.Errorf("failed to init title: want: foo, got: %s", sheet.Title)
|
t.Errorf("failed to init title: want: foo, got: %s", sheet.Title)
|
||||||
}
|
}
|
||||||
|
|
||||||
if sheet.Path != mock.Path("sheet/foo") {
|
if sheet.Path != mocks.Path("sheet/foo") {
|
||||||
t.Errorf(
|
t.Errorf(
|
||||||
"failed to init path: want: %s, got: %s",
|
"failed to init path: want: %s, got: %s",
|
||||||
mock.Path("sheet/foo"),
|
mocks.Path("sheet/foo"),
|
||||||
sheet.Path,
|
sheet.Path,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
@@ -63,7 +63,7 @@ func TestSheetFailure(t *testing.T) {
|
|||||||
_, err := New(
|
_, err := New(
|
||||||
"foo",
|
"foo",
|
||||||
"community",
|
"community",
|
||||||
mock.Path("/does-not-exist"),
|
mocks.Path("/does-not-exist"),
|
||||||
[]string{"alpha", "bravo"},
|
[]string{"alpha", "bravo"},
|
||||||
false,
|
false,
|
||||||
)
|
)
|
||||||
@@ -80,7 +80,7 @@ func TestSheetFrontMatterFailure(t *testing.T) {
|
|||||||
_, err := New(
|
_, err := New(
|
||||||
"foo",
|
"foo",
|
||||||
"community",
|
"community",
|
||||||
mock.Path("sheet/bad-fm"),
|
mocks.Path("sheet/bad-fm"),
|
||||||
[]string{"alpha", "bravo"},
|
[]string{"alpha", "bravo"},
|
||||||
false,
|
false,
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -1,15 +1,8 @@
|
|||||||
package sheet
|
package sheet
|
||||||
|
|
||||||
|
import "slices"
|
||||||
|
|
||||||
// Tagged returns true if a sheet was tagged with `needle`
|
// Tagged returns true if a sheet was tagged with `needle`
|
||||||
func (s *Sheet) Tagged(needle string) bool {
|
func (s *Sheet) Tagged(needle string) bool {
|
||||||
|
return slices.Contains(s.Tags, needle)
|
||||||
// if any of the tags match `needle`, return `true`
|
|
||||||
for _, tag := range s.Tags {
|
|
||||||
if tag == needle {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// otherwise, return `false`
|
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
|
|||||||
40
internal/sheet/validate.go
Normal file
40
internal/sheet/validate.go
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
package sheet
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Validate ensures that a cheatsheet name does not contain
|
||||||
|
// directory traversal sequences or other potentially dangerous patterns.
|
||||||
|
func Validate(name string) error {
|
||||||
|
// Reject empty names
|
||||||
|
if name == "" {
|
||||||
|
return fmt.Errorf("cheatsheet name cannot be empty")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Reject names containing directory traversal
|
||||||
|
if strings.Contains(name, "..") {
|
||||||
|
return fmt.Errorf("cheatsheet name cannot contain '..'")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Reject absolute paths
|
||||||
|
if filepath.IsAbs(name) {
|
||||||
|
return fmt.Errorf("cheatsheet name cannot be an absolute path")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Reject names that start with ~ (home directory expansion)
|
||||||
|
if strings.HasPrefix(name, "~") {
|
||||||
|
return fmt.Errorf("cheatsheet name cannot start with '~'")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Reject hidden files (files that start with a dot)
|
||||||
|
// We don't display hidden files, so we shouldn't create them
|
||||||
|
filename := filepath.Base(name)
|
||||||
|
if strings.HasPrefix(filename, ".") {
|
||||||
|
return fmt.Errorf("cheatsheet name cannot start with '.' (hidden files are not supported)")
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package cheatpath
|
package sheet
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"strings"
|
"strings"
|
||||||
@@ -6,9 +6,9 @@ import (
|
|||||||
"unicode/utf8"
|
"unicode/utf8"
|
||||||
)
|
)
|
||||||
|
|
||||||
// FuzzValidateSheetName tests the ValidateSheetName function with fuzzing
|
// FuzzValidate tests the Validate function with fuzzing
|
||||||
// to ensure it properly prevents path traversal and other security issues
|
// to ensure it properly prevents path traversal and other security issues
|
||||||
func FuzzValidateSheetName(f *testing.F) {
|
func FuzzValidate(f *testing.F) {
|
||||||
// Add seed corpus with various valid and malicious inputs
|
// Add seed corpus with various valid and malicious inputs
|
||||||
// Valid names
|
// Valid names
|
||||||
f.Add("docker")
|
f.Add("docker")
|
||||||
@@ -84,11 +84,11 @@ func FuzzValidateSheetName(f *testing.F) {
|
|||||||
func() {
|
func() {
|
||||||
defer func() {
|
defer func() {
|
||||||
if r := recover(); r != nil {
|
if r := recover(); r != nil {
|
||||||
t.Errorf("ValidateSheetName panicked with input %q: %v", input, r)
|
t.Errorf("Validate panicked with input %q: %v", input, r)
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
err := ValidateSheetName(input)
|
err := Validate(input)
|
||||||
|
|
||||||
// Security invariants that must always hold
|
// Security invariants that must always hold
|
||||||
if err == nil {
|
if err == nil {
|
||||||
@@ -129,8 +129,8 @@ func FuzzValidateSheetName(f *testing.F) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// FuzzValidateSheetNamePathTraversal specifically targets path traversal bypasses
|
// FuzzValidatePathTraversal specifically targets path traversal bypasses
|
||||||
func FuzzValidateSheetNamePathTraversal(f *testing.F) {
|
func FuzzValidatePathTraversal(f *testing.F) {
|
||||||
// Seed corpus focusing on path traversal variations
|
// Seed corpus focusing on path traversal variations
|
||||||
f.Add("..", "/", "")
|
f.Add("..", "/", "")
|
||||||
f.Add("", "..", "/")
|
f.Add("", "..", "/")
|
||||||
@@ -153,11 +153,11 @@ func FuzzValidateSheetNamePathTraversal(f *testing.F) {
|
|||||||
func() {
|
func() {
|
||||||
defer func() {
|
defer func() {
|
||||||
if r := recover(); r != nil {
|
if r := recover(); r != nil {
|
||||||
t.Errorf("ValidateSheetName panicked with constructed input %q: %v", input, r)
|
t.Errorf("Validate panicked with constructed input %q: %v", input, r)
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
err := ValidateSheetName(input)
|
err := Validate(input)
|
||||||
|
|
||||||
// If the input contains literal "..", it must be rejected
|
// If the input contains literal "..", it must be rejected
|
||||||
if strings.Contains(input, "..") && err == nil {
|
if strings.Contains(input, "..") && err == nil {
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package cheatpath
|
package sheet
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"runtime"
|
"runtime"
|
||||||
@@ -6,7 +6,7 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestValidateSheetName(t *testing.T) {
|
func TestValidate(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
input string
|
input string
|
||||||
@@ -98,14 +98,14 @@ func TestValidateSheetName(t *testing.T) {
|
|||||||
|
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
err := ValidateSheetName(tt.input)
|
err := Validate(tt.input)
|
||||||
if (err != nil) != tt.wantErr {
|
if (err != nil) != tt.wantErr {
|
||||||
t.Errorf("ValidateName(%q) error = %v, wantErr %v", tt.input, err, tt.wantErr)
|
t.Errorf("Validate(%q) error = %v, wantErr %v", tt.input, err, tt.wantErr)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if err != nil && tt.errMsg != "" {
|
if err != nil && tt.errMsg != "" {
|
||||||
if !strings.Contains(err.Error(), tt.errMsg) {
|
if !strings.Contains(err.Error(), tt.errMsg) {
|
||||||
t.Errorf("ValidateName(%q) error = %v, want error containing %q", tt.input, err, tt.errMsg)
|
t.Errorf("Validate(%q) error = %v, want error containing %q", tt.input, err, tt.errMsg)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
@@ -1,65 +0,0 @@
|
|||||||
// Package sheets manages collections of cheat sheets across multiple cheatpaths.
|
|
||||||
//
|
|
||||||
// The sheets package provides functionality to:
|
|
||||||
// - Load sheets from multiple cheatpaths
|
|
||||||
// - Consolidate duplicate sheets (with precedence rules)
|
|
||||||
// - Filter sheets by tags
|
|
||||||
// - Sort sheets alphabetically
|
|
||||||
// - Extract unique tags across all sheets
|
|
||||||
//
|
|
||||||
// # Loading Sheets
|
|
||||||
//
|
|
||||||
// Sheets are loaded recursively from cheatpath directories, excluding:
|
|
||||||
// - Hidden files (starting with .)
|
|
||||||
// - Files in .git directories
|
|
||||||
// - Files with extensions (sheets have no extension)
|
|
||||||
//
|
|
||||||
// # Consolidation
|
|
||||||
//
|
|
||||||
// When multiple cheatpaths contain sheets with the same name, consolidation
|
|
||||||
// rules apply based on the order of cheatpaths. Sheets from earlier paths
|
|
||||||
// override those from later paths, allowing personal sheets to override
|
|
||||||
// community sheets.
|
|
||||||
//
|
|
||||||
// Example:
|
|
||||||
//
|
|
||||||
// cheatpaths:
|
|
||||||
// 1. personal: ~/cheat
|
|
||||||
// 2. community: ~/cheat/community
|
|
||||||
//
|
|
||||||
// If both contain "git", the version from "personal" is used.
|
|
||||||
//
|
|
||||||
// # Filtering
|
|
||||||
//
|
|
||||||
// Sheets can be filtered by:
|
|
||||||
// - Tags: Include only sheets with specific tags
|
|
||||||
// - Cheatpath: Include only sheets from specific paths
|
|
||||||
//
|
|
||||||
// Key Functions
|
|
||||||
//
|
|
||||||
// - Load: Loads all sheets from the given cheatpaths
|
|
||||||
// - Filter: Filters sheets by tag
|
|
||||||
// - Consolidate: Merges sheets from multiple paths with precedence
|
|
||||||
// - Sort: Sorts sheets alphabetically by title
|
|
||||||
// - Tags: Extracts all unique tags from sheets
|
|
||||||
//
|
|
||||||
// Example Usage
|
|
||||||
//
|
|
||||||
// // Load sheets from all cheatpaths
|
|
||||||
// allSheets, err := sheets.Load(cheatpaths)
|
|
||||||
// if err != nil {
|
|
||||||
// log.Fatal(err)
|
|
||||||
// }
|
|
||||||
//
|
|
||||||
// // Consolidate to handle duplicates
|
|
||||||
// consolidated := sheets.Consolidate(allSheets)
|
|
||||||
//
|
|
||||||
// // Filter by tag
|
|
||||||
// filtered := sheets.Filter(consolidated, "networking")
|
|
||||||
//
|
|
||||||
// // Sort alphabetically
|
|
||||||
// sheets.Sort(filtered)
|
|
||||||
//
|
|
||||||
// // Get all unique tags
|
|
||||||
// tags := sheets.Tags(consolidated)
|
|
||||||
package sheets
|
|
||||||
@@ -18,28 +18,26 @@ func TestFilterSingleTag(t *testing.T) {
|
|||||||
|
|
||||||
map[string]sheet.Sheet{
|
map[string]sheet.Sheet{
|
||||||
"foo": sheet.Sheet{Title: "foo", Tags: []string{"alpha", "bravo"}},
|
"foo": sheet.Sheet{Title: "foo", Tags: []string{"alpha", "bravo"}},
|
||||||
"bar": sheet.Sheet{Title: "bar", Tags: []string{"bravo", "charlie"}},
|
"bar": sheet.Sheet{Title: "bar", Tags: []string{"charlie"}},
|
||||||
},
|
},
|
||||||
|
|
||||||
map[string]sheet.Sheet{
|
map[string]sheet.Sheet{
|
||||||
"baz": sheet.Sheet{Title: "baz", Tags: []string{"alpha", "bravo"}},
|
"baz": sheet.Sheet{Title: "baz", Tags: []string{"alpha"}},
|
||||||
"bat": sheet.Sheet{Title: "bat", Tags: []string{"bravo", "charlie"}},
|
"bat": sheet.Sheet{Title: "bat", Tags: []string{"bravo", "charlie"}},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
// filter the cheatsheets
|
// filter the cheatsheets
|
||||||
filtered := Filter(cheatpaths, []string{"bravo"})
|
filtered := Filter(cheatpaths, []string{"alpha"})
|
||||||
|
|
||||||
// assert that the expect results were returned
|
// assert that the expect results were returned
|
||||||
want := []map[string]sheet.Sheet{
|
want := []map[string]sheet.Sheet{
|
||||||
map[string]sheet.Sheet{
|
map[string]sheet.Sheet{
|
||||||
"foo": sheet.Sheet{Title: "foo", Tags: []string{"alpha", "bravo"}},
|
"foo": sheet.Sheet{Title: "foo", Tags: []string{"alpha", "bravo"}},
|
||||||
"bar": sheet.Sheet{Title: "bar", Tags: []string{"bravo", "charlie"}},
|
|
||||||
},
|
},
|
||||||
|
|
||||||
map[string]sheet.Sheet{
|
map[string]sheet.Sheet{
|
||||||
"baz": sheet.Sheet{Title: "baz", Tags: []string{"alpha", "bravo"}},
|
"baz": sheet.Sheet{Title: "baz", Tags: []string{"alpha"}},
|
||||||
"bat": sheet.Sheet{Title: "bat", Tags: []string{"bravo", "charlie"}},
|
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -8,12 +8,11 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
cp "github.com/cheat/cheat/internal/cheatpath"
|
cp "github.com/cheat/cheat/internal/cheatpath"
|
||||||
"github.com/cheat/cheat/internal/repo"
|
|
||||||
"github.com/cheat/cheat/internal/sheet"
|
"github.com/cheat/cheat/internal/sheet"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Load produces a map of cheatsheet titles to filesystem paths
|
// Load produces a map of cheatsheet titles to filesystem paths
|
||||||
func Load(cheatpaths []cp.Cheatpath) ([]map[string]sheet.Sheet, error) {
|
func Load(cheatpaths []cp.Path) ([]map[string]sheet.Sheet, error) {
|
||||||
|
|
||||||
// create a slice of maps of sheets. This structure will store all sheets
|
// create a slice of maps of sheets. This structure will store all sheets
|
||||||
// that are associated with each cheatpath.
|
// that are associated with each cheatpath.
|
||||||
@@ -27,10 +26,10 @@ func Load(cheatpaths []cp.Cheatpath) ([]map[string]sheet.Sheet, error) {
|
|||||||
|
|
||||||
// recursively iterate over the cheatpath, and load each cheatsheet
|
// recursively iterate over the cheatpath, and load each cheatsheet
|
||||||
// encountered along the way
|
// encountered along the way
|
||||||
err := filepath.Walk(
|
err := filepath.WalkDir(
|
||||||
cheatpath.Path, func(
|
cheatpath.Path, func(
|
||||||
path string,
|
path string,
|
||||||
info os.FileInfo,
|
d fs.DirEntry,
|
||||||
err error) error {
|
err error) error {
|
||||||
|
|
||||||
// fail if an error occurred while walking the directory
|
// fail if an error occurred while walking the directory
|
||||||
@@ -38,8 +37,12 @@ func Load(cheatpaths []cp.Cheatpath) ([]map[string]sheet.Sheet, error) {
|
|||||||
return fmt.Errorf("failed to walk path: %v", err)
|
return fmt.Errorf("failed to walk path: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// don't register directories as cheatsheets
|
if d.IsDir() {
|
||||||
if info.IsDir() {
|
// skip .git directories to avoid hundreds/thousands of
|
||||||
|
// needless syscalls (see repo.GitDir for full history)
|
||||||
|
if filepath.Base(path) == ".git" {
|
||||||
|
return fs.SkipDir
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -63,17 +66,6 @@ func Load(cheatpaths []cp.Cheatpath) ([]map[string]sheet.Sheet, error) {
|
|||||||
string(os.PathSeparator),
|
string(os.PathSeparator),
|
||||||
)
|
)
|
||||||
|
|
||||||
// Don't walk the `.git` directory. Doing so creates
|
|
||||||
// hundreds/thousands of needless syscalls and could
|
|
||||||
// potentially harm performance on machines with slow disks.
|
|
||||||
skip, err := repo.GitDir(path)
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("failed to identify .git directory: %v", err)
|
|
||||||
}
|
|
||||||
if skip {
|
|
||||||
return fs.SkipDir
|
|
||||||
}
|
|
||||||
|
|
||||||
// parse the cheatsheet file into a `sheet` struct
|
// parse the cheatsheet file into a `sheet` struct
|
||||||
s, err := sheet.New(
|
s, err := sheet.New(
|
||||||
title,
|
title,
|
||||||
|
|||||||
@@ -5,22 +5,22 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/cheat/cheat/internal/cheatpath"
|
"github.com/cheat/cheat/internal/cheatpath"
|
||||||
"github.com/cheat/cheat/internal/mock"
|
"github.com/cheat/cheat/mocks"
|
||||||
)
|
)
|
||||||
|
|
||||||
// TestLoad asserts that sheets on valid cheatpaths can be loaded successfully
|
// TestLoad asserts that sheets on valid cheatpaths can be loaded successfully
|
||||||
func TestLoad(t *testing.T) {
|
func TestLoad(t *testing.T) {
|
||||||
|
|
||||||
// mock cheatpaths
|
// mock cheatpaths
|
||||||
cheatpaths := []cheatpath.Cheatpath{
|
cheatpaths := []cheatpath.Path{
|
||||||
{
|
{
|
||||||
Name: "community",
|
Name: "community",
|
||||||
Path: path.Join(mock.Path("cheatsheets"), "community"),
|
Path: path.Join(mocks.Path("cheatsheets"), "community"),
|
||||||
ReadOnly: true,
|
ReadOnly: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Name: "personal",
|
Name: "personal",
|
||||||
Path: path.Join(mock.Path("cheatsheets"), "personal"),
|
Path: path.Join(mocks.Path("cheatsheets"), "personal"),
|
||||||
ReadOnly: false,
|
ReadOnly: false,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
@@ -54,7 +54,7 @@ func TestLoad(t *testing.T) {
|
|||||||
func TestLoadBadPath(t *testing.T) {
|
func TestLoadBadPath(t *testing.T) {
|
||||||
|
|
||||||
// mock a bad cheatpath
|
// mock a bad cheatpath
|
||||||
cheatpaths := []cheatpath.Cheatpath{
|
cheatpaths := []cheatpath.Path{
|
||||||
{
|
{
|
||||||
Name: "badpath",
|
Name: "badpath",
|
||||||
Path: "/cheat/test/path/does/not/exist",
|
Path: "/cheat/test/path/does/not/exist",
|
||||||
|
|||||||
@@ -32,9 +32,7 @@ func Tags(cheatpaths []map[string]sheet.Sheet) []string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// sort the slice
|
// sort the slice
|
||||||
sort.Slice(sorted, func(i, j int) bool {
|
sort.Strings(sorted)
|
||||||
return sorted[i] < sorted[j]
|
|
||||||
})
|
|
||||||
|
|
||||||
return sorted
|
return sorted
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -127,64 +127,3 @@ func FuzzTags(f *testing.F) {
|
|||||||
}()
|
}()
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// FuzzTagsStress tests Tags function with large numbers of tags
|
|
||||||
func FuzzTagsStress(f *testing.F) {
|
|
||||||
// Seed: number of unique tags, number of sheets, tags per sheet
|
|
||||||
f.Add(10, 10, 5)
|
|
||||||
f.Add(100, 50, 10)
|
|
||||||
f.Add(1000, 100, 20)
|
|
||||||
|
|
||||||
f.Fuzz(func(t *testing.T, numUniqueTags int, numSheets int, tagsPerSheet int) {
|
|
||||||
// Limit to reasonable values
|
|
||||||
if numUniqueTags > 1000 || numUniqueTags < 0 ||
|
|
||||||
numSheets > 1000 || numSheets < 0 ||
|
|
||||||
tagsPerSheet > 100 || tagsPerSheet < 0 {
|
|
||||||
t.Skip("Skipping unreasonable test case")
|
|
||||||
}
|
|
||||||
|
|
||||||
// Generate unique tags
|
|
||||||
uniqueTags := make([]string, numUniqueTags)
|
|
||||||
for i := 0; i < numUniqueTags; i++ {
|
|
||||||
uniqueTags[i] = "tag" + string(rune(i))
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create sheets with random tags
|
|
||||||
cheatpaths := []map[string]sheet.Sheet{
|
|
||||||
make(map[string]sheet.Sheet),
|
|
||||||
}
|
|
||||||
|
|
||||||
for i := 0; i < numSheets; i++ {
|
|
||||||
// Select random tags for this sheet
|
|
||||||
sheetTags := make([]string, 0, tagsPerSheet)
|
|
||||||
for j := 0; j < tagsPerSheet && j < numUniqueTags; j++ {
|
|
||||||
// Distribute tags across sheets
|
|
||||||
tagIndex := (i*tagsPerSheet + j) % numUniqueTags
|
|
||||||
sheetTags = append(sheetTags, uniqueTags[tagIndex])
|
|
||||||
}
|
|
||||||
|
|
||||||
cheatpaths[0]["sheet"+string(rune(i))] = sheet.Sheet{
|
|
||||||
Title: "sheet" + string(rune(i)),
|
|
||||||
Tags: sheetTags,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Should handle large numbers efficiently
|
|
||||||
func() {
|
|
||||||
defer func() {
|
|
||||||
if r := recover(); r != nil {
|
|
||||||
t.Errorf("Tags panicked with %d unique tags, %d sheets, %d tags/sheet: %v",
|
|
||||||
numUniqueTags, numSheets, tagsPerSheet, r)
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
result := Tags(cheatpaths)
|
|
||||||
|
|
||||||
// Should have at most numUniqueTags in result
|
|
||||||
if len(result) > numUniqueTags {
|
|
||||||
t.Errorf("More tags in result (%d) than unique tags created (%d)",
|
|
||||||
len(result), numUniqueTags)
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|||||||
23
mocks/path.go
Normal file
23
mocks/path.go
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
// Package mocks provides test fixture data and helpers for unit tests.
|
||||||
|
package mocks
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"path/filepath"
|
||||||
|
"runtime"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Path returns the absolute path to the specified mock file within
|
||||||
|
// the mocks/ directory.
|
||||||
|
func Path(filename string) string {
|
||||||
|
_, thisfile, _, _ := runtime.Caller(0)
|
||||||
|
|
||||||
|
file, err := filepath.Abs(
|
||||||
|
filepath.Join(filepath.Dir(thisfile), filename),
|
||||||
|
)
|
||||||
|
if err != nil {
|
||||||
|
panic(fmt.Errorf("failed to resolve mock path: %v", err))
|
||||||
|
}
|
||||||
|
|
||||||
|
return file
|
||||||
|
}
|
||||||
@@ -1,74 +0,0 @@
|
|||||||
# cheat(1) completion -*- shell-script -*-
|
|
||||||
|
|
||||||
# generate cheatsheet completions, optionally using `fzf`
|
|
||||||
_cheat_complete_cheatsheets()
|
|
||||||
{
|
|
||||||
if [[ "$CHEAT_USE_FZF" = true ]]; then
|
|
||||||
FZF_COMPLETION_TRIGGER='' _fzf_complete "--no-multi" "$@" < <(
|
|
||||||
cheat -l | tail -n +2 | cut -d' ' -f1
|
|
||||||
)
|
|
||||||
else
|
|
||||||
COMPREPLY=( $(compgen -W "$(cheat -l | tail -n +2 | cut -d' ' -f1)" -- "$cur") )
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
|
|
||||||
# generate tag completions, optionally using `fzf`
|
|
||||||
_cheat_complete_tags()
|
|
||||||
{
|
|
||||||
if [ "$CHEAT_USE_FZF" = true ]; then
|
|
||||||
FZF_COMPLETION_TRIGGER='' _fzf_complete "--no-multi" "$@" < <(cheat -T)
|
|
||||||
else
|
|
||||||
COMPREPLY=( $(compgen -W "$(cheat -T)" -- "$cur") )
|
|
||||||
fi
|
|
||||||
}
|
|
||||||
|
|
||||||
# implement the `cheat` autocompletions
|
|
||||||
_cheat()
|
|
||||||
{
|
|
||||||
local cur prev words cword split
|
|
||||||
_init_completion -s || return
|
|
||||||
|
|
||||||
# complete options that are currently being typed: `--col` => `--colorize`
|
|
||||||
if [[ $cur == -* ]]; then
|
|
||||||
COMPREPLY=( $(compgen -W '$(_parse_help "$1" | sed "s/=//g")' -- "$cur") )
|
|
||||||
[[ $COMPREPLY == *= ]] && compopt -o nospace
|
|
||||||
return
|
|
||||||
fi
|
|
||||||
|
|
||||||
# implement completions
|
|
||||||
case $prev in
|
|
||||||
--colorize|-c|\
|
|
||||||
--directories|-d|\
|
|
||||||
--init|\
|
|
||||||
--regex|-r|\
|
|
||||||
--search|-s|\
|
|
||||||
--tags|-T|\
|
|
||||||
--version|-v)
|
|
||||||
# noop the above, which should implement no completions
|
|
||||||
;;
|
|
||||||
--edit|-e)
|
|
||||||
_cheat_complete_cheatsheets
|
|
||||||
;;
|
|
||||||
--list|-l)
|
|
||||||
_cheat_complete_cheatsheets
|
|
||||||
;;
|
|
||||||
--path|-p)
|
|
||||||
COMPREPLY=( $(compgen -W "$(cheat -d | cut -d':' -f1)" -- "$cur") )
|
|
||||||
;;
|
|
||||||
--rm)
|
|
||||||
_cheat_complete_cheatsheets
|
|
||||||
;;
|
|
||||||
--tag|-t)
|
|
||||||
_cheat_complete_tags
|
|
||||||
;;
|
|
||||||
*)
|
|
||||||
_cheat_complete_cheatsheets
|
|
||||||
;;
|
|
||||||
esac
|
|
||||||
|
|
||||||
$split && return
|
|
||||||
|
|
||||||
} &&
|
|
||||||
complete -F _cheat cheat
|
|
||||||
|
|
||||||
# ex: filetype=sh
|
|
||||||
@@ -1,13 +0,0 @@
|
|||||||
complete -c cheat -f -a "(cheat -l | tail -n +2 | cut -d ' ' -f 1)"
|
|
||||||
complete -c cheat -l init -d "Write a default config file to stdout"
|
|
||||||
complete -c cheat -s c -l colorize -d "Colorize output"
|
|
||||||
complete -c cheat -s d -l directories -d "List cheatsheet directories"
|
|
||||||
complete -c cheat -s e -l edit -x -a "(cheat -l | tail -n +2 | cut -d ' ' -f 1)" -d "Edit cheatsheet"
|
|
||||||
complete -c cheat -s l -l list -d "List cheatsheets"
|
|
||||||
complete -c cheat -s p -l path -x -a "(cheat -d | cut -d ':' -f 1)" -d "Return only sheets found on given path"
|
|
||||||
complete -c cheat -s r -l regex -d "Treat search phrase as a regex"
|
|
||||||
complete -c cheat -s s -l search -x -d "Search cheatsheets for given phrase"
|
|
||||||
complete -c cheat -s t -l tag -x -a "(cheat -T)" -d "Return only sheets matching the given tag"
|
|
||||||
complete -c cheat -s T -l tags -d "List all tags in use"
|
|
||||||
complete -c cheat -s v -l version -d "Print the version number"
|
|
||||||
complete -c cheat -l rm -x -a "(cheat -l | tail -n +2 | cut -d ' ' -f 1)" -d "Remove (delete) cheatsheet"
|
|
||||||
@@ -1,65 +0,0 @@
|
|||||||
#compdef cheat
|
|
||||||
|
|
||||||
local cheats taglist pathlist
|
|
||||||
|
|
||||||
_cheat_complete_personal_cheatsheets()
|
|
||||||
{
|
|
||||||
cheats=("${(f)$(cheat -l -t personal | tail -n +2 | cut -d' ' -f1)}")
|
|
||||||
_describe -t cheats 'cheats' cheats
|
|
||||||
}
|
|
||||||
|
|
||||||
_cheat_complete_full_cheatsheets()
|
|
||||||
{
|
|
||||||
cheats=("${(f)$(cheat -l | tail -n +2 | cut -d' ' -f1)}")
|
|
||||||
_describe -t cheats 'cheats' cheats
|
|
||||||
}
|
|
||||||
|
|
||||||
_cheat_complete_tags()
|
|
||||||
{
|
|
||||||
taglist=("${(f)$(cheat -T)}")
|
|
||||||
_describe -t taglist 'taglist' taglist
|
|
||||||
}
|
|
||||||
|
|
||||||
_cheat_complete_paths()
|
|
||||||
{
|
|
||||||
pathlist=("${(f)$(cheat -d | cut -d':' -f1)}")
|
|
||||||
_describe -t pathlist 'pathlist' pathlist
|
|
||||||
}
|
|
||||||
|
|
||||||
_cheat() {
|
|
||||||
|
|
||||||
_arguments -C \
|
|
||||||
'(--init)--init[Write a default config file to stdout]: :->none' \
|
|
||||||
'(-c --colorize)'{-c,--colorize}'[Colorize output]: :->none' \
|
|
||||||
'(-d --directories)'{-d,--directories}'[List cheatsheet directories]: :->none' \
|
|
||||||
'(-e --edit)'{-e,--edit}'[Edit <sheet>]: :->personal' \
|
|
||||||
'(-l --list)'{-l,--list}'[List cheatsheets]: :->full' \
|
|
||||||
'(-p --path)'{-p,--path}'[Return only sheets found on path <name>]: :->pathlist' \
|
|
||||||
'(-r --regex)'{-r,--regex}'[Treat search <phrase> as a regex]: :->none' \
|
|
||||||
'(-s --search)'{-s,--search}'[Search cheatsheets for <phrase>]: :->none' \
|
|
||||||
'(-t --tag)'{-t,--tag}'[Return only sheets matching <tag>]: :->taglist' \
|
|
||||||
'(-T --tags)'{-T,--tags}'[List all tags in use]: :->none' \
|
|
||||||
'(-v --version)'{-v,--version}'[Print the version number]: :->none' \
|
|
||||||
'(--rm)--rm[Remove (delete) <sheet>]: :->personal'
|
|
||||||
|
|
||||||
case $state in
|
|
||||||
(none)
|
|
||||||
;;
|
|
||||||
(full)
|
|
||||||
_cheat_complete_full_cheatsheets
|
|
||||||
;;
|
|
||||||
(personal)
|
|
||||||
_cheat_complete_personal_cheatsheets
|
|
||||||
;;
|
|
||||||
(taglist)
|
|
||||||
_cheat_complete_tags
|
|
||||||
;;
|
|
||||||
(pathlist)
|
|
||||||
_cheat_complete_paths
|
|
||||||
;;
|
|
||||||
(*)
|
|
||||||
;;
|
|
||||||
esac
|
|
||||||
}
|
|
||||||
|
|
||||||
compdef _cheat cheat
|
|
||||||
@@ -1,46 +0,0 @@
|
|||||||
#!/bin/sh -e
|
|
||||||
|
|
||||||
pull() {
|
|
||||||
for d in `cheat -d | awk '{print $2}'`;
|
|
||||||
do
|
|
||||||
echo "Update $d"
|
|
||||||
cd "$d"
|
|
||||||
[ -d ".git" ] && git pull || :
|
|
||||||
done
|
|
||||||
|
|
||||||
echo
|
|
||||||
echo "Finished update"
|
|
||||||
}
|
|
||||||
|
|
||||||
push() {
|
|
||||||
for d in `cheat -d | grep -v "community" | awk '{print $2}'`;
|
|
||||||
do
|
|
||||||
cd "$d"
|
|
||||||
if [ -d ".git" ]
|
|
||||||
then
|
|
||||||
echo "Push modifications $d"
|
|
||||||
files=$(git ls-files -mo | tr '\n' ' ')
|
|
||||||
git add -A && git commit -m "Edited files: $files" && git push || :
|
|
||||||
else
|
|
||||||
echo "$(pwd) is not a git managed folder"
|
|
||||||
echo "First connect this to your personal git repository"
|
|
||||||
fi
|
|
||||||
done
|
|
||||||
|
|
||||||
echo
|
|
||||||
echo "Finished push operation"
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
if [ "$1" = "pull" ]; then
|
|
||||||
pull
|
|
||||||
elif [ "$1" = "push" ]; then
|
|
||||||
push
|
|
||||||
else
|
|
||||||
echo "Usage:
|
|
||||||
# pull changes
|
|
||||||
cheatsheets pull
|
|
||||||
|
|
||||||
# push changes
|
|
||||||
cheatsheets push"
|
|
||||||
fi
|
|
||||||
@@ -16,14 +16,12 @@ DURATION="${1:-15s}"
|
|||||||
# Define fuzz tests: "TestName:Package:Description"
|
# Define fuzz tests: "TestName:Package:Description"
|
||||||
TESTS=(
|
TESTS=(
|
||||||
"FuzzParse:./internal/sheet:YAML frontmatter parsing"
|
"FuzzParse:./internal/sheet:YAML frontmatter parsing"
|
||||||
"FuzzValidateSheetName:./internal/cheatpath:sheet name validation (path traversal protection)"
|
"FuzzValidate:./internal/sheet:sheet name validation (path traversal protection)"
|
||||||
"FuzzSearchRegex:./internal/sheet:regex search operations"
|
"FuzzSearchRegex:./internal/sheet:regex search operations"
|
||||||
"FuzzSearchCatastrophicBacktracking:./internal/sheet:catastrophic backtracking"
|
|
||||||
"FuzzTagged:./internal/sheet:tag matching with malicious input"
|
"FuzzTagged:./internal/sheet:tag matching with malicious input"
|
||||||
"FuzzFilter:./internal/sheets:tag filtering operations"
|
"FuzzFilter:./internal/sheets:tag filtering operations"
|
||||||
"FuzzTags:./internal/sheets:tag aggregation and sorting"
|
"FuzzTags:./internal/sheets:tag aggregation and sorting"
|
||||||
"FuzzFindLocalCheatpath:./internal/config:recursive .cheat directory discovery"
|
"FuzzFindLocalCheatpath:./internal/config:recursive .cheat directory discovery"
|
||||||
"FuzzFindLocalCheatpathNearestWins:./internal/config:nearest .cheat wins invariant"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
echo "Running fuzz tests ($DURATION each)..."
|
echo "Running fuzz tests ($DURATION each)..."
|
||||||
129
test/integration/brief_integration_test.go
Normal file
129
test/integration/brief_integration_test.go
Normal file
@@ -0,0 +1,129 @@
|
|||||||
|
package integration
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"os/exec"
|
||||||
|
"path/filepath"
|
||||||
|
"runtime"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
// TestBriefFlagIntegration exercises the -b/--brief flag end-to-end.
|
||||||
|
func TestBriefFlagIntegration(t *testing.T) {
|
||||||
|
if runtime.GOOS == "windows" {
|
||||||
|
t.Skip("integration test uses Unix-specific env vars")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Build the cheat binary once for all sub-tests.
|
||||||
|
binPath := filepath.Join(t.TempDir(), "cheat_test")
|
||||||
|
build := exec.Command("go", "build", "-o", binPath, "./cmd/cheat")
|
||||||
|
build.Dir = repoRoot(t)
|
||||||
|
if output, err := build.CombinedOutput(); err != nil {
|
||||||
|
t.Fatalf("failed to build cheat: %v\nOutput: %s", err, output)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set up a temp environment with some cheatsheets.
|
||||||
|
root := t.TempDir()
|
||||||
|
sheetsDir := filepath.Join(root, "sheets")
|
||||||
|
os.MkdirAll(sheetsDir, 0755)
|
||||||
|
|
||||||
|
os.WriteFile(
|
||||||
|
filepath.Join(sheetsDir, "tar"),
|
||||||
|
[]byte("---\nsyntax: bash\ntags: [ compression ]\n---\ntar xf archive.tar\n"),
|
||||||
|
0644,
|
||||||
|
)
|
||||||
|
os.WriteFile(
|
||||||
|
filepath.Join(sheetsDir, "curl"),
|
||||||
|
[]byte("---\nsyntax: bash\ntags: [ networking, http ]\n---\ncurl https://example.com\n"),
|
||||||
|
0644,
|
||||||
|
)
|
||||||
|
|
||||||
|
confPath := filepath.Join(root, "conf.yml")
|
||||||
|
conf := fmt.Sprintf("---\neditor: vi\ncolorize: false\ncheatpaths:\n - name: test\n path: %s\n readonly: true\n", sheetsDir)
|
||||||
|
os.WriteFile(confPath, []byte(conf), 0644)
|
||||||
|
|
||||||
|
env := []string{
|
||||||
|
"CHEAT_CONFIG_PATH=" + confPath,
|
||||||
|
"HOME=" + root,
|
||||||
|
"PATH=" + os.Getenv("PATH"),
|
||||||
|
"EDITOR=vi",
|
||||||
|
}
|
||||||
|
|
||||||
|
run := func(t *testing.T, args ...string) string {
|
||||||
|
t.Helper()
|
||||||
|
cmd := exec.Command(binPath, args...)
|
||||||
|
cmd.Dir = root
|
||||||
|
cmd.Env = env
|
||||||
|
output, err := cmd.CombinedOutput()
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("cheat %v failed: %v\nOutput: %s", args, err, output)
|
||||||
|
}
|
||||||
|
return string(output)
|
||||||
|
}
|
||||||
|
|
||||||
|
t.Run("brief output omits file path column", func(t *testing.T) {
|
||||||
|
output := run(t, "-b")
|
||||||
|
lines := strings.Split(strings.TrimSpace(output), "\n")
|
||||||
|
|
||||||
|
// Header should have title and tags but not file
|
||||||
|
if !strings.Contains(lines[0], "title:") {
|
||||||
|
t.Errorf("expected title: in header, got: %s", lines[0])
|
||||||
|
}
|
||||||
|
if !strings.Contains(lines[0], "tags:") {
|
||||||
|
t.Errorf("expected tags: in header, got: %s", lines[0])
|
||||||
|
}
|
||||||
|
if strings.Contains(lines[0], "file:") {
|
||||||
|
t.Errorf("brief output should not contain file: column, got: %s", lines[0])
|
||||||
|
}
|
||||||
|
|
||||||
|
// Data lines should not contain the sheets directory path
|
||||||
|
for _, line := range lines[1:] {
|
||||||
|
if strings.Contains(line, sheetsDir) {
|
||||||
|
t.Errorf("brief output should not contain file paths, got: %s", line)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("list output still includes file path column", func(t *testing.T) {
|
||||||
|
output := run(t, "-l")
|
||||||
|
lines := strings.Split(strings.TrimSpace(output), "\n")
|
||||||
|
|
||||||
|
if !strings.Contains(lines[0], "file:") {
|
||||||
|
t.Errorf("list output should contain file: column, got: %s", lines[0])
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("brief with filter works", func(t *testing.T) {
|
||||||
|
output := run(t, "-b", "tar")
|
||||||
|
if !strings.Contains(output, "tar") {
|
||||||
|
t.Errorf("expected tar in output, got: %s", output)
|
||||||
|
}
|
||||||
|
if strings.Contains(output, "curl") {
|
||||||
|
t.Errorf("filter should exclude curl, got: %s", output)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("combined -lb works identically to -b", func(t *testing.T) {
|
||||||
|
briefOnly := run(t, "-b", "tar")
|
||||||
|
combined := run(t, "-lb", "tar")
|
||||||
|
if briefOnly != combined {
|
||||||
|
t.Errorf("-b and -lb should produce identical output\n-b:\n%s\n-lb:\n%s", briefOnly, combined)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("brief with tag filter works", func(t *testing.T) {
|
||||||
|
output := run(t, "-b", "-t", "networking")
|
||||||
|
if !strings.Contains(output, "curl") {
|
||||||
|
t.Errorf("expected curl in tag-filtered output, got: %s", output)
|
||||||
|
}
|
||||||
|
if strings.Contains(output, "tar") {
|
||||||
|
// tar is tagged "compression", not "networking"
|
||||||
|
t.Errorf("tag filter should exclude tar, got: %s", output)
|
||||||
|
}
|
||||||
|
if strings.Contains(output, "file:") {
|
||||||
|
t.Errorf("brief output should not contain file: column, got: %s", output)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package main
|
package integration
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
@@ -32,7 +32,8 @@ func TestLocalCheatpathIntegration(t *testing.T) {
|
|||||||
|
|
||||||
// Build the cheat binary once for all sub-tests.
|
// Build the cheat binary once for all sub-tests.
|
||||||
binPath := filepath.Join(t.TempDir(), "cheat_test")
|
binPath := filepath.Join(t.TempDir(), "cheat_test")
|
||||||
build := exec.Command("go", "build", "-o", binPath, ".")
|
build := exec.Command("go", "build", "-o", binPath, "./cmd/cheat")
|
||||||
|
build.Dir = repoRoot(t)
|
||||||
if output, err := build.CombinedOutput(); err != nil {
|
if output, err := build.CombinedOutput(); err != nil {
|
||||||
t.Fatalf("failed to build cheat: %v\nOutput: %s", err, output)
|
t.Fatalf("failed to build cheat: %v\nOutput: %s", err, output)
|
||||||
}
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package main
|
package integration
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
"os"
|
||||||
@@ -19,7 +19,8 @@ func TestFirstRunIntegration(t *testing.T) {
|
|||||||
binName += ".exe"
|
binName += ".exe"
|
||||||
}
|
}
|
||||||
binPath := filepath.Join(t.TempDir(), binName)
|
binPath := filepath.Join(t.TempDir(), binName)
|
||||||
build := exec.Command("go", "build", "-o", binPath, ".")
|
build := exec.Command("go", "build", "-o", binPath, "./cmd/cheat")
|
||||||
|
build.Dir = repoRoot(t)
|
||||||
if output, err := build.CombinedOutput(); err != nil {
|
if output, err := build.CombinedOutput(); err != nil {
|
||||||
t.Fatalf("failed to build cheat: %v\nOutput: %s", err, output)
|
t.Fatalf("failed to build cheat: %v\nOutput: %s", err, output)
|
||||||
}
|
}
|
||||||
30
test/integration/helpers_test.go
Normal file
30
test/integration/helpers_test.go
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
package integration
|
||||||
|
|
||||||
|
import (
|
||||||
|
"path/filepath"
|
||||||
|
"runtime"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
// repoRoot returns the absolute path to the repository root.
|
||||||
|
// It derives this from the known location of this source file
|
||||||
|
// (test/integration/) relative to the repo root.
|
||||||
|
func repoRoot(t *testing.T) string {
|
||||||
|
t.Helper()
|
||||||
|
_, file, _, ok := runtime.Caller(0)
|
||||||
|
if !ok {
|
||||||
|
t.Fatal("failed to determine repo root via runtime.Caller")
|
||||||
|
}
|
||||||
|
// file is <repo>/test/integration/helpers_test.go → go up two dirs
|
||||||
|
return filepath.Dir(filepath.Dir(filepath.Dir(file)))
|
||||||
|
}
|
||||||
|
|
||||||
|
// repoRootBench is the same as repoRoot but for use in benchmarks.
|
||||||
|
func repoRootBench(b *testing.B) string {
|
||||||
|
b.Helper()
|
||||||
|
_, file, _, ok := runtime.Caller(0)
|
||||||
|
if !ok {
|
||||||
|
b.Fatal("failed to determine repo root via runtime.Caller")
|
||||||
|
}
|
||||||
|
return filepath.Dir(filepath.Dir(filepath.Dir(file)))
|
||||||
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package main
|
package integration
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
@@ -19,7 +19,9 @@ func TestPathTraversalIntegration(t *testing.T) {
|
|||||||
|
|
||||||
// Build the cheat binary
|
// Build the cheat binary
|
||||||
binPath := filepath.Join(t.TempDir(), "cheat_test")
|
binPath := filepath.Join(t.TempDir(), "cheat_test")
|
||||||
if output, err := exec.Command("go", "build", "-o", binPath, ".").CombinedOutput(); err != nil {
|
build := exec.Command("go", "build", "-o", binPath, "./cmd/cheat")
|
||||||
|
build.Dir = repoRoot(t)
|
||||||
|
if output, err := build.CombinedOutput(); err != nil {
|
||||||
t.Fatalf("Failed to build cheat: %v\nOutput: %s", err, output)
|
t.Fatalf("Failed to build cheat: %v\nOutput: %s", err, output)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -159,7 +161,9 @@ func TestPathTraversalRealWorld(t *testing.T) {
|
|||||||
|
|
||||||
// Build cheat
|
// Build cheat
|
||||||
binPath := filepath.Join(t.TempDir(), "cheat_test")
|
binPath := filepath.Join(t.TempDir(), "cheat_test")
|
||||||
if output, err := exec.Command("go", "build", "-o", binPath, ".").CombinedOutput(); err != nil {
|
build := exec.Command("go", "build", "-o", binPath, "./cmd/cheat")
|
||||||
|
build.Dir = repoRoot(t)
|
||||||
|
if output, err := build.CombinedOutput(); err != nil {
|
||||||
t.Fatalf("Failed to build: %v\n%s", err, output)
|
t.Fatalf("Failed to build: %v\n%s", err, output)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
//go:build integration
|
//go:build integration
|
||||||
|
|
||||||
package main
|
package integration
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
@@ -9,7 +9,6 @@ import (
|
|||||||
"os/exec"
|
"os/exec"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/go-git/go-git/v5"
|
"github.com/go-git/go-git/v5"
|
||||||
"github.com/go-git/go-git/v5/plumbing"
|
"github.com/go-git/go-git/v5/plumbing"
|
||||||
@@ -17,12 +16,10 @@ import (
|
|||||||
|
|
||||||
// BenchmarkSearchCommand benchmarks the actual cheat search command
|
// BenchmarkSearchCommand benchmarks the actual cheat search command
|
||||||
func BenchmarkSearchCommand(b *testing.B) {
|
func BenchmarkSearchCommand(b *testing.B) {
|
||||||
|
root := repoRootBench(b)
|
||||||
|
|
||||||
// Build the cheat binary in .tmp (using absolute path)
|
// Build the cheat binary in .tmp (using absolute path)
|
||||||
rootDir, err := filepath.Abs(filepath.Join("..", ".."))
|
tmpDir := filepath.Join(root, ".tmp", "bench-test")
|
||||||
if err != nil {
|
|
||||||
b.Fatalf("Failed to get root dir: %v", err)
|
|
||||||
}
|
|
||||||
tmpDir := filepath.Join(rootDir, ".tmp", "bench-test")
|
|
||||||
if err := os.MkdirAll(tmpDir, 0755); err != nil {
|
if err := os.MkdirAll(tmpDir, 0755); err != nil {
|
||||||
b.Fatalf("Failed to create temp dir: %v", err)
|
b.Fatalf("Failed to create temp dir: %v", err)
|
||||||
}
|
}
|
||||||
@@ -35,7 +32,7 @@ func BenchmarkSearchCommand(b *testing.B) {
|
|||||||
})
|
})
|
||||||
|
|
||||||
cmd := exec.Command("go", "build", "-o", cheatBin, "./cmd/cheat")
|
cmd := exec.Command("go", "build", "-o", cheatBin, "./cmd/cheat")
|
||||||
cmd.Dir = rootDir
|
cmd.Dir = root
|
||||||
if output, err := cmd.CombinedOutput(); err != nil {
|
if output, err := cmd.CombinedOutput(); err != nil {
|
||||||
b.Fatalf("Failed to build cheat: %v\nOutput: %s", err, output)
|
b.Fatalf("Failed to build cheat: %v\nOutput: %s", err, output)
|
||||||
}
|
}
|
||||||
@@ -108,23 +105,15 @@ cheatpaths:
|
|||||||
cmd := exec.Command(cheatBin, tc.args...)
|
cmd := exec.Command(cheatBin, tc.args...)
|
||||||
cmd.Env = env
|
cmd.Env = env
|
||||||
|
|
||||||
// Capture output to prevent spamming
|
|
||||||
var stdout, stderr bytes.Buffer
|
var stdout, stderr bytes.Buffer
|
||||||
cmd.Stdout = &stdout
|
cmd.Stdout = &stdout
|
||||||
cmd.Stderr = &stderr
|
cmd.Stderr = &stderr
|
||||||
|
|
||||||
start := time.Now()
|
|
||||||
err := cmd.Run()
|
err := cmd.Run()
|
||||||
elapsed := time.Since(start)
|
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
b.Fatalf("Command failed: %v\nStderr: %s", err, stderr.String())
|
b.Fatalf("Command failed: %v\nStderr: %s", err, stderr.String())
|
||||||
}
|
}
|
||||||
|
|
||||||
// Report custom metric
|
|
||||||
b.ReportMetric(float64(elapsed.Nanoseconds())/1e6, "ms/op")
|
|
||||||
|
|
||||||
// Ensure we got some results
|
|
||||||
if stdout.Len() == 0 {
|
if stdout.Len() == 0 {
|
||||||
b.Fatal("No output from search")
|
b.Fatal("No output from search")
|
||||||
}
|
}
|
||||||
@@ -135,12 +124,10 @@ cheatpaths:
|
|||||||
|
|
||||||
// BenchmarkListCommand benchmarks the list command for comparison
|
// BenchmarkListCommand benchmarks the list command for comparison
|
||||||
func BenchmarkListCommand(b *testing.B) {
|
func BenchmarkListCommand(b *testing.B) {
|
||||||
|
root := repoRootBench(b)
|
||||||
|
|
||||||
// Build the cheat binary in .tmp (using absolute path)
|
// Build the cheat binary in .tmp (using absolute path)
|
||||||
rootDir, err := filepath.Abs(filepath.Join("..", ".."))
|
tmpDir := filepath.Join(root, ".tmp", "bench-test")
|
||||||
if err != nil {
|
|
||||||
b.Fatalf("Failed to get root dir: %v", err)
|
|
||||||
}
|
|
||||||
tmpDir := filepath.Join(rootDir, ".tmp", "bench-test")
|
|
||||||
if err := os.MkdirAll(tmpDir, 0755); err != nil {
|
if err := os.MkdirAll(tmpDir, 0755); err != nil {
|
||||||
b.Fatalf("Failed to create temp dir: %v", err)
|
b.Fatalf("Failed to create temp dir: %v", err)
|
||||||
}
|
}
|
||||||
@@ -153,7 +140,7 @@ func BenchmarkListCommand(b *testing.B) {
|
|||||||
})
|
})
|
||||||
|
|
||||||
cmd := exec.Command("go", "build", "-o", cheatBin, "./cmd/cheat")
|
cmd := exec.Command("go", "build", "-o", cheatBin, "./cmd/cheat")
|
||||||
cmd.Dir = rootDir
|
cmd.Dir = root
|
||||||
if output, err := cmd.CombinedOutput(); err != nil {
|
if output, err := cmd.CombinedOutput(); err != nil {
|
||||||
b.Fatalf("Failed to build cheat: %v\nOutput: %s", err, output)
|
b.Fatalf("Failed to build cheat: %v\nOutput: %s", err, output)
|
||||||
}
|
}
|
||||||
32
vendor/github.com/docopt/docopt-go/.travis.yml
generated
vendored
32
vendor/github.com/docopt/docopt-go/.travis.yml
generated
vendored
@@ -1,32 +0,0 @@
|
|||||||
# Travis CI (http://travis-ci.org/) is a continuous integration
|
|
||||||
# service for open source projects. This file configures it
|
|
||||||
# to run unit tests for docopt-go.
|
|
||||||
|
|
||||||
language: go
|
|
||||||
|
|
||||||
go:
|
|
||||||
- 1.4
|
|
||||||
- 1.5
|
|
||||||
- 1.6
|
|
||||||
- 1.7
|
|
||||||
- 1.8
|
|
||||||
- 1.9
|
|
||||||
- tip
|
|
||||||
|
|
||||||
matrix:
|
|
||||||
fast_finish: true
|
|
||||||
|
|
||||||
before_install:
|
|
||||||
- go get golang.org/x/tools/cmd/cover
|
|
||||||
- go get github.com/mattn/goveralls
|
|
||||||
|
|
||||||
install:
|
|
||||||
- go get -d -v ./... && go build -v ./...
|
|
||||||
|
|
||||||
script:
|
|
||||||
- go vet -x ./...
|
|
||||||
- go test -v ./...
|
|
||||||
- go test -covermode=count -coverprofile=profile.cov .
|
|
||||||
|
|
||||||
after_script:
|
|
||||||
- $HOME/gopath/bin/goveralls -coverprofile=profile.cov -service=travis-ci
|
|
||||||
21
vendor/github.com/docopt/docopt-go/LICENSE
generated
vendored
21
vendor/github.com/docopt/docopt-go/LICENSE
generated
vendored
@@ -1,21 +0,0 @@
|
|||||||
The MIT License (MIT)
|
|
||||||
|
|
||||||
Copyright (c) 2013 Keith Batten
|
|
||||||
Copyright (c) 2016 David Irvine
|
|
||||||
|
|
||||||
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
|
||||||
this software and associated documentation files (the "Software"), to deal in
|
|
||||||
the Software without restriction, including without limitation the rights to
|
|
||||||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
|
||||||
the Software, and to permit persons to whom the Software is furnished to do so,
|
|
||||||
subject to the following conditions:
|
|
||||||
|
|
||||||
The above copyright notice and this permission notice shall be included in all
|
|
||||||
copies or substantial portions of the Software.
|
|
||||||
|
|
||||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
||||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
|
||||||
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
|
||||||
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
|
||||||
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
|
||||||
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
||||||
116
vendor/github.com/docopt/docopt-go/README.md
generated
vendored
116
vendor/github.com/docopt/docopt-go/README.md
generated
vendored
@@ -1,116 +0,0 @@
|
|||||||
docopt-go
|
|
||||||
=========
|
|
||||||
|
|
||||||
[](https://travis-ci.org/docopt/docopt.go)
|
|
||||||
[](https://coveralls.io/github/docopt/docopt.go)
|
|
||||||
[](https://godoc.org/github.com/docopt/docopt.go)
|
|
||||||
|
|
||||||
An implementation of [docopt](http://docopt.org/) in the [Go](http://golang.org/) programming language.
|
|
||||||
|
|
||||||
**docopt** helps you create *beautiful* command-line interfaces easily:
|
|
||||||
|
|
||||||
```go
|
|
||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"github.com/docopt/docopt-go"
|
|
||||||
)
|
|
||||||
|
|
||||||
func main() {
|
|
||||||
usage := `Naval Fate.
|
|
||||||
|
|
||||||
Usage:
|
|
||||||
naval_fate ship new <name>...
|
|
||||||
naval_fate ship <name> move <x> <y> [--speed=<kn>]
|
|
||||||
naval_fate ship shoot <x> <y>
|
|
||||||
naval_fate mine (set|remove) <x> <y> [--moored|--drifting]
|
|
||||||
naval_fate -h | --help
|
|
||||||
naval_fate --version
|
|
||||||
|
|
||||||
Options:
|
|
||||||
-h --help Show this screen.
|
|
||||||
--version Show version.
|
|
||||||
--speed=<kn> Speed in knots [default: 10].
|
|
||||||
--moored Moored (anchored) mine.
|
|
||||||
--drifting Drifting mine.`
|
|
||||||
|
|
||||||
arguments, _ := docopt.ParseDoc(usage)
|
|
||||||
fmt.Println(arguments)
|
|
||||||
}
|
|
||||||
```
|
|
||||||
|
|
||||||
**docopt** parses command-line arguments based on a help message. Don't write parser code: a good help message already has all the necessary information in it.
|
|
||||||
|
|
||||||
## Installation
|
|
||||||
|
|
||||||
⚠ Use the alias "docopt-go". To use docopt in your Go code:
|
|
||||||
|
|
||||||
```go
|
|
||||||
import "github.com/docopt/docopt-go"
|
|
||||||
```
|
|
||||||
|
|
||||||
To install docopt in your `$GOPATH`:
|
|
||||||
|
|
||||||
```console
|
|
||||||
$ go get github.com/docopt/docopt-go
|
|
||||||
```
|
|
||||||
|
|
||||||
## API
|
|
||||||
|
|
||||||
Given a conventional command-line help message, docopt processes the arguments. See https://github.com/docopt/docopt#help-message-format for a description of the help message format.
|
|
||||||
|
|
||||||
This package exposes three different APIs, depending on the level of control required. The first, simplest way to parse your docopt usage is to just call:
|
|
||||||
|
|
||||||
```go
|
|
||||||
docopt.ParseDoc(usage)
|
|
||||||
```
|
|
||||||
|
|
||||||
This will use `os.Args[1:]` as the argv slice, and use the default parser options. If you want to provide your own version string and args, then use:
|
|
||||||
|
|
||||||
```go
|
|
||||||
docopt.ParseArgs(usage, argv, "1.2.3")
|
|
||||||
```
|
|
||||||
|
|
||||||
If the last parameter (version) is a non-empty string, it will be printed when `--version` is given in the argv slice. Finally, we can instantiate our own `docopt.Parser` which gives us control over how things like help messages are printed and whether to exit after displaying usage messages, etc.
|
|
||||||
|
|
||||||
```go
|
|
||||||
parser := &docopt.Parser{
|
|
||||||
HelpHandler: docopt.PrintHelpOnly,
|
|
||||||
OptionsFirst: true,
|
|
||||||
}
|
|
||||||
opts, err := parser.ParseArgs(usage, argv, "")
|
|
||||||
```
|
|
||||||
|
|
||||||
In particular, setting your own custom `HelpHandler` function makes unit testing your own docs with example command line invocations much more enjoyable.
|
|
||||||
|
|
||||||
All three of these return a map of option names to the values parsed from argv, and an error or nil. You can get the values using the helpers, or just treat it as a regular map:
|
|
||||||
|
|
||||||
```go
|
|
||||||
flag, _ := opts.Bool("--flag")
|
|
||||||
secs, _ := opts.Int("<seconds>")
|
|
||||||
```
|
|
||||||
|
|
||||||
Additionally, you can `Bind` these to a struct, assigning option values to the
|
|
||||||
exported fields of that struct, all at once.
|
|
||||||
|
|
||||||
```go
|
|
||||||
var config struct {
|
|
||||||
Command string `docopt:"<cmd>"`
|
|
||||||
Tries int `docopt:"-n"`
|
|
||||||
Force bool // Gets the value of --force
|
|
||||||
}
|
|
||||||
opts.Bind(&config)
|
|
||||||
```
|
|
||||||
|
|
||||||
More documentation is available at [godoc.org](https://godoc.org/github.com/docopt/docopt-go).
|
|
||||||
|
|
||||||
## Unit Testing
|
|
||||||
|
|
||||||
Unit testing your own usage docs is recommended, so you can be sure that for a given command line invocation, the expected options are set. An example of how to do this is [in the examples folder](examples/unit_test/unit_test.go).
|
|
||||||
|
|
||||||
## Tests
|
|
||||||
|
|
||||||
All tests from the Python version are implemented and passing at [Travis CI](https://travis-ci.org/docopt/docopt-go). New language-agnostic tests have been added to [test_golang.docopt](test_golang.docopt).
|
|
||||||
|
|
||||||
To run tests for docopt-go, use `go test`.
|
|
||||||
49
vendor/github.com/docopt/docopt-go/doc.go
generated
vendored
49
vendor/github.com/docopt/docopt-go/doc.go
generated
vendored
@@ -1,49 +0,0 @@
|
|||||||
/*
|
|
||||||
Package docopt parses command-line arguments based on a help message.
|
|
||||||
|
|
||||||
Given a conventional command-line help message, docopt processes the arguments.
|
|
||||||
See https://github.com/docopt/docopt#help-message-format for a description of
|
|
||||||
the help message format.
|
|
||||||
|
|
||||||
This package exposes three different APIs, depending on the level of control
|
|
||||||
required. The first, simplest way to parse your docopt usage is to just call:
|
|
||||||
|
|
||||||
docopt.ParseDoc(usage)
|
|
||||||
|
|
||||||
This will use os.Args[1:] as the argv slice, and use the default parser
|
|
||||||
options. If you want to provide your own version string and args, then use:
|
|
||||||
|
|
||||||
docopt.ParseArgs(usage, argv, "1.2.3")
|
|
||||||
|
|
||||||
If the last parameter (version) is a non-empty string, it will be printed when
|
|
||||||
--version is given in the argv slice. Finally, we can instantiate our own
|
|
||||||
docopt.Parser which gives us control over how things like help messages are
|
|
||||||
printed and whether to exit after displaying usage messages, etc.
|
|
||||||
|
|
||||||
parser := &docopt.Parser{
|
|
||||||
HelpHandler: docopt.PrintHelpOnly,
|
|
||||||
OptionsFirst: true,
|
|
||||||
}
|
|
||||||
opts, err := parser.ParseArgs(usage, argv, "")
|
|
||||||
|
|
||||||
In particular, setting your own custom HelpHandler function makes unit testing
|
|
||||||
your own docs with example command line invocations much more enjoyable.
|
|
||||||
|
|
||||||
All three of these return a map of option names to the values parsed from argv,
|
|
||||||
and an error or nil. You can get the values using the helpers, or just treat it
|
|
||||||
as a regular map:
|
|
||||||
|
|
||||||
flag, _ := opts.Bool("--flag")
|
|
||||||
secs, _ := opts.Int("<seconds>")
|
|
||||||
|
|
||||||
Additionally, you can `Bind` these to a struct, assigning option values to the
|
|
||||||
exported fields of that struct, all at once.
|
|
||||||
|
|
||||||
var config struct {
|
|
||||||
Command string `docopt:"<cmd>"`
|
|
||||||
Tries int `docopt:"-n"`
|
|
||||||
Force bool // Gets the value of --force
|
|
||||||
}
|
|
||||||
opts.Bind(&config)
|
|
||||||
*/
|
|
||||||
package docopt
|
|
||||||
575
vendor/github.com/docopt/docopt-go/docopt.go
generated
vendored
575
vendor/github.com/docopt/docopt-go/docopt.go
generated
vendored
@@ -1,575 +0,0 @@
|
|||||||
// Licensed under terms of MIT license (see LICENSE-MIT)
|
|
||||||
// Copyright (c) 2013 Keith Batten, kbatten@gmail.com
|
|
||||||
// Copyright (c) 2016 David Irvine
|
|
||||||
|
|
||||||
package docopt
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"os"
|
|
||||||
"regexp"
|
|
||||||
"strings"
|
|
||||||
)
|
|
||||||
|
|
||||||
type Parser struct {
|
|
||||||
// HelpHandler is called when we encounter bad user input, or when the user
|
|
||||||
// asks for help.
|
|
||||||
// By default, this calls os.Exit(0) if it handled a built-in option such
|
|
||||||
// as -h, --help or --version. If the user errored with a wrong command or
|
|
||||||
// options, we exit with a return code of 1.
|
|
||||||
HelpHandler func(err error, usage string)
|
|
||||||
// OptionsFirst requires that option flags always come before positional
|
|
||||||
// arguments; otherwise they can overlap.
|
|
||||||
OptionsFirst bool
|
|
||||||
// SkipHelpFlags tells the parser not to look for -h and --help flags and
|
|
||||||
// call the HelpHandler.
|
|
||||||
SkipHelpFlags bool
|
|
||||||
}
|
|
||||||
|
|
||||||
var PrintHelpAndExit = func(err error, usage string) {
|
|
||||||
if err != nil {
|
|
||||||
fmt.Fprintln(os.Stderr, usage)
|
|
||||||
os.Exit(1)
|
|
||||||
} else {
|
|
||||||
fmt.Println(usage)
|
|
||||||
os.Exit(0)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
var PrintHelpOnly = func(err error, usage string) {
|
|
||||||
if err != nil {
|
|
||||||
fmt.Fprintln(os.Stderr, usage)
|
|
||||||
} else {
|
|
||||||
fmt.Println(usage)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
var NoHelpHandler = func(err error, usage string) {}
|
|
||||||
|
|
||||||
var DefaultParser = &Parser{
|
|
||||||
HelpHandler: PrintHelpAndExit,
|
|
||||||
OptionsFirst: false,
|
|
||||||
SkipHelpFlags: false,
|
|
||||||
}
|
|
||||||
|
|
||||||
// ParseDoc parses os.Args[1:] based on the interface described in doc, using the default parser options.
|
|
||||||
func ParseDoc(doc string) (Opts, error) {
|
|
||||||
return ParseArgs(doc, nil, "")
|
|
||||||
}
|
|
||||||
|
|
||||||
// ParseArgs parses custom arguments based on the interface described in doc. If you provide a non-empty version
|
|
||||||
// string, then this will be displayed when the --version flag is found. This method uses the default parser options.
|
|
||||||
func ParseArgs(doc string, argv []string, version string) (Opts, error) {
|
|
||||||
return DefaultParser.ParseArgs(doc, argv, version)
|
|
||||||
}
|
|
||||||
|
|
||||||
// ParseArgs parses custom arguments based on the interface described in doc. If you provide a non-empty version
|
|
||||||
// string, then this will be displayed when the --version flag is found.
|
|
||||||
func (p *Parser) ParseArgs(doc string, argv []string, version string) (Opts, error) {
|
|
||||||
return p.parse(doc, argv, version)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Deprecated: Parse is provided for backward compatibility with the original docopt.go package.
|
|
||||||
// Please rather make use of ParseDoc, ParseArgs, or use your own custom Parser.
|
|
||||||
func Parse(doc string, argv []string, help bool, version string, optionsFirst bool, exit ...bool) (map[string]interface{}, error) {
|
|
||||||
exitOk := true
|
|
||||||
if len(exit) > 0 {
|
|
||||||
exitOk = exit[0]
|
|
||||||
}
|
|
||||||
p := &Parser{
|
|
||||||
OptionsFirst: optionsFirst,
|
|
||||||
SkipHelpFlags: !help,
|
|
||||||
}
|
|
||||||
if exitOk {
|
|
||||||
p.HelpHandler = PrintHelpAndExit
|
|
||||||
} else {
|
|
||||||
p.HelpHandler = PrintHelpOnly
|
|
||||||
}
|
|
||||||
return p.parse(doc, argv, version)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *Parser) parse(doc string, argv []string, version string) (map[string]interface{}, error) {
|
|
||||||
if argv == nil {
|
|
||||||
argv = os.Args[1:]
|
|
||||||
}
|
|
||||||
if p.HelpHandler == nil {
|
|
||||||
p.HelpHandler = DefaultParser.HelpHandler
|
|
||||||
}
|
|
||||||
args, output, err := parse(doc, argv, !p.SkipHelpFlags, version, p.OptionsFirst)
|
|
||||||
if _, ok := err.(*UserError); ok {
|
|
||||||
// the user gave us bad input
|
|
||||||
p.HelpHandler(err, output)
|
|
||||||
} else if len(output) > 0 && err == nil {
|
|
||||||
// the user asked for help or --version
|
|
||||||
p.HelpHandler(err, output)
|
|
||||||
}
|
|
||||||
return args, err
|
|
||||||
}
|
|
||||||
|
|
||||||
// -----------------------------------------------------------------------------
|
|
||||||
|
|
||||||
// parse and return a map of args, output and all errors
|
|
||||||
func parse(doc string, argv []string, help bool, version string, optionsFirst bool) (args map[string]interface{}, output string, err error) {
|
|
||||||
if argv == nil && len(os.Args) > 1 {
|
|
||||||
argv = os.Args[1:]
|
|
||||||
}
|
|
||||||
|
|
||||||
usageSections := parseSection("usage:", doc)
|
|
||||||
|
|
||||||
if len(usageSections) == 0 {
|
|
||||||
err = newLanguageError("\"usage:\" (case-insensitive) not found.")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if len(usageSections) > 1 {
|
|
||||||
err = newLanguageError("More than one \"usage:\" (case-insensitive).")
|
|
||||||
return
|
|
||||||
}
|
|
||||||
usage := usageSections[0]
|
|
||||||
|
|
||||||
options := parseDefaults(doc)
|
|
||||||
formal, err := formalUsage(usage)
|
|
||||||
if err != nil {
|
|
||||||
output = handleError(err, usage)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
pat, err := parsePattern(formal, &options)
|
|
||||||
if err != nil {
|
|
||||||
output = handleError(err, usage)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
patternArgv, err := parseArgv(newTokenList(argv, errorUser), &options, optionsFirst)
|
|
||||||
if err != nil {
|
|
||||||
output = handleError(err, usage)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
patFlat, err := pat.flat(patternOption)
|
|
||||||
if err != nil {
|
|
||||||
output = handleError(err, usage)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
patternOptions := patFlat.unique()
|
|
||||||
|
|
||||||
patFlat, err = pat.flat(patternOptionSSHORTCUT)
|
|
||||||
if err != nil {
|
|
||||||
output = handleError(err, usage)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
for _, optionsShortcut := range patFlat {
|
|
||||||
docOptions := parseDefaults(doc)
|
|
||||||
optionsShortcut.children = docOptions.unique().diff(patternOptions)
|
|
||||||
}
|
|
||||||
|
|
||||||
if output = extras(help, version, patternArgv, doc); len(output) > 0 {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
err = pat.fix()
|
|
||||||
if err != nil {
|
|
||||||
output = handleError(err, usage)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
matched, left, collected := pat.match(&patternArgv, nil)
|
|
||||||
if matched && len(*left) == 0 {
|
|
||||||
patFlat, err = pat.flat(patternDefault)
|
|
||||||
if err != nil {
|
|
||||||
output = handleError(err, usage)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
args = append(patFlat, *collected...).dictionary()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
err = newUserError("")
|
|
||||||
output = handleError(err, usage)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
func handleError(err error, usage string) string {
|
|
||||||
if _, ok := err.(*UserError); ok {
|
|
||||||
return strings.TrimSpace(fmt.Sprintf("%s\n%s", err, usage))
|
|
||||||
}
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
|
|
||||||
func parseSection(name, source string) []string {
|
|
||||||
p := regexp.MustCompile(`(?im)^([^\n]*` + name + `[^\n]*\n?(?:[ \t].*?(?:\n|$))*)`)
|
|
||||||
s := p.FindAllString(source, -1)
|
|
||||||
if s == nil {
|
|
||||||
s = []string{}
|
|
||||||
}
|
|
||||||
for i, v := range s {
|
|
||||||
s[i] = strings.TrimSpace(v)
|
|
||||||
}
|
|
||||||
return s
|
|
||||||
}
|
|
||||||
|
|
||||||
func parseDefaults(doc string) patternList {
|
|
||||||
defaults := patternList{}
|
|
||||||
p := regexp.MustCompile(`\n[ \t]*(-\S+?)`)
|
|
||||||
for _, s := range parseSection("options:", doc) {
|
|
||||||
// FIXME corner case "bla: options: --foo"
|
|
||||||
_, _, s = stringPartition(s, ":") // get rid of "options:"
|
|
||||||
split := p.Split("\n"+s, -1)[1:]
|
|
||||||
match := p.FindAllStringSubmatch("\n"+s, -1)
|
|
||||||
for i := range split {
|
|
||||||
optionDescription := match[i][1] + split[i]
|
|
||||||
if strings.HasPrefix(optionDescription, "-") {
|
|
||||||
defaults = append(defaults, parseOption(optionDescription))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return defaults
|
|
||||||
}
|
|
||||||
|
|
||||||
func parsePattern(source string, options *patternList) (*pattern, error) {
|
|
||||||
tokens := tokenListFromPattern(source)
|
|
||||||
result, err := parseExpr(tokens, options)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
if tokens.current() != nil {
|
|
||||||
return nil, tokens.errorFunc("unexpected ending: %s" + strings.Join(tokens.tokens, " "))
|
|
||||||
}
|
|
||||||
return newRequired(result...), nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func parseArgv(tokens *tokenList, options *patternList, optionsFirst bool) (patternList, error) {
|
|
||||||
/*
|
|
||||||
Parse command-line argument vector.
|
|
||||||
|
|
||||||
If options_first:
|
|
||||||
argv ::= [ long | shorts ]* [ argument ]* [ '--' [ argument ]* ] ;
|
|
||||||
else:
|
|
||||||
argv ::= [ long | shorts | argument ]* [ '--' [ argument ]* ] ;
|
|
||||||
*/
|
|
||||||
parsed := patternList{}
|
|
||||||
for tokens.current() != nil {
|
|
||||||
if tokens.current().eq("--") {
|
|
||||||
for _, v := range tokens.tokens {
|
|
||||||
parsed = append(parsed, newArgument("", v))
|
|
||||||
}
|
|
||||||
return parsed, nil
|
|
||||||
} else if tokens.current().hasPrefix("--") {
|
|
||||||
pl, err := parseLong(tokens, options)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
parsed = append(parsed, pl...)
|
|
||||||
} else if tokens.current().hasPrefix("-") && !tokens.current().eq("-") {
|
|
||||||
ps, err := parseShorts(tokens, options)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
parsed = append(parsed, ps...)
|
|
||||||
} else if optionsFirst {
|
|
||||||
for _, v := range tokens.tokens {
|
|
||||||
parsed = append(parsed, newArgument("", v))
|
|
||||||
}
|
|
||||||
return parsed, nil
|
|
||||||
} else {
|
|
||||||
parsed = append(parsed, newArgument("", tokens.move().String()))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return parsed, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func parseOption(optionDescription string) *pattern {
|
|
||||||
optionDescription = strings.TrimSpace(optionDescription)
|
|
||||||
options, _, description := stringPartition(optionDescription, " ")
|
|
||||||
options = strings.Replace(options, ",", " ", -1)
|
|
||||||
options = strings.Replace(options, "=", " ", -1)
|
|
||||||
|
|
||||||
short := ""
|
|
||||||
long := ""
|
|
||||||
argcount := 0
|
|
||||||
var value interface{}
|
|
||||||
value = false
|
|
||||||
|
|
||||||
reDefault := regexp.MustCompile(`(?i)\[default: (.*)\]`)
|
|
||||||
for _, s := range strings.Fields(options) {
|
|
||||||
if strings.HasPrefix(s, "--") {
|
|
||||||
long = s
|
|
||||||
} else if strings.HasPrefix(s, "-") {
|
|
||||||
short = s
|
|
||||||
} else {
|
|
||||||
argcount = 1
|
|
||||||
}
|
|
||||||
if argcount > 0 {
|
|
||||||
matched := reDefault.FindAllStringSubmatch(description, -1)
|
|
||||||
if len(matched) > 0 {
|
|
||||||
value = matched[0][1]
|
|
||||||
} else {
|
|
||||||
value = nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return newOption(short, long, argcount, value)
|
|
||||||
}
|
|
||||||
|
|
||||||
func parseExpr(tokens *tokenList, options *patternList) (patternList, error) {
|
|
||||||
// expr ::= seq ( '|' seq )* ;
|
|
||||||
seq, err := parseSeq(tokens, options)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
if !tokens.current().eq("|") {
|
|
||||||
return seq, nil
|
|
||||||
}
|
|
||||||
var result patternList
|
|
||||||
if len(seq) > 1 {
|
|
||||||
result = patternList{newRequired(seq...)}
|
|
||||||
} else {
|
|
||||||
result = seq
|
|
||||||
}
|
|
||||||
for tokens.current().eq("|") {
|
|
||||||
tokens.move()
|
|
||||||
seq, err = parseSeq(tokens, options)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
if len(seq) > 1 {
|
|
||||||
result = append(result, newRequired(seq...))
|
|
||||||
} else {
|
|
||||||
result = append(result, seq...)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if len(result) > 1 {
|
|
||||||
return patternList{newEither(result...)}, nil
|
|
||||||
}
|
|
||||||
return result, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func parseSeq(tokens *tokenList, options *patternList) (patternList, error) {
|
|
||||||
// seq ::= ( atom [ '...' ] )* ;
|
|
||||||
result := patternList{}
|
|
||||||
for !tokens.current().match(true, "]", ")", "|") {
|
|
||||||
atom, err := parseAtom(tokens, options)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
if tokens.current().eq("...") {
|
|
||||||
atom = patternList{newOneOrMore(atom...)}
|
|
||||||
tokens.move()
|
|
||||||
}
|
|
||||||
result = append(result, atom...)
|
|
||||||
}
|
|
||||||
return result, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func parseAtom(tokens *tokenList, options *patternList) (patternList, error) {
|
|
||||||
// atom ::= '(' expr ')' | '[' expr ']' | 'options' | long | shorts | argument | command ;
|
|
||||||
tok := tokens.current()
|
|
||||||
result := patternList{}
|
|
||||||
if tokens.current().match(false, "(", "[") {
|
|
||||||
tokens.move()
|
|
||||||
var matching string
|
|
||||||
pl, err := parseExpr(tokens, options)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
if tok.eq("(") {
|
|
||||||
matching = ")"
|
|
||||||
result = patternList{newRequired(pl...)}
|
|
||||||
} else if tok.eq("[") {
|
|
||||||
matching = "]"
|
|
||||||
result = patternList{newOptional(pl...)}
|
|
||||||
}
|
|
||||||
moved := tokens.move()
|
|
||||||
if !moved.eq(matching) {
|
|
||||||
return nil, tokens.errorFunc("unmatched '%s', expected: '%s' got: '%s'", tok, matching, moved)
|
|
||||||
}
|
|
||||||
return result, nil
|
|
||||||
} else if tok.eq("options") {
|
|
||||||
tokens.move()
|
|
||||||
return patternList{newOptionsShortcut()}, nil
|
|
||||||
} else if tok.hasPrefix("--") && !tok.eq("--") {
|
|
||||||
return parseLong(tokens, options)
|
|
||||||
} else if tok.hasPrefix("-") && !tok.eq("-") && !tok.eq("--") {
|
|
||||||
return parseShorts(tokens, options)
|
|
||||||
} else if tok.hasPrefix("<") && tok.hasSuffix(">") || tok.isUpper() {
|
|
||||||
return patternList{newArgument(tokens.move().String(), nil)}, nil
|
|
||||||
}
|
|
||||||
return patternList{newCommand(tokens.move().String(), false)}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func parseLong(tokens *tokenList, options *patternList) (patternList, error) {
|
|
||||||
// long ::= '--' chars [ ( ' ' | '=' ) chars ] ;
|
|
||||||
long, eq, v := stringPartition(tokens.move().String(), "=")
|
|
||||||
var value interface{}
|
|
||||||
var opt *pattern
|
|
||||||
if eq == "" && v == "" {
|
|
||||||
value = nil
|
|
||||||
} else {
|
|
||||||
value = v
|
|
||||||
}
|
|
||||||
|
|
||||||
if !strings.HasPrefix(long, "--") {
|
|
||||||
return nil, newError("long option '%s' doesn't start with --", long)
|
|
||||||
}
|
|
||||||
similar := patternList{}
|
|
||||||
for _, o := range *options {
|
|
||||||
if o.long == long {
|
|
||||||
similar = append(similar, o)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if tokens.err == errorUser && len(similar) == 0 { // if no exact match
|
|
||||||
similar = patternList{}
|
|
||||||
for _, o := range *options {
|
|
||||||
if strings.HasPrefix(o.long, long) {
|
|
||||||
similar = append(similar, o)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if len(similar) > 1 { // might be simply specified ambiguously 2+ times?
|
|
||||||
similarLong := make([]string, len(similar))
|
|
||||||
for i, s := range similar {
|
|
||||||
similarLong[i] = s.long
|
|
||||||
}
|
|
||||||
return nil, tokens.errorFunc("%s is not a unique prefix: %s?", long, strings.Join(similarLong, ", "))
|
|
||||||
} else if len(similar) < 1 {
|
|
||||||
argcount := 0
|
|
||||||
if eq == "=" {
|
|
||||||
argcount = 1
|
|
||||||
}
|
|
||||||
opt = newOption("", long, argcount, false)
|
|
||||||
*options = append(*options, opt)
|
|
||||||
if tokens.err == errorUser {
|
|
||||||
var val interface{}
|
|
||||||
if argcount > 0 {
|
|
||||||
val = value
|
|
||||||
} else {
|
|
||||||
val = true
|
|
||||||
}
|
|
||||||
opt = newOption("", long, argcount, val)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
opt = newOption(similar[0].short, similar[0].long, similar[0].argcount, similar[0].value)
|
|
||||||
if opt.argcount == 0 {
|
|
||||||
if value != nil {
|
|
||||||
return nil, tokens.errorFunc("%s must not have an argument", opt.long)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if value == nil {
|
|
||||||
if tokens.current().match(true, "--") {
|
|
||||||
return nil, tokens.errorFunc("%s requires argument", opt.long)
|
|
||||||
}
|
|
||||||
moved := tokens.move()
|
|
||||||
if moved != nil {
|
|
||||||
value = moved.String() // only set as string if not nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if tokens.err == errorUser {
|
|
||||||
if value != nil {
|
|
||||||
opt.value = value
|
|
||||||
} else {
|
|
||||||
opt.value = true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return patternList{opt}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func parseShorts(tokens *tokenList, options *patternList) (patternList, error) {
|
|
||||||
// shorts ::= '-' ( chars )* [ [ ' ' ] chars ] ;
|
|
||||||
tok := tokens.move()
|
|
||||||
if !tok.hasPrefix("-") || tok.hasPrefix("--") {
|
|
||||||
return nil, newError("short option '%s' doesn't start with -", tok)
|
|
||||||
}
|
|
||||||
left := strings.TrimLeft(tok.String(), "-")
|
|
||||||
parsed := patternList{}
|
|
||||||
for left != "" {
|
|
||||||
var opt *pattern
|
|
||||||
short := "-" + left[0:1]
|
|
||||||
left = left[1:]
|
|
||||||
similar := patternList{}
|
|
||||||
for _, o := range *options {
|
|
||||||
if o.short == short {
|
|
||||||
similar = append(similar, o)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if len(similar) > 1 {
|
|
||||||
return nil, tokens.errorFunc("%s is specified ambiguously %d times", short, len(similar))
|
|
||||||
} else if len(similar) < 1 {
|
|
||||||
opt = newOption(short, "", 0, false)
|
|
||||||
*options = append(*options, opt)
|
|
||||||
if tokens.err == errorUser {
|
|
||||||
opt = newOption(short, "", 0, true)
|
|
||||||
}
|
|
||||||
} else { // why copying is necessary here?
|
|
||||||
opt = newOption(short, similar[0].long, similar[0].argcount, similar[0].value)
|
|
||||||
var value interface{}
|
|
||||||
if opt.argcount > 0 {
|
|
||||||
if left == "" {
|
|
||||||
if tokens.current().match(true, "--") {
|
|
||||||
return nil, tokens.errorFunc("%s requires argument", short)
|
|
||||||
}
|
|
||||||
value = tokens.move().String()
|
|
||||||
} else {
|
|
||||||
value = left
|
|
||||||
left = ""
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if tokens.err == errorUser {
|
|
||||||
if value != nil {
|
|
||||||
opt.value = value
|
|
||||||
} else {
|
|
||||||
opt.value = true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
parsed = append(parsed, opt)
|
|
||||||
}
|
|
||||||
return parsed, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func formalUsage(section string) (string, error) {
|
|
||||||
_, _, section = stringPartition(section, ":") // drop "usage:"
|
|
||||||
pu := strings.Fields(section)
|
|
||||||
|
|
||||||
if len(pu) == 0 {
|
|
||||||
return "", newLanguageError("no fields found in usage (perhaps a spacing error).")
|
|
||||||
}
|
|
||||||
|
|
||||||
result := "( "
|
|
||||||
for _, s := range pu[1:] {
|
|
||||||
if s == pu[0] {
|
|
||||||
result += ") | ( "
|
|
||||||
} else {
|
|
||||||
result += s + " "
|
|
||||||
}
|
|
||||||
}
|
|
||||||
result += ")"
|
|
||||||
|
|
||||||
return result, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func extras(help bool, version string, options patternList, doc string) string {
|
|
||||||
if help {
|
|
||||||
for _, o := range options {
|
|
||||||
if (o.name == "-h" || o.name == "--help") && o.value == true {
|
|
||||||
return strings.Trim(doc, "\n")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if version != "" {
|
|
||||||
for _, o := range options {
|
|
||||||
if (o.name == "--version") && o.value == true {
|
|
||||||
return version
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
|
|
||||||
func stringPartition(s, sep string) (string, string, string) {
|
|
||||||
sepPos := strings.Index(s, sep)
|
|
||||||
if sepPos == -1 { // no seperator found
|
|
||||||
return s, "", ""
|
|
||||||
}
|
|
||||||
split := strings.SplitN(s, sep, 2)
|
|
||||||
return split[0], sep, split[1]
|
|
||||||
}
|
|
||||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user