File tree Expand file tree Collapse file tree 3 files changed +44
-4
lines changed
Expand file tree Collapse file tree 3 files changed +44
-4
lines changed Original file line number Diff line number Diff line change 33</p >
44
55<p align =" center " >
6- Yo dawg! I heard you liked <code>embed.FS</code>... <br/><br/>
6+ <code >embed.FS</code > wrapper providing additional functionality <br /><br />
77 <a href =" https://github.com/leaanthony/debme/blob/master/LICENSE " ><img src =" https://img.shields.io/badge/License-MIT-blue.svg " ></a >
88 <a href =" https://goreportcard.com/report/github.com/leaanthony/debme " ><img src =" https://goreportcard.com/badge/github.com/leaanthony/debme " /></a >
99 <a href =" https://godoc.org/github.com/leaanthony/debme " ><img src =" https://img.shields.io/badge/godoc-reference-blue.svg " /></a >
1515
1616## Features
1717
18- * Provides a way to get an ` embed.FS ` from an embedded directory
19- * One method: ` FS() `
20- * You can keep calling ` FS() ` , all the way down...
18+ * Get an ` embed.FS ` from an embedded subdirectory
19+ * Handy ` Copy(sourcePath, targetPath) ` method to copy an embedded file to the filesystem
2120 * 100% ` embed.FS ` compatible
2221 * 100% code coverage
2322
@@ -86,6 +85,9 @@ func main() {
8685
8786 println (len (deeperFiles)) // 1
8887 println (files1[0 ].Name ()) // "three.txt"
88+
89+ // Copy files
90+ err := deeper.Copy (" three.txt" , " /path/to/target.txt" )
8991}
9092```
9193
Original file line number Diff line number Diff line change @@ -2,7 +2,9 @@ package debme
22
33import (
44 "embed"
5+ "io"
56 "io/fs"
7+ "os"
68 "path/filepath"
79)
810
@@ -52,3 +54,16 @@ func (d Debme) FS(subDir string) (Debme, error) {
5254 path := d .calculatePath (subDir )
5355 return FS (d .embedFS , path )
5456}
57+
58+ func (d Debme ) CopyFile (sourcePath string , target string , perm os.FileMode ) error {
59+ sourceFile , err := d .Open (sourcePath )
60+ if err != nil {
61+ return err
62+ }
63+ targetFile , err := os .OpenFile (target , os .O_CREATE , perm )
64+ if err != nil {
65+ return err
66+ }
67+ _ , err = io .Copy (targetFile , sourceFile )
68+ return err
69+ }
Original file line number Diff line number Diff line change 66 "github.com/matryer/is"
77 "io"
88 "io/fs"
9+ "os"
910 "testing"
1011)
1112
@@ -93,3 +94,25 @@ func TestFS(t *testing.T) {
9394 _ , err := FS (testfixtures , "baddir" )
9495 is2 .True (err != nil )
9596}
97+
98+ func TestCopy (t * testing.T ) {
99+ is2 := is .New (t )
100+ inner , err := FS (testfixtures , "fixtures/test2/inner" )
101+ is2 .NoErr (err )
102+ err = inner .CopyFile ("one.txt" , "one.txt" , 0644 )
103+ is2 .NoErr (err )
104+ sourceData , err := inner .ReadFile ("one.txt" )
105+ is2 .NoErr (err )
106+ targetData , err := os .ReadFile ("one.txt" )
107+ is2 .NoErr (err )
108+ is2 .Equal (sourceData , targetData )
109+
110+ // Bad source file
111+ err = inner .CopyFile ("one.txtsd" , "one.txt" , 0644 )
112+ is2 .True (err != nil )
113+
114+ // Bad target file
115+ err = inner .CopyFile ("one.txt" , "/:09/one.txt" , 0644 )
116+ is2 .True (err != nil )
117+
118+ }
You can’t perform that action at this time.
0 commit comments