mirror of
https://github.com/cheat/cheat.git
synced 2026-03-07 11:13:33 +01:00
Bug fixes: - Fix inverted pager detection logic (returned error instead of path) - Fix repo.Clone ignoring destination directory parameter - Fix sheet loading using append on pre-sized slices - Clean up partial files on copy failure - Trim whitespace from editor config Security: - Add path traversal protection for cheatsheet names Performance: - Move regex compilation outside search loop - Replace string concatenation with strings.Join in search Build: - Remove go:generate; embed config and usage as string literals - Parallelize release builds - Add fuzz testing infrastructure Testing: - Improve test coverage from 38.9% to 50.2% - Add fuzz tests for search, filter, tags, and validation Documentation: - Fix inaccurate code examples in HACKING.md - Add missing --conf and --all options to man page - Add ADRs for path traversal, env parsing, and search parallelization - Update CONTRIBUTING.md to reflect project policy Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
54 lines
1.3 KiB
Go
54 lines
1.3 KiB
Go
package sheet
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
"path/filepath"
|
|
)
|
|
|
|
// Copy copies a cheatsheet to a new location
|
|
func (s *Sheet) Copy(dest string) error {
|
|
|
|
// NB: while the `infile` has already been loaded and parsed into a `sheet`
|
|
// struct, we're going to read it again here. This is a bit wasteful, but
|
|
// necessary if we want the "raw" file contents (including the front-matter).
|
|
// This is because the frontmatter is parsed and then discarded when the file
|
|
// is loaded via `sheets.Load`.
|
|
infile, err := os.Open(s.Path)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to open cheatsheet: %s, %v", s.Path, err)
|
|
}
|
|
defer infile.Close()
|
|
|
|
// create any necessary subdirectories
|
|
dirs := filepath.Dir(dest)
|
|
if dirs != "." {
|
|
if err := os.MkdirAll(dirs, 0755); err != nil {
|
|
return fmt.Errorf("failed to create directory: %s, %v", dirs, err)
|
|
}
|
|
}
|
|
|
|
// create the outfile
|
|
outfile, err := os.Create(dest)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to create outfile: %s, %v", dest, err)
|
|
}
|
|
defer outfile.Close()
|
|
|
|
// copy file contents
|
|
_, err = io.Copy(outfile, infile)
|
|
if err != nil {
|
|
// Clean up the partially written file on error
|
|
os.Remove(dest)
|
|
return fmt.Errorf(
|
|
"failed to copy file: infile: %s, outfile: %s, err: %v",
|
|
s.Path,
|
|
dest,
|
|
err,
|
|
)
|
|
}
|
|
|
|
return nil
|
|
}
|