Skip to content

Commit fc414bc

Browse files
Remove sed usage from docgen
Signed-off-by: Rohit Nayak <[email protected]>
1 parent 06def14 commit fc414bc

File tree

1 file changed

+1
-47
lines changed

1 file changed

+1
-47
lines changed

go/cmd/internal/docgen/docgen.go

+1-47
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,6 @@ func GenerateMarkdownTree(cmd *cobra.Command, dir string) error {
8585
return fmt.Errorf("failed to index doc (generated at %s) into proper position (%s): %w", rootDocPath, indexDocPath, err)
8686
}
8787

88-
if err := anonymizeHomedir(indexDocPath); err != nil {
89-
return fmt.Errorf("failed to anonymize homedir in help text for command %s: %w", indexDocPath, err)
90-
}
91-
9288
if err := restructure(dir, dir, cmd.Name(), cmd.Commands()); err != nil {
9389
return err
9490
}
@@ -132,10 +128,6 @@ func restructure(rootDir string, dir string, name string, commands []*cobra.Comm
132128
return fmt.Errorf("failed to move index doc for command %s with children: %w", fullCmdFilename, err)
133129
}
134130

135-
if err := anonymizeHomedir(indexFile); err != nil {
136-
return fmt.Errorf("failed to anonymize homedir in help text for command %s: %w", indexFile, err)
137-
}
138-
139131
if err := restructure(rootDir, cmdDir, fullCmdFilename, children); err != nil {
140132
return fmt.Errorf("failed to restructure child commands for %s: %w", fullCmdFilename, err)
141133
}
@@ -158,59 +150,21 @@ func restructure(rootDir string, dir string, name string, commands []*cobra.Comm
158150
if out, err := sed.CombinedOutput(); err != nil {
159151
return fmt.Errorf("failed to rewrite links to parent command in child %s: %w (extra: %s)", newName, err, out)
160152
}
161-
162-
if err := anonymizeHomedir(newName); err != nil {
163-
return fmt.Errorf("failed to anonymize homedir in help text for command %s: %w", newName, err)
164-
}
165-
default:
166-
// Top-level command without children. Nothing to restructure.
167-
// However we still need to anonymize the homedir in the help text.
168-
if cmd.Name() == "help" {
169-
// all commands with children have their own "help" subcommand,
170-
// which we do not generate docs for
171-
continue
172-
}
173-
f := filepath.Join(dir, fullCmdFilename+".md")
174-
_ = anonymizeHomedir(f) // it is possible that the file does not exist, so we ignore the error
175-
continue
176153
}
177154
}
178155

179156
return nil
180157
}
181158

182159
func newParentLinkSedCommand(parent string, file string) *exec.Cmd {
183-
return exec.Command("sed", "-i", "", "-e", fmt.Sprintf("s:(./%s/):(../):i", parent), file)
160+
return exec.Command("xyz", "-i", "-e", fmt.Sprintf("s:(./%s/):(../):i", parent), file)
184161
}
185162

186163
var (
187164
wd string
188165
once sync.Once
189166
)
190167

191-
func anonymizeHomedir(file string) (err error) {
192-
once.Do(func() {
193-
// Only do this once per run.
194-
wd, err = os.Getwd()
195-
})
196-
if err != nil {
197-
return err
198-
}
199-
if _, err := os.Stat(file); err != nil {
200-
return nil
201-
}
202-
203-
// We're replacing the stuff inside the square brackets in the example sed
204-
// below:
205-
// 's:Paths to search for config files in. (default \[.*\])$:Paths to search for config files in. (default \[<WORKDIR>\]):'
206-
sed := exec.Command("sed", "-i", "", "-e", fmt.Sprintf("s:%s:%s:", wd, "<WORKDIR>"), file)
207-
if out, err := sed.CombinedOutput(); err != nil {
208-
return fmt.Errorf("%w: %s", err, out)
209-
}
210-
211-
return nil
212-
}
213-
214168
func recursivelyDisableAutoGenTags(root *cobra.Command) {
215169
commands := []*cobra.Command{root}
216170
for cmd := commands[0]; len(commands) > 0; cmd, commands = commands[0], commands[1:] {

0 commit comments

Comments
 (0)