Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add public function to generate docs and remove old files #43

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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