MCPcopy Index your code
hub / github.com/cheat/cheat / Copy

Method Copy

internal/sheet/copy.go:11–53  ·  view source on GitHub ↗

Copy copies a cheatsheet to a new location

(dest string)

Source from the content-addressed store, hash-verified

9
10// Copy copies a cheatsheet to a new location
11func (s *Sheet) Copy(dest string) error {
12
13 // NB: while the `infile` has already been loaded and parsed into a `sheet`
14 // struct, we're going to read it again here. This is a bit wasteful, but
15 // necessary if we want the "raw" file contents (including the front-matter).
16 // This is because the frontmatter is parsed and then discarded when the file
17 // is loaded via `sheets.Load`.
18 infile, err := os.Open(s.Path)
19 if err != nil {
20 return fmt.Errorf("failed to open cheatsheet: %s, %v", s.Path, err)
21 }
22 defer infile.Close()
23
24 // create any necessary subdirectories
25 dirs := filepath.Dir(dest)
26 if dirs != "." {
27 if err := os.MkdirAll(dirs, 0755); err != nil {
28 return fmt.Errorf("failed to create directory: %s, %v", dirs, err)
29 }
30 }
31
32 // create the outfile
33 outfile, err := os.Create(dest)
34 if err != nil {
35 return fmt.Errorf("failed to create outfile: %s, %v", dest, err)
36 }
37 defer outfile.Close()
38
39 // copy file contents
40 _, err = io.Copy(outfile, infile)
41 if err != nil {
42 // Clean up the partially written file on error
43 os.Remove(dest)
44 return fmt.Errorf(
45 "failed to copy file: infile: %s, outfile: %s, err: %v",
46 s.Path,
47 dest,
48 err,
49 )
50 }
51
52 return nil
53}

Callers 7

TestCopyErrorsFunction · 0.95
TestCopyUnreadableSourceFunction · 0.95
TestWriteToPagerFunction · 0.80
TestCopyFlatFunction · 0.80
TestCopyDeepFunction · 0.80
TestPromptFunction · 0.80
cmdEditFunction · 0.80

Calls

no outgoing calls

Tested by 6

TestCopyErrorsFunction · 0.76
TestCopyUnreadableSourceFunction · 0.76
TestWriteToPagerFunction · 0.64
TestCopyFlatFunction · 0.64
TestCopyDeepFunction · 0.64
TestPromptFunction · 0.64