MCPcopy
hub / github.com/cheat/cheat / cmdRemove

Function cmdRemove

cmd/cheat/cmd_remove.go:16–63  ·  view source on GitHub ↗

cmdRemove removes (deletes) a cheatsheet.

(cmd *cobra.Command, _ []string, conf config.Config)

Source from the content-addressed store, hash-verified

14
15// cmdRemove removes (deletes) a cheatsheet.
16func cmdRemove(cmd *cobra.Command, _ []string, conf config.Config) {
17
18 cheatsheet, _ := cmd.Flags().GetString("rm")
19
20 // validate the cheatsheet name
21 if err := sheet.Validate(cheatsheet); err != nil {
22 fmt.Fprintf(os.Stderr, "invalid cheatsheet name: %v\n", err)
23 os.Exit(1)
24 }
25
26 // load the cheatsheets
27 cheatsheets, err := sheets.Load(conf.Cheatpaths)
28 if err != nil {
29 fmt.Fprintf(os.Stderr, "failed to list cheatsheets: %v\n", err)
30 os.Exit(1)
31 }
32 if cmd.Flags().Changed("tag") {
33 tagVal, _ := cmd.Flags().GetString("tag")
34 cheatsheets = sheets.Filter(
35 cheatsheets,
36 strings.Split(tagVal, ","),
37 )
38 }
39
40 // consolidate the cheatsheets found on all paths into a single map of
41 // `title` => `sheet` (ie, allow more local cheatsheets to override less
42 // local cheatsheets)
43 consolidated := sheets.Consolidate(cheatsheets)
44
45 // fail early if the requested cheatsheet does not exist
46 sheet, ok := consolidated[cheatsheet]
47 if !ok {
48 fmt.Fprintf(os.Stderr, "No cheatsheet found for '%s'.\n", cheatsheet)
49 os.Exit(2)
50 }
51
52 // fail early if the sheet is read-only
53 if sheet.ReadOnly {
54 fmt.Fprintf(os.Stderr, "cheatsheet '%s' is read-only.\n", cheatsheet)
55 os.Exit(1)
56 }
57
58 // otherwise, attempt to delete the sheet
59 if err := os.Remove(sheet.Path); err != nil {
60 fmt.Fprintf(os.Stderr, "failed to delete sheet: %s, %v\n", sheet.Title, err)
61 os.Exit(1)
62 }
63}

Callers 1

runFunction · 0.85

Calls 4

ValidateFunction · 0.92
LoadFunction · 0.92
FilterFunction · 0.92
ConsolidateFunction · 0.92

Tested by

no test coverage detected