-
Notifications
You must be signed in to change notification settings - Fork 243
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
feat : add support for generating manpages for crc cli commands (#4181) #4586
Open
rohanKanojia
wants to merge
1
commit into
crc-org:main
Choose a base branch
from
rohankanojia-forks:pr/issue4181
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package cmd | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestCrcManPageGenerator_WhenInvoked_GeneratesManPagesForAllCrcSubCommands(t *testing.T) { | ||
// Given | ||
dir := t.TempDir() | ||
|
||
// When | ||
err := crcManPageGenerator(dir) | ||
|
||
// Then | ||
assert.NoError(t, err) | ||
files, readErr := os.ReadDir(dir) | ||
assert.NoError(t, readErr) | ||
var manPagesFiles []string | ||
for _, manPage := range files { | ||
manPagesFiles = append(manPagesFiles, manPage.Name()) | ||
} | ||
assert.ElementsMatch(t, []string{ | ||
"crc-bundle-generate.1", | ||
"crc-bundle.1", | ||
"crc-cleanup.1", | ||
"crc-config-get.1", | ||
"crc-config-set.1", | ||
"crc-config-unset.1", | ||
"crc-config-view.1", | ||
"crc-config.1", | ||
"crc-console.1", | ||
"crc-delete.1", | ||
"crc-ip.1", | ||
"crc-oc-env.1", | ||
"crc-podman-env.1", | ||
"crc-setup.1", | ||
"crc-start.1", | ||
"crc-status.1", | ||
"crc-stop.1", | ||
"crc-version.1", | ||
"crc.1", | ||
}, manPagesFiles) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,184 @@ | ||
//go:build !windows | ||
// +build !windows | ||
|
||
package manpages | ||
|
||
import ( | ||
"compress/gzip" | ||
"fmt" | ||
"io" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/spf13/cobra/doc" | ||
|
||
"github.com/crc-org/crc/v2/pkg/crc/logging" | ||
) | ||
|
||
var ( | ||
rootCrcManPage = "crc.1.gz" | ||
osEnvGetter = os.Getenv | ||
osEnvSetter = os.Setenv | ||
ManPathEnvironmentVariable = "MANPATH" | ||
CrcManPageHeader = &doc.GenManHeader{ | ||
Title: "CRC", | ||
Section: "1", | ||
} | ||
) | ||
|
||
// GenerateManPages generates manual pages for user commands and places them | ||
// in the specified target directory. It performs the following steps: | ||
// | ||
// 1. Checks if the man pages should be generated based on the target folder. | ||
// 2. Creates the necessary directory structure if it does not exist. | ||
// 3. Generates man pages in a temporary directory. | ||
// 4. Compresses the generated man pages and moves them to the target folder. | ||
// 5. Updates the MANPATH environment variable to include the target directory. | ||
// 6. Cleans up the temporary directory. | ||
// | ||
// manPageGenerator: Function that generates man pages in the specified directory. | ||
// targetDir: Directory where the generated man pages should be placed. | ||
// | ||
// Returns an error if any step in the process fails. | ||
func GenerateManPages(manPageGenerator func(targetDir string) error, targetDir string) error { | ||
manUserCommandTargetFolder := filepath.Join(targetDir, "man1") | ||
if !manPagesAlreadyGenerated(manUserCommandTargetFolder) { | ||
if _, err := os.Stat(manUserCommandTargetFolder); os.IsNotExist(err) { | ||
err = os.MkdirAll(manUserCommandTargetFolder, 0755) | ||
if err != nil { | ||
logging.Errorf("error in creating dir for man pages: %s", err.Error()) | ||
} | ||
} | ||
temporaryManPagesDir, err := generateManPagesInTemporaryDirectory(manPageGenerator) | ||
if err != nil { | ||
return err | ||
} | ||
err = compressManPages(temporaryManPagesDir, manUserCommandTargetFolder) | ||
if err != nil { | ||
return fmt.Errorf("error in compressing man pages: %s", err.Error()) | ||
} | ||
err = appendToManPathEnvironmentVariable(targetDir) | ||
if err != nil { | ||
return fmt.Errorf("error updating MANPATH environment variable: %s", err.Error()) | ||
} | ||
err = os.RemoveAll(temporaryManPagesDir) | ||
if err != nil { | ||
return fmt.Errorf("error removing temporary man pages directory: %s", err.Error()) | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func appendToManPathEnvironmentVariable(folder string) error { | ||
manPath := osEnvGetter(ManPathEnvironmentVariable) | ||
if !manPathAlreadyContains(folder, manPath) { | ||
if manPath == "" { | ||
manPath = folder | ||
} else { | ||
manPath = fmt.Sprintf("%s%c%s", manPath, os.PathListSeparator, folder) | ||
} | ||
err := osEnvSetter(ManPathEnvironmentVariable, manPath) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func manPathAlreadyContains(manPathEnvVarValue string, folder string) bool { | ||
manDirs := strings.Split(manPathEnvVarValue, string(os.PathListSeparator)) | ||
for _, manDir := range manDirs { | ||
if manDir == folder { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
func removeFromManPathEnvironmentVariable(manPathEnvVarValue string, folder string) error { | ||
manDirs := strings.Split(manPathEnvVarValue, string(os.PathListSeparator)) | ||
var updatedManPathEnvVarValues []string | ||
for _, manDir := range manDirs { | ||
if manDir != folder { | ||
updatedManPathEnvVarValues = append(updatedManPathEnvVarValues, manDir) | ||
} | ||
} | ||
return osEnvSetter(ManPathEnvironmentVariable, strings.Join(updatedManPathEnvVarValues, string(os.PathListSeparator))) | ||
} | ||
|
||
func generateManPagesInTemporaryDirectory(manPageGenerator func(targetDir string) error) (string, error) { | ||
tempDir, err := os.MkdirTemp("", "crc-manpages") | ||
if err != nil { | ||
return "", err | ||
} | ||
manPagesGenerationErr := manPageGenerator(tempDir) | ||
if manPagesGenerationErr != nil { | ||
return "", manPagesGenerationErr | ||
} | ||
logging.Debugf("Successfully generated manpages in %s", tempDir) | ||
return tempDir, nil | ||
} | ||
|
||
func compressManPages(manPagesSourceFolder string, manPagesTargetFolder string) error { | ||
return filepath.Walk(manPagesSourceFolder, func(path string, info os.FileInfo, err error) error { | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if info.IsDir() { | ||
return nil | ||
} | ||
|
||
srcFile, err := os.Open(path) | ||
if err != nil { | ||
return err | ||
} | ||
defer srcFile.Close() | ||
|
||
compressedFilePath := filepath.Join(manPagesTargetFolder, info.Name()+".gz") | ||
compressedFile, err := os.Create(compressedFilePath) | ||
if err != nil { | ||
return err | ||
} | ||
defer compressedFile.Close() | ||
|
||
gzipWriter := gzip.NewWriter(compressedFile) | ||
defer gzipWriter.Close() | ||
|
||
_, err = io.Copy(gzipWriter, srcFile) | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
}) | ||
} | ||
|
||
func manPagesAlreadyGenerated(manPagesTargetFolder string) bool { | ||
rootCrcManPageFilePath := filepath.Join(manPagesTargetFolder, rootCrcManPage) | ||
if _, err := os.Stat(rootCrcManPageFilePath); os.IsNotExist(err) { | ||
return false | ||
} | ||
return true | ||
} | ||
|
||
func RemoveCrcManPages(manPageDir string) error { | ||
manUserCommandTargetFolder := filepath.Join(manPageDir, "man1") | ||
err := filepath.Walk(manUserCommandTargetFolder, func(path string, info os.FileInfo, err error) error { | ||
if err != nil { | ||
return err | ||
} | ||
if !info.IsDir() && filepath.Base(path)[:len("crc")] == "crc" { | ||
err = os.Remove(path) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
return removeFromManPathEnvironmentVariable(osEnvGetter(ManPathEnvironmentVariable), manPageDir) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
how much extra time does it take when we generate it first time?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm trying to measure execution time using the
time
command however I'm not seeing any significant difference in execution times. Execution time comes out different on every execution:In both cases I ran
crc cleanup
before to make sure no man pages were generated.Time for
crc setup
on v2.46.0:Time for
crc setup
based on this PR: