chore(io): Migrate internal/cmd/docs/* to Medium abstraction
Fixes #113
This commit is contained in:
parent
e91481c285
commit
aaa7739749
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/internal/cmd/workspace"
|
||||||
"github.com/host-uk/core/pkg/cli"
|
"github.com/host-uk/core/pkg/cli"
|
||||||
"github.com/host-uk/core/pkg/i18n"
|
"github.com/host-uk/core/pkg/i18n"
|
||||||
|
"github.com/host-uk/core/pkg/io"
|
||||||
"github.com/host-uk/core/pkg/repos"
|
"github.com/host-uk/core/pkg/repos"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -93,28 +94,29 @@ func scanRepoDocs(repo *repos.Repo) RepoDocInfo {
|
||||||
|
|
||||||
// Check for README.md
|
// Check for README.md
|
||||||
readme := filepath.Join(repo.Path, "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.Readme = readme
|
||||||
info.HasDocs = true
|
info.HasDocs = true
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check for CLAUDE.md
|
// Check for CLAUDE.md
|
||||||
claudeMd := filepath.Join(repo.Path, "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.ClaudeMd = claudeMd
|
||||||
info.HasDocs = true
|
info.HasDocs = true
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check for CHANGELOG.md
|
// Check for CHANGELOG.md
|
||||||
changelog := filepath.Join(repo.Path, "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.Changelog = changelog
|
||||||
info.HasDocs = true
|
info.HasDocs = true
|
||||||
}
|
}
|
||||||
|
|
||||||
// Recursively scan docs/ directory for .md files
|
// Recursively scan docs/ directory for .md files
|
||||||
docsDir := filepath.Join(repo.Path, "docs")
|
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 {
|
filepath.WalkDir(docsDir, func(path string, d fs.DirEntry, err error) error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil
|
return nil
|
||||||
|
|
@ -137,11 +139,3 @@ func scanRepoDocs(repo *repos.Repo) RepoDocInfo {
|
||||||
|
|
||||||
return info
|
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
|
package docs
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/host-uk/core/pkg/cli"
|
"github.com/host-uk/core/pkg/cli"
|
||||||
"github.com/host-uk/core/pkg/i18n"
|
"github.com/host-uk/core/pkg/i18n"
|
||||||
|
"github.com/host-uk/core/pkg/io"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Flag variables for sync command
|
// Flag variables for sync command
|
||||||
|
|
@ -127,9 +127,9 @@ func runDocsSync(registryPath string, outputDir string, dryRun bool) error {
|
||||||
repoOutDir := filepath.Join(outputDir, outName)
|
repoOutDir := filepath.Join(outputDir, outName)
|
||||||
|
|
||||||
// Clear existing directory
|
// 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)
|
cli.Print(" %s %s: %s\n", errorStyle.Render("✗"), info.Name, err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
@ -139,8 +139,10 @@ func runDocsSync(registryPath string, outputDir string, dryRun bool) error {
|
||||||
for _, f := range info.DocsFiles {
|
for _, f := range info.DocsFiles {
|
||||||
src := filepath.Join(docsDir, f)
|
src := filepath.Join(docsDir, f)
|
||||||
dst := filepath.Join(repoOutDir, f)
|
dst := filepath.Join(repoOutDir, f)
|
||||||
os.MkdirAll(filepath.Dir(dst), 0755)
|
// Ensure parent dir
|
||||||
if err := copyFile(src, dst); err != nil {
|
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)
|
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 (
|
import (
|
||||||
"os"
|
"os"
|
||||||
|
"strings"
|
||||||
|
|
||||||
coreerr "github.com/host-uk/core/pkg/framework/core"
|
coreerr "github.com/host-uk/core/pkg/framework/core"
|
||||||
"github.com/host-uk/core/pkg/io/local"
|
"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)
|
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 {
|
func (m *MockMedium) Delete(path string) error {
|
||||||
|
// Delete exact match
|
||||||
delete(m.Files, path)
|
delete(m.Files, path)
|
||||||
delete(m.Dirs, 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
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -168,13 +168,13 @@ func (m *Medium) FileSet(relativePath, content string) error {
|
||||||
return m.Write(relativePath, content)
|
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 {
|
func (m *Medium) Delete(relativePath string) error {
|
||||||
fullPath, err := m.path(relativePath)
|
fullPath, err := m.path(relativePath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return os.Remove(fullPath)
|
return os.RemoveAll(fullPath)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Rename moves or renames a file.
|
// Rename moves or renames a file.
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue