Skip to content

Commit

Permalink
Remove the build directories from the manpages
Browse files Browse the repository at this point in the history
They should reflect the actual install location

Also replace the current home directory with ~

Signed-off-by: Anders F Björklund <[email protected]>
  • Loading branch information
afbjorklund committed May 29, 2023
1 parent e7aa4ff commit 703d69f
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 2 deletions.
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@ _output/share/lima/lima-guestagent.Linux-riscv64:
.PHONY: manpages
manpages: _output/bin/limactl$(exe)
@mkdir -p _output/share/man/man1
$< generate-man _output/share/man/man1
$< generate-man _output/share/man/man1 \
--output _output --prefix $(PREFIX)

.PHONY: diagrams
diagrams: docs/lima-sequence-diagram.png
Expand Down
55 changes: 54 additions & 1 deletion cmd/limactl/genman.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package main

import (
"bytes"
"io/fs"
"os"
"path/filepath"

Expand All @@ -18,10 +20,28 @@ func newGenManCommand() *cobra.Command {
RunE: genmanAction,
Hidden: true,
}
genmanCommand.Flags().String("output", "", "Output directory")
genmanCommand.Flags().String("prefix", "", "Install prefix")
return genmanCommand
}

func genmanAction(cmd *cobra.Command, args []string) error {
output, err := cmd.Flags().GetString("output")
if err != nil {
return err
}
output, err = filepath.Abs(output)
if err != nil {
return err
}
prefix, err := cmd.Flags().GetString("prefix")
if err != nil {
return err
}
homeDir, err := os.UserHomeDir()
if err != nil {
return err
}
dir := args[0]
logrus.Infof("Generating man %q", dir)
// lima(1)
Expand Down Expand Up @@ -49,5 +69,38 @@ and $LIMA_WORKDIR.
Title: "LIMACTL",
Section: "1",
}
return doc.GenManTree(cmd.Root(), header, dir)
if err := doc.GenManTree(cmd.Root(), header, dir); err != nil {
return err
}
if output != "" && prefix != "" {
replaceAll(dir, output, prefix)
}
replaceAll(dir, homeDir, "~")
return nil
}

// replaceAll replaces all occurrences of new with old, for all files in dir
func replaceAll(dir string, old, new string) error {
logrus.Infof("Replacing %q with %q", old, new)
return filepath.Walk(dir, func(path string, info fs.FileInfo, err error) error {
if err != nil {
return err
}
if path == dir {
return nil
}
if info.IsDir() {
return filepath.SkipDir
}
in, err := os.ReadFile(path)
if err != nil {
return err
}
out := bytes.Replace(in, []byte(old), []byte(new), -1)
err = os.WriteFile(path, out, 0644)
if err != nil {
return err
}
return nil
})
}

0 comments on commit 703d69f

Please sign in to comment.