copy
API
copy
packageAPI reference for the copy
package.
Imports
(8)
S
struct
rule
cmd/rfw/plugins/copy/copy.go:14-17
type rule struct
Fields
| Name | Type | Description |
|---|---|---|
| From | string | json:"from" |
| To | string | json:"to" |
S
struct
plugin
cmd/rfw/plugins/copy/copy.go:19-21
type plugin struct
Methods
Build
Method
Parameters
raw
json.RawMessage
Returns
error
func (*plugin) Build(raw json.RawMessage) error
{
cfg := struct {
Files []rule `json:"files"`
}{}
if len(raw) > 0 {
if err := json.Unmarshal(raw, &cfg); err != nil {
return err
}
}
p.rules = cfg.Files
for _, r := range p.rules {
matches, err := doublestar.Glob(os.DirFS("."), r.From)
if err != nil {
return err
}
base, _ := doublestar.SplitPattern(r.From)
base = filepath.FromSlash(base)
for _, m := range matches {
path := filepath.FromSlash(m)
info, err := os.Stat(path)
if err != nil {
return err
}
if info.IsDir() {
continue
}
rel, err := filepath.Rel(base, path)
if err != nil {
return err
}
dst := filepath.Join(r.To, rel)
if err := os.MkdirAll(filepath.Dir(dst), 0o755); err != nil {
return err
}
if err := copyFile(path, dst); err != nil {
return err
}
logging.Log.Info("copied file", logging.F("plugin", "copy"), logging.F("path", dst))
}
}
return nil
}
ShouldRebuild
Method
Parameters
path
string
Returns
bool
func (*plugin) ShouldRebuild(path string) bool
{
for _, r := range p.rules {
if ok, _ := doublestar.PathMatch(r.From, path); ok {
return true
}
}
return false
}
Fields
| Name | Type | Description |
|---|---|---|
| rules | []rule |
F
function
init
cmd/rfw/plugins/copy/copy.go:23-23
func init()
{ plugins.Register(&plugin{}) }
F
function
copyFile
Parameters
src
string
dst
string
Returns
error
cmd/rfw/plugins/copy/copy.go:81-97
func copyFile(src, dst string) error
{
in, err := os.Open(src)
if err != nil {
return err
}
defer in.Close()
out, err := os.Create(dst)
if err != nil {
return err
}
if _, err := io.Copy(out, in); err != nil {
out.Close()
return err
}
return out.Close()
}
F
function
TestBuildAndShouldRebuild
TestBuildAndShouldRebuild ensures files matching patterns are copied
and rebuilds are triggered for matched paths.
Parameters
t
cmd/rfw/plugins/copy/copy_test.go:12-69
func TestBuildAndShouldRebuild(t *testing.T)
{
p := &plugin{}
tmp := t.TempDir()
cwd, _ := os.Getwd()
defer os.Chdir(cwd)
if err := os.Chdir(tmp); err != nil {
t.Fatalf("chdir: %v", err)
}
srcRoot := filepath.Join("examples", "components")
if err := os.MkdirAll(filepath.Join(srcRoot, "templates"), 0o755); err != nil {
t.Fatalf("mkdir: %v", err)
}
fileA := filepath.Join(srcRoot, "comp.txt")
if err := os.WriteFile(fileA, []byte("a"), 0o644); err != nil {
t.Fatalf("write: %v", err)
}
fileB := filepath.Join(srcRoot, "templates", "tpl.txt")
if err := os.WriteFile(fileB, []byte("b"), 0o644); err != nil {
t.Fatalf("write: %v", err)
}
destRoot := filepath.Join("build", "static", "examples", "components")
cfg := struct {
Files []struct {
From string `json:"from"`
To string `json:"to"`
} `json:"files"`
}{
Files: []struct {
From string `json:"from"`
To string `json:"to"`
}{{
From: filepath.Join(srcRoot, "**", "*"),
To: destRoot,
}},
}
raw, _ := json.Marshal(cfg)
if err := p.Build(raw); err != nil {
t.Fatalf("Build: %v", err)
}
absDest := filepath.Join(tmp, destRoot)
if data, err := os.ReadFile(filepath.Join(absDest, "comp.txt")); err != nil || string(data) != "a" {
t.Fatalf("comp.txt not copied: %v %s", err, data)
}
if data, err := os.ReadFile(filepath.Join(absDest, "templates", "tpl.txt")); err != nil || string(data) != "b" {
t.Fatalf("tpl.txt not copied: %v %s", err, data)
}
if !p.ShouldRebuild(fileA) {
t.Fatalf("expected ShouldRebuild true for %s", fileA)
}
if p.ShouldRebuild(filepath.Join("other.txt")) {
t.Fatalf("unexpected rebuild for unrelated file")
}
}