mirror of
				https://github.com/cheat/cheat.git
				synced 2025-11-04 07:45:28 +01:00 
			
		
		
		
	feat: --rm and --tags
- Implements the `--rm` command (#483) - Implements the `--tags` command (#484) - Bumps version to `3.1.0`
This commit is contained in:
		
							
								
								
									
										55
									
								
								cmd/cheat/cmd_remove.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										55
									
								
								cmd/cheat/cmd_remove.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,55 @@
 | 
			
		||||
package main
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"fmt"
 | 
			
		||||
	"os"
 | 
			
		||||
	"strings"
 | 
			
		||||
 | 
			
		||||
	"github.com/cheat/cheat/internal/config"
 | 
			
		||||
	"github.com/cheat/cheat/internal/sheets"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// cmdRemove opens a cheatsheet for editing (or creates it if it doesn't exist).
 | 
			
		||||
func cmdRemove(opts map[string]interface{}, conf config.Config) {
 | 
			
		||||
 | 
			
		||||
	cheatsheet := opts["--rm"].(string)
 | 
			
		||||
 | 
			
		||||
	// load the cheatsheets
 | 
			
		||||
	cheatsheets, err := sheets.Load(conf.Cheatpaths)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		fmt.Fprintln(os.Stderr, fmt.Sprintf("failed to list cheatsheets: %v", err))
 | 
			
		||||
		os.Exit(1)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// filter cheatcheats by tag if --tag was provided
 | 
			
		||||
	if opts["--tag"] != nil {
 | 
			
		||||
		cheatsheets = sheets.Filter(
 | 
			
		||||
			cheatsheets,
 | 
			
		||||
			strings.Split(opts["--tag"].(string), ","),
 | 
			
		||||
		)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// consolidate the cheatsheets found on all paths into a single map of
 | 
			
		||||
	// `title` => `sheet` (ie, allow more local cheatsheets to override less
 | 
			
		||||
	// local cheatsheets)
 | 
			
		||||
	consolidated := sheets.Consolidate(cheatsheets)
 | 
			
		||||
 | 
			
		||||
	// fail early if the requested cheatsheet does not exist
 | 
			
		||||
	sheet, ok := consolidated[cheatsheet]
 | 
			
		||||
	if !ok {
 | 
			
		||||
		fmt.Fprintln(os.Stderr, fmt.Sprintf("no cheatsheet found for '%s'.\n", cheatsheet))
 | 
			
		||||
		os.Exit(1)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// fail early if the sheet is read-only
 | 
			
		||||
	if sheet.ReadOnly {
 | 
			
		||||
		fmt.Fprintln(os.Stderr, fmt.Sprintf("cheatsheet '%s' is read-only.", cheatsheet))
 | 
			
		||||
		os.Exit(1)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	// otherwise, attempt to delete the sheet
 | 
			
		||||
	if err := os.Remove(sheet.Path); err != nil {
 | 
			
		||||
		fmt.Fprintln(os.Stderr, fmt.Sprintf("failed to delete sheet: %s, %v", sheet.Title, err))
 | 
			
		||||
		os.Exit(1)
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
@@ -13,6 +13,7 @@ Options:
 | 
			
		||||
  -t --tag=<tag>        Return only sheets matching <tag>
 | 
			
		||||
  -T --tags             List all tags in use
 | 
			
		||||
  -v --version          Print the version number
 | 
			
		||||
  --rm=<sheet>          Remove (delete) <sheet>
 | 
			
		||||
 | 
			
		||||
Examples:
 | 
			
		||||
 | 
			
		||||
@@ -45,3 +46,6 @@ Examples:
 | 
			
		||||
 | 
			
		||||
  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
 | 
			
		||||
 
 | 
			
		||||
@@ -13,7 +13,7 @@ import (
 | 
			
		||||
	"github.com/cheat/cheat/internal/config"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
const version = "3.0.7"
 | 
			
		||||
const version = "3.1.0"
 | 
			
		||||
 | 
			
		||||
func main() {
 | 
			
		||||
 | 
			
		||||
@@ -82,6 +82,9 @@ func main() {
 | 
			
		||||
	case opts["--search"] != nil:
 | 
			
		||||
		cmd = cmdSearch
 | 
			
		||||
 | 
			
		||||
	case opts["--rm"] != nil:
 | 
			
		||||
		cmd = cmdRemove
 | 
			
		||||
 | 
			
		||||
	case opts["<cheatsheet>"] != nil:
 | 
			
		||||
		cmd = cmdView
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -22,6 +22,7 @@ Options:
 | 
			
		||||
  -t --tag=<tag>        Return only sheets matching <tag>
 | 
			
		||||
  -T --tags             List all tags in use
 | 
			
		||||
  -v --version          Print the version number
 | 
			
		||||
  --rm=<sheet>          Remove (delete) <sheet>
 | 
			
		||||
 | 
			
		||||
Examples:
 | 
			
		||||
 | 
			
		||||
@@ -43,6 +44,9 @@ Examples:
 | 
			
		||||
  To list all available cheatsheets:
 | 
			
		||||
    cheat -l
 | 
			
		||||
 | 
			
		||||
  To list all tags in use:
 | 
			
		||||
    cheat -T
 | 
			
		||||
 | 
			
		||||
  To list available cheatsheets that are tagged as "personal":
 | 
			
		||||
    cheat -l -t personal
 | 
			
		||||
 | 
			
		||||
@@ -51,5 +55,8 @@ Examples:
 | 
			
		||||
 | 
			
		||||
  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
 | 
			
		||||
`)
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user