docs
API
docs
packageAPI reference for the docs
package.
Imports
(7)
S
struct
plugin
cmd/rfw/plugins/docs/docs.go:13-15
type plugin struct
Methods
Build
Method
Parameters
raw
json.RawMessage
Returns
error
func (*plugin) Build(raw json.RawMessage) error
{
cfg := struct {
Dir string `json:"dir"`
Dest string `json:"dest"`
}{
Dir: "articles",
Dest: filepath.Join("build", "static"),
}
if len(raw) > 0 {
_ = json.Unmarshal(raw, &cfg)
}
p.src = cfg.Dir
base := filepath.Base(cfg.Dir)
destRoot := filepath.Join(cfg.Dest, base)
return filepath.Walk(cfg.Dir, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
rel, err := filepath.Rel(cfg.Dir, path)
if err != nil {
return err
}
target := filepath.Join(destRoot, rel)
if err := os.MkdirAll(filepath.Dir(target), 0o755); err != nil {
return err
}
in, err := os.Open(path)
if err != nil {
return err
}
defer in.Close()
out, err := os.Create(target)
if err != nil {
return err
}
if _, err := io.Copy(out, in); err != nil {
out.Close()
return err
}
return out.Close()
})
}
ShouldRebuild
Method
Parameters
path
string
Returns
bool
func (*plugin) ShouldRebuild(path string) bool
{
return strings.HasPrefix(path, p.src)
}
Fields
| Name | Type | Description |
|---|---|---|
| src | string |
F
function
init
cmd/rfw/plugins/docs/docs.go:17-17
func init()
{ plugins.Register(&plugin{}) }
F
function
TestBuildAndShouldRebuild
TestBuildAndShouldRebuild ensures the docs plugin copies files from the
source directory to the destination and correctly reports rebuild needs.
Parameters
t
cmd/rfw/plugins/docs/docs_test.go:12-47
func TestBuildAndShouldRebuild(t *testing.T)
{
p := &plugin{}
tmp := t.TempDir()
src := filepath.Join(tmp, "articles")
dest := filepath.Join(tmp, "out")
if err := os.MkdirAll(filepath.Join(src, "a"), 0o755); err != nil {
t.Fatalf("mkdir: %v", err)
}
srcFile := filepath.Join(src, "a", "doc.txt")
if err := os.WriteFile(srcFile, []byte("hello"), 0o644); err != nil {
t.Fatalf("write src: %v", err)
}
cfg := struct {
Dir string `json:"dir"`
Dest string `json:"dest"`
}{Dir: src, Dest: dest}
raw, _ := json.Marshal(cfg)
if err := p.Build(raw); err != nil {
t.Fatalf("Build: %v", err)
}
// File should be copied under dest/<basename>/a/doc.txt
copied := filepath.Join(dest, filepath.Base(src), "a", "doc.txt")
if data, err := os.ReadFile(copied); err != nil || string(data) != "hello" {
t.Fatalf("expected copied file, got %v %q", err, data)
}
if !p.ShouldRebuild(srcFile) {
t.Fatalf("expected ShouldRebuild true for %s", srcFile)
}
if p.ShouldRebuild(filepath.Join(tmp, "other.txt")) {
t.Fatalf("unexpected rebuild for unrelated file")
}
}