@@ -17,7 +17,9 @@ package clidocstool
1717import (
1818 "errors"
1919 "io"
20+ "io/fs"
2021 "os"
22+ "path/filepath"
2123 "strings"
2224
2325 "github.com/spf13/cobra"
@@ -76,6 +78,40 @@ func (c *Client) GenAllTree() error {
7678 return nil
7779}
7880
81+ // GenAllTreeAndRemoveOldFiles creates all structured ref files for this command and
82+ // all descendants in the directory given then removes potential old documentation files from the origin directory tree.
83+ func (c * Client ) GenAllTreeAndRemoveOldFiles () error {
84+ if err := c .GenAllTree (); err != nil {
85+ return err
86+ }
87+ filesToRemove := make (map [string ]any )
88+ filepath .WalkDir (c .source , func (path string , entry fs.DirEntry , err error ) error {
89+ return c .checkIfShouldBeRemoved (filesToRemove , path , entry , err )
90+ })
91+ for file , _ := range filesToRemove {
92+ if err := os .Remove (file ); err != nil {
93+ return err
94+ }
95+ }
96+ return nil
97+ }
98+
99+ func (c * Client ) checkIfShouldBeRemoved (filesToRemove map [string ]any , path string , entry fs.DirEntry , err error ) error {
100+ if err != nil {
101+ return err
102+ }
103+ if ! entry .IsDir () {
104+ if _ , err := entry .Info (); err != nil {
105+ return err
106+ }
107+ targetFile := filepath .Join (c .target , strings .ReplaceAll (path , c .source , "" ))
108+ if _ , err := os .Stat (targetFile ); os .IsNotExist (err ) {
109+ filesToRemove [path ] = struct {}{}
110+ }
111+ }
112+ return nil
113+ }
114+
79115func fileExists (f string ) bool {
80116 info , err := os .Stat (f )
81117 if os .IsNotExist (err ) {
0 commit comments