1
1
package main
2
2
3
3
import (
4
+ "bytes"
5
+ "io/fs"
4
6
"os"
5
7
"path/filepath"
6
8
@@ -18,10 +20,28 @@ func newGenManCommand() *cobra.Command {
18
20
RunE : genmanAction ,
19
21
Hidden : true ,
20
22
}
23
+ genmanCommand .Flags ().String ("output" , "" , "Output directory" )
24
+ genmanCommand .Flags ().String ("prefix" , "" , "Install prefix" )
21
25
return genmanCommand
22
26
}
23
27
24
28
func genmanAction (cmd * cobra.Command , args []string ) error {
29
+ output , err := cmd .Flags ().GetString ("output" )
30
+ if err != nil {
31
+ return err
32
+ }
33
+ output , err = filepath .Abs (output )
34
+ if err != nil {
35
+ return err
36
+ }
37
+ prefix , err := cmd .Flags ().GetString ("prefix" )
38
+ if err != nil {
39
+ return err
40
+ }
41
+ homeDir , err := os .UserHomeDir ()
42
+ if err != nil {
43
+ return err
44
+ }
25
45
dir := args [0 ]
26
46
logrus .Infof ("Generating man %q" , dir )
27
47
// lima(1)
@@ -49,5 +69,38 @@ and $LIMA_WORKDIR.
49
69
Title : "LIMACTL" ,
50
70
Section : "1" ,
51
71
}
52
- return doc .GenManTree (cmd .Root (), header , dir )
72
+ if err := doc .GenManTree (cmd .Root (), header , dir ); err != nil {
73
+ return err
74
+ }
75
+ if output != "" && prefix != "" {
76
+ replaceAll (dir , output , prefix )
77
+ }
78
+ replaceAll (dir , homeDir , "~" )
79
+ return nil
80
+ }
81
+
82
+ // replaceAll replaces all occurrences of new with old, for all files in dir
83
+ func replaceAll (dir string , old , new string ) error {
84
+ logrus .Infof ("Replacing %q with %q" , old , new )
85
+ return filepath .Walk (dir , func (path string , info fs.FileInfo , err error ) error {
86
+ if err != nil {
87
+ return err
88
+ }
89
+ if path == dir {
90
+ return nil
91
+ }
92
+ if info .IsDir () {
93
+ return filepath .SkipDir
94
+ }
95
+ in , err := os .ReadFile (path )
96
+ if err != nil {
97
+ return err
98
+ }
99
+ out := bytes .Replace (in , []byte (old ), []byte (new ), - 1 )
100
+ err = os .WriteFile (path , out , 0644 )
101
+ if err != nil {
102
+ return err
103
+ }
104
+ return nil
105
+ })
53
106
}
0 commit comments