Skip to content

Commit

Permalink
Merge pull request #1592 from afbjorklund/man-prefix
Browse files Browse the repository at this point in the history
Remove the output directory from the manpages
  • Loading branch information
AkihiroSuda authored May 29, 2023
2 parents e7aa4ff + 703d69f commit 3dbc08c
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 3dbc08c

Please sign in to comment.