chore(io): Migrate internal/cmd/docs/* to Medium abstraction
Fixes #113
This commit is contained in:
parent
61fd51f7fc
commit
2a68bcd5da
5 changed files with 31 additions and 20 deletions
BIN
core-test
BIN
core-test
Binary file not shown.
|
|
@ -9,6 +9,7 @@ import (
|
|||
"github.com/host-uk/core/internal/cmd/workspace"
|
||||
"github.com/host-uk/core/pkg/cli"
|
||||
"github.com/host-uk/core/pkg/i18n"
|
||||
"github.com/host-uk/core/pkg/io"
|
||||
"github.com/host-uk/core/pkg/repos"
|
||||
)
|
||||
|
||||
|
|
@ -93,28 +94,29 @@ func scanRepoDocs(repo *repos.Repo) RepoDocInfo {
|
|||
|
||||
// Check for README.md
|
||||
readme := filepath.Join(repo.Path, "README.md")
|
||||
if _, err := os.Stat(readme); err == nil {
|
||||
if io.Local.IsFile(readme) {
|
||||
info.Readme = readme
|
||||
info.HasDocs = true
|
||||
}
|
||||
|
||||
// Check for CLAUDE.md
|
||||
claudeMd := filepath.Join(repo.Path, "CLAUDE.md")
|
||||
if _, err := os.Stat(claudeMd); err == nil {
|
||||
if io.Local.IsFile(claudeMd) {
|
||||
info.ClaudeMd = claudeMd
|
||||
info.HasDocs = true
|
||||
}
|
||||
|
||||
// Check for CHANGELOG.md
|
||||
changelog := filepath.Join(repo.Path, "CHANGELOG.md")
|
||||
if _, err := os.Stat(changelog); err == nil {
|
||||
if io.Local.IsFile(changelog) {
|
||||
info.Changelog = changelog
|
||||
info.HasDocs = true
|
||||
}
|
||||
|
||||
// Recursively scan docs/ directory for .md files
|
||||
docsDir := filepath.Join(repo.Path, "docs")
|
||||
if _, err := os.Stat(docsDir); err == nil {
|
||||
// Check if directory exists by listing it
|
||||
if _, err := io.Local.List(docsDir); err == nil {
|
||||
filepath.WalkDir(docsDir, func(path string, d fs.DirEntry, err error) error {
|
||||
if err != nil {
|
||||
return nil
|
||||
|
|
@ -137,11 +139,3 @@ func scanRepoDocs(repo *repos.Repo) RepoDocInfo {
|
|||
|
||||
return info
|
||||
}
|
||||
|
||||
func copyFile(src, dst string) error {
|
||||
data, err := os.ReadFile(src)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return os.WriteFile(dst, data, 0644)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,12 +1,12 @@
|
|||
package docs
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/host-uk/core/pkg/cli"
|
||||
"github.com/host-uk/core/pkg/i18n"
|
||||
"github.com/host-uk/core/pkg/io"
|
||||
)
|
||||
|
||||
// Flag variables for sync command
|
||||
|
|
@ -127,9 +127,9 @@ func runDocsSync(registryPath string, outputDir string, dryRun bool) error {
|
|||
repoOutDir := filepath.Join(outputDir, outName)
|
||||
|
||||
// Clear existing directory
|
||||
os.RemoveAll(repoOutDir)
|
||||
io.Local.Delete(repoOutDir) // Recursive delete
|
||||
|
||||
if err := os.MkdirAll(repoOutDir, 0755); err != nil {
|
||||
if err := io.Local.EnsureDir(repoOutDir); err != nil {
|
||||
cli.Print(" %s %s: %s\n", errorStyle.Render("✗"), info.Name, err)
|
||||
continue
|
||||
}
|
||||
|
|
@ -139,8 +139,10 @@ func runDocsSync(registryPath string, outputDir string, dryRun bool) error {
|
|||
for _, f := range info.DocsFiles {
|
||||
src := filepath.Join(docsDir, f)
|
||||
dst := filepath.Join(repoOutDir, f)
|
||||
os.MkdirAll(filepath.Dir(dst), 0755)
|
||||
if err := copyFile(src, dst); err != nil {
|
||||
// Ensure parent dir
|
||||
io.Local.EnsureDir(filepath.Dir(dst))
|
||||
|
||||
if err := io.Copy(io.Local, src, io.Local, dst); err != nil {
|
||||
cli.Print(" %s %s: %s\n", errorStyle.Render("✗"), f, err)
|
||||
}
|
||||
}
|
||||
|
|
|
|||
17
pkg/io/io.go
17
pkg/io/io.go
|
|
@ -2,6 +2,7 @@ package io
|
|||
|
||||
import (
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
coreerr "github.com/host-uk/core/pkg/framework/core"
|
||||
"github.com/host-uk/core/pkg/io/local"
|
||||
|
|
@ -146,10 +147,24 @@ func (m *MockMedium) FileSet(path, content string) error {
|
|||
return m.Write(path, content)
|
||||
}
|
||||
|
||||
// Delete removes a file or empty directory from the mock filesystem.
|
||||
// Delete removes a file or directory recursively from the mock filesystem.
|
||||
func (m *MockMedium) Delete(path string) error {
|
||||
// Delete exact match
|
||||
delete(m.Files, path)
|
||||
delete(m.Dirs, path)
|
||||
|
||||
// Delete all children (naive string prefix check)
|
||||
prefix := path + "/"
|
||||
for k := range m.Files {
|
||||
if strings.HasPrefix(k, prefix) {
|
||||
delete(m.Files, k)
|
||||
}
|
||||
}
|
||||
for k := range m.Dirs {
|
||||
if strings.HasPrefix(k, prefix) {
|
||||
delete(m.Dirs, k)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -168,13 +168,13 @@ func (m *Medium) FileSet(relativePath, content string) error {
|
|||
return m.Write(relativePath, content)
|
||||
}
|
||||
|
||||
// Delete removes a file or empty directory.
|
||||
// Delete removes a file or directory recursively.
|
||||
func (m *Medium) Delete(relativePath string) error {
|
||||
fullPath, err := m.path(relativePath)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return os.Remove(fullPath)
|
||||
return os.RemoveAll(fullPath)
|
||||
}
|
||||
|
||||
// Rename moves or renames a file.
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue