Skip to content

Commit

Permalink
add public function to remove old doc files
Browse files Browse the repository at this point in the history
Signed-off-by: Guillaume Lours <[email protected]>
  • Loading branch information
glours committed Nov 2, 2023
1 parent 3895c94 commit 7470ed4
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions clidocstool.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ package clidocstool
import (
"errors"
"io"
"io/fs"
"os"
"path/filepath"
"strings"

"github.com/spf13/cobra"
Expand Down Expand Up @@ -76,6 +78,40 @@ func (c *Client) GenAllTree() error {
return nil
}

// GenAllTreeAndRemoveOldFiles creates all structured ref files for this command and
// all descendants in the directory given then removes potential old documentation files from the origin directory tree.
func (c *Client) GenAllTreeAndRemoveOldFiles() error {
if err := c.GenAllTree(); err != nil {
return err
}
filesToRemove := make(map[string]any)
filepath.WalkDir(c.source, func(path string, entry fs.DirEntry, err error) error {
return c.checkIfShouldBeRemoved(filesToRemove, path, entry, err)
})
for file := range filesToRemove {
if err := os.Remove(file); err != nil {
return err
}
}
return nil
}

func (c *Client) checkIfShouldBeRemoved(filesToRemove map[string]any, path string, entry fs.DirEntry, err error) error {
if err != nil {
return err
}
if !entry.IsDir() {
if _, err := entry.Info(); err != nil {
return err
}
targetFile := filepath.Join(c.target, strings.ReplaceAll(path, c.source, ""))
if _, err := os.Stat(targetFile); os.IsNotExist(err) {
filesToRemove[path] = struct{}{}
}
}
return nil
}

func fileExists(f string) bool {
info, err := os.Stat(f)
if os.IsNotExist(err) {
Expand Down

0 comments on commit 7470ed4

Please sign in to comment.