|
| 1 | +//go:build !windows |
| 2 | +// +build !windows |
| 3 | + |
| 4 | +package manpages |
| 5 | + |
| 6 | +import ( |
| 7 | + "compress/gzip" |
| 8 | + "fmt" |
| 9 | + "io" |
| 10 | + "os" |
| 11 | + "path/filepath" |
| 12 | + "strings" |
| 13 | + |
| 14 | + "github.com/spf13/cobra/doc" |
| 15 | + |
| 16 | + "github.com/crc-org/crc/v2/pkg/crc/logging" |
| 17 | +) |
| 18 | + |
| 19 | +var ( |
| 20 | + rootCrcManPage = "crc.1.gz" |
| 21 | + osEnvGetter = os.Getenv |
| 22 | + osEnvSetter = os.Setenv |
| 23 | + ManPathEnvironmentVariable = "MANPATH" |
| 24 | + CrcManPageHeader = &doc.GenManHeader{ |
| 25 | + Title: "CRC", |
| 26 | + Section: "1", |
| 27 | + } |
| 28 | +) |
| 29 | + |
| 30 | +// GenerateManPages generates manual pages for user commands and places them |
| 31 | +// in the specified target directory. It performs the following steps: |
| 32 | +// |
| 33 | +// 1. Checks if the man pages should be generated based on the target folder. |
| 34 | +// 2. Creates the necessary directory structure if it does not exist. |
| 35 | +// 3. Generates man pages in a temporary directory. |
| 36 | +// 4. Compresses the generated man pages and moves them to the target folder. |
| 37 | +// 5. Updates the MANPATH environment variable to include the target directory. |
| 38 | +// 6. Cleans up the temporary directory. |
| 39 | +// |
| 40 | +// manPageGenerator: Function that generates man pages in the specified directory. |
| 41 | +// targetDir: Directory where the generated man pages should be placed. |
| 42 | +// |
| 43 | +// Returns an error if any step in the process fails. |
| 44 | +func GenerateManPages(manPageGenerator func(targetDir string) error, targetDir string) error { |
| 45 | + manUserCommandTargetFolder := filepath.Join(targetDir, "man1") |
| 46 | + if !manPagesAlreadyGenerated(manUserCommandTargetFolder) { |
| 47 | + if _, err := os.Stat(manUserCommandTargetFolder); os.IsNotExist(err) { |
| 48 | + err = os.MkdirAll(manUserCommandTargetFolder, 0755) |
| 49 | + if err != nil { |
| 50 | + logging.Errorf("error in creating dir for man pages: %s", err.Error()) |
| 51 | + } |
| 52 | + } |
| 53 | + temporaryManPagesDir, err := generateManPagesInTemporaryDirectory(manPageGenerator) |
| 54 | + if err != nil { |
| 55 | + return err |
| 56 | + } |
| 57 | + err = compressManPages(temporaryManPagesDir, manUserCommandTargetFolder) |
| 58 | + if err != nil { |
| 59 | + return fmt.Errorf("error in compressing man pages: %s", err.Error()) |
| 60 | + } |
| 61 | + err = appendToManPathEnvironmentVariable(targetDir) |
| 62 | + if err != nil { |
| 63 | + return fmt.Errorf("error updating MANPATH environment variable: %s", err.Error()) |
| 64 | + } |
| 65 | + err = os.RemoveAll(temporaryManPagesDir) |
| 66 | + if err != nil { |
| 67 | + return fmt.Errorf("error removing temporary man pages directory: %s", err.Error()) |
| 68 | + } |
| 69 | + } |
| 70 | + return nil |
| 71 | +} |
| 72 | + |
| 73 | +func appendToManPathEnvironmentVariable(folder string) error { |
| 74 | + manPath := osEnvGetter(ManPathEnvironmentVariable) |
| 75 | + if !manPathAlreadyContains(folder, manPath) { |
| 76 | + if manPath == "" { |
| 77 | + manPath = folder |
| 78 | + } else { |
| 79 | + manPath = fmt.Sprintf("%s%c%s", manPath, os.PathListSeparator, folder) |
| 80 | + } |
| 81 | + err := osEnvSetter(ManPathEnvironmentVariable, manPath) |
| 82 | + if err != nil { |
| 83 | + return err |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + return nil |
| 88 | +} |
| 89 | + |
| 90 | +func manPathAlreadyContains(manPathEnvVarValue string, folder string) bool { |
| 91 | + manDirs := strings.Split(manPathEnvVarValue, string(os.PathListSeparator)) |
| 92 | + for _, manDir := range manDirs { |
| 93 | + if manDir == folder { |
| 94 | + return true |
| 95 | + } |
| 96 | + } |
| 97 | + return false |
| 98 | +} |
| 99 | + |
| 100 | +func removeFromManPathEnvironmentVariable(manPathEnvVarValue string, folder string) error { |
| 101 | + manDirs := strings.Split(manPathEnvVarValue, string(os.PathListSeparator)) |
| 102 | + var updatedManPathEnvVarValues []string |
| 103 | + for _, manDir := range manDirs { |
| 104 | + if manDir != folder { |
| 105 | + updatedManPathEnvVarValues = append(updatedManPathEnvVarValues, manDir) |
| 106 | + } |
| 107 | + } |
| 108 | + return osEnvSetter(ManPathEnvironmentVariable, strings.Join(updatedManPathEnvVarValues, string(os.PathListSeparator))) |
| 109 | +} |
| 110 | + |
| 111 | +func generateManPagesInTemporaryDirectory(manPageGenerator func(targetDir string) error) (string, error) { |
| 112 | + tempDir, err := os.MkdirTemp("", "crc-manpages") |
| 113 | + if err != nil { |
| 114 | + return "", err |
| 115 | + } |
| 116 | + manPagesGenerationErr := manPageGenerator(tempDir) |
| 117 | + if manPagesGenerationErr != nil { |
| 118 | + return "", manPagesGenerationErr |
| 119 | + } |
| 120 | + logging.Debugf("Successfully generated manpages in %s", tempDir) |
| 121 | + return tempDir, nil |
| 122 | +} |
| 123 | + |
| 124 | +func compressManPages(manPagesSourceFolder string, manPagesTargetFolder string) error { |
| 125 | + return filepath.Walk(manPagesSourceFolder, func(path string, info os.FileInfo, err error) error { |
| 126 | + if err != nil { |
| 127 | + return err |
| 128 | + } |
| 129 | + |
| 130 | + if info.IsDir() { |
| 131 | + return nil |
| 132 | + } |
| 133 | + |
| 134 | + srcFile, err := os.Open(path) |
| 135 | + if err != nil { |
| 136 | + return err |
| 137 | + } |
| 138 | + defer srcFile.Close() |
| 139 | + |
| 140 | + compressedFilePath := filepath.Join(manPagesTargetFolder, info.Name()+".gz") |
| 141 | + compressedFile, err := os.Create(compressedFilePath) |
| 142 | + if err != nil { |
| 143 | + return err |
| 144 | + } |
| 145 | + defer compressedFile.Close() |
| 146 | + |
| 147 | + gzipWriter := gzip.NewWriter(compressedFile) |
| 148 | + defer gzipWriter.Close() |
| 149 | + |
| 150 | + _, err = io.Copy(gzipWriter, srcFile) |
| 151 | + if err != nil { |
| 152 | + return err |
| 153 | + } |
| 154 | + return nil |
| 155 | + }) |
| 156 | +} |
| 157 | + |
| 158 | +func manPagesAlreadyGenerated(manPagesTargetFolder string) bool { |
| 159 | + rootCrcManPageFilePath := filepath.Join(manPagesTargetFolder, rootCrcManPage) |
| 160 | + if _, err := os.Stat(rootCrcManPageFilePath); os.IsNotExist(err) { |
| 161 | + return false |
| 162 | + } |
| 163 | + return true |
| 164 | +} |
| 165 | + |
| 166 | +func RemoveCrcManPages(manPageDir string) error { |
| 167 | + manUserCommandTargetFolder := filepath.Join(manPageDir, "man1") |
| 168 | + err := filepath.Walk(manUserCommandTargetFolder, func(path string, info os.FileInfo, err error) error { |
| 169 | + if err != nil { |
| 170 | + return err |
| 171 | + } |
| 172 | + if !info.IsDir() && filepath.Base(path)[:len("crc")] == "crc" { |
| 173 | + err = os.Remove(path) |
| 174 | + if err != nil { |
| 175 | + return err |
| 176 | + } |
| 177 | + } |
| 178 | + return nil |
| 179 | + }) |
| 180 | + if err != nil { |
| 181 | + return err |
| 182 | + } |
| 183 | + return removeFromManPathEnvironmentVariable(osEnvGetter(ManPathEnvironmentVariable), manPageDir) |
| 184 | +} |
0 commit comments