feat(clustertool): fix-up command-docs generator

This commit is contained in:
Kjeld Schouten
2024-10-25 22:04:22 +02:00
parent cb56a8a886
commit 1fa26e6123
2 changed files with 43 additions and 8 deletions
+1 -1
View File
@@ -16,7 +16,7 @@ var gendocsCmd = &cobra.Command{
Hidden: true, Hidden: true,
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
outdir := args[0] outdir := args[0]
tmpdir := "./tmp/docs" tmpdir := helper.DocsCache
if err := os.MkdirAll(tmpdir, 0o777); err != nil { if err := os.MkdirAll(tmpdir, 0o777); err != nil {
log.Fatal(err) log.Fatal(err)
+42 -7
View File
@@ -11,7 +11,6 @@ import (
) )
func ToolDocs(tmpDir string, outputDir string) { func ToolDocs(tmpDir string, outputDir string) {
log.Info().Msg("Starting file processing") log.Info().Msg("Starting file processing")
if err := processFiles(tmpDir, outputDir); err != nil { if err := processFiles(tmpDir, outputDir); err != nil {
@@ -23,12 +22,14 @@ func ToolDocs(tmpDir string, outputDir string) {
return return
} }
renameClustertoolToIndex(outputDir)
log.Info().Msg("File processing completed successfully") log.Info().Msg("File processing completed successfully")
} }
// processFiles reads and processes each file, writing modified content to output directories. // processFiles reads and processes each file, writing modified content to output directories.
func processFiles(tmpdir string, outputDir string) error { func processFiles(tmpDir string, outputDir string) error {
files, err := os.ReadDir(tmpdir) files, err := os.ReadDir(tmpDir)
if err != nil { if err != nil {
return fmt.Errorf("reading input directory: %w", err) return fmt.Errorf("reading input directory: %w", err)
} }
@@ -45,7 +46,7 @@ func processFiles(tmpdir string, outputDir string) error {
log.Info().Str("file", file.Name()).Str("new_path", newPath).Msg("Processing file") log.Info().Str("file", file.Name()).Str("new_path", newPath).Msg("Processing file")
content, err := os.ReadFile(filepath.Join(tmpdir, file.Name())) content, err := os.ReadFile(filepath.Join(tmpDir, file.Name()))
if err != nil { if err != nil {
log.Error().Err(err).Str("file", file.Name()).Msg("Error reading file") log.Error().Err(err).Str("file", file.Name()).Msg("Error reading file")
continue continue
@@ -57,7 +58,7 @@ func processFiles(tmpdir string, outputDir string) error {
continue continue
} }
if err := os.Remove(filepath.Join(tmpdir, file.Name())); err != nil { if err := os.Remove(filepath.Join(tmpDir, file.Name())); err != nil {
log.Error().Err(err).Str("file", file.Name()).Msg("Error deleting original file") log.Error().Err(err).Str("file", file.Name()).Msg("Error deleting original file")
} else { } else {
log.Debug().Str("file", file.Name()).Msg("Original file deleted") log.Debug().Str("file", file.Name()).Msg("Original file deleted")
@@ -83,7 +84,7 @@ func determinePaths(filename string) (subDir, newFileName string) {
return return
} }
// addYamlTitle adds the YAML front matter with title. // addYamlTitle adds the YAML front matter with title and removes the "### SEE ALSO" section.
func addYamlTitle(content []byte, isPrimaryIndex bool) []byte { func addYamlTitle(content []byte, isPrimaryIndex bool) []byte {
scanner := bufio.NewScanner(strings.NewReader(string(content))) scanner := bufio.NewScanner(strings.NewReader(string(content)))
var builder strings.Builder var builder strings.Builder
@@ -98,9 +99,17 @@ func addYamlTitle(content []byte, isPrimaryIndex bool) []byte {
builder.WriteString(scanner.Text() + "\n") builder.WriteString(scanner.Text() + "\n")
log.Debug().Str("title", title).Msg("Set title from first line") log.Debug().Str("title", title).Msg("Set title from first line")
} }
// Add remaining lines until "### SEE ALSO" is found
for scanner.Scan() { for scanner.Scan() {
builder.WriteString(scanner.Text() + "\n") line := scanner.Text()
if strings.HasPrefix(line, "### SEE ALSO") {
log.Debug().Msg("Skipping 'SEE ALSO' section")
break
}
builder.WriteString(line + "\n")
} }
return []byte(builder.String()) return []byte(builder.String())
} }
@@ -112,6 +121,12 @@ func writeToFile(path string, content []byte, file os.DirEntry) error {
if err := os.WriteFile(path, content, file.Type()); err != nil { if err := os.WriteFile(path, content, file.Type()); err != nil {
return fmt.Errorf("writing file to path %s: %w", path, err) return fmt.Errorf("writing file to path %s: %w", path, err)
} }
// Set file permissions to 777
if err := os.Chmod(path, 0777); err != nil {
return fmt.Errorf("setting permissions for path %s: %w", path, err)
}
log.Info().Str("path", path).Msg("File written successfully") log.Info().Str("path", path).Msg("File written successfully")
return nil return nil
} }
@@ -141,3 +156,23 @@ func moveMatchingFilesToSubdirs(outputDir string) error {
} }
return nil return nil
} }
// renameClustertoolToIndex renames clustertool.md to index.md in the given directory.
func renameClustertoolToIndex(dir string) error {
oldPath := filepath.Join(dir, "clustertool.md")
newPath := filepath.Join(dir, "index.md")
// Check if clustertool.md exists
if _, err := os.Stat(oldPath); os.IsNotExist(err) {
log.Warn().Str("file", oldPath).Msg("clustertool.md does not exist")
return nil // No error, just return since file doesn't exist
}
// Rename clustertool.md to index.md
if err := os.Rename(oldPath, newPath); err != nil {
return fmt.Errorf("renaming %s to %s: %w", oldPath, newPath, err)
}
log.Info().Str("old_path", oldPath).Str("new_path", newPath).Msg("File renamed successfully")
return nil
}