github.com/lmorg/murex@v0.0.0-20240217211045-e081c89cd4ef/utils/docgen/api/filesystem.go (about)

     1  package docgen
     2  
     3  import (
     4  	"io"
     5  	"os"
     6  	"path/filepath"
     7  	"strings"
     8  )
     9  
    10  func fileReader(path string) *os.File {
    11  	f, err := os.OpenFile(path, os.O_RDONLY, 0)
    12  	if err != nil {
    13  		panic(err.Error())
    14  	}
    15  	return f
    16  }
    17  
    18  func readAll(f *os.File) []byte {
    19  	b, err := io.ReadAll(f)
    20  	if err != nil {
    21  		panic(err.Error())
    22  	}
    23  	return b
    24  }
    25  
    26  func makePath(path string) {
    27  	err := os.MkdirAll(path, 0755)
    28  	if err != nil {
    29  		panic(err)
    30  	}
    31  }
    32  
    33  func walkSourcePath(path string) {
    34  	err := filepath.Walk(path, walkCallback)
    35  	if err != nil {
    36  		panic(err.Error())
    37  	}
    38  }
    39  
    40  func walkCallback(path string, f os.FileInfo, err error) error {
    41  	if err != nil {
    42  		panic(err.Error())
    43  	}
    44  
    45  	// We are not interested in anything that isn't a source file
    46  	if !strings.HasSuffix(f.Name(), Config.SourceExt) {
    47  		return nil
    48  	}
    49  
    50  	log("Reading", path)
    51  
    52  	var src []document
    53  	parseSourceFile(path, &src)
    54  
    55  	for i := range src {
    56  		src[i].SourcePath = path
    57  	}
    58  
    59  	Documents = append(Documents, src...)
    60  
    61  	return nil
    62  }