Skip to content
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

docgen tool: Remove sed usage #17492

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 0 additions & 54 deletions go/cmd/internal/docgen/docgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,6 @@ func GenerateMarkdownTree(cmd *cobra.Command, dir string) error {
return fmt.Errorf("failed to index doc (generated at %s) into proper position (%s): %w", rootDocPath, indexDocPath, err)
}

if err := anonymizeHomedir(indexDocPath); err != nil {
return fmt.Errorf("failed to anonymize homedir in help text for command %s: %w", indexDocPath, err)
}

if err := restructure(dir, dir, cmd.Name(), cmd.Commands()); err != nil {
return err
}
Expand Down Expand Up @@ -132,10 +128,6 @@ func restructure(rootDir string, dir string, name string, commands []*cobra.Comm
return fmt.Errorf("failed to move index doc for command %s with children: %w", fullCmdFilename, err)
}

if err := anonymizeHomedir(indexFile); err != nil {
return fmt.Errorf("failed to anonymize homedir in help text for command %s: %w", indexFile, err)
}

if err := restructure(rootDir, cmdDir, fullCmdFilename, children); err != nil {
return fmt.Errorf("failed to restructure child commands for %s: %w", fullCmdFilename, err)
}
Expand All @@ -154,63 +146,17 @@ func restructure(rootDir string, dir string, name string, commands []*cobra.Comm
return fmt.Errorf("failed to move child command %s to its parent's dir: %w", fullCmdFilename, err)
}

sed := newParentLinkSedCommand(name, newName)
if out, err := sed.CombinedOutput(); err != nil {
return fmt.Errorf("failed to rewrite links to parent command in child %s: %w (extra: %s)", newName, err, out)
}

if err := anonymizeHomedir(newName); err != nil {
return fmt.Errorf("failed to anonymize homedir in help text for command %s: %w", newName, err)
}
default:
// Top-level command without children. Nothing to restructure.
// However we still need to anonymize the homedir in the help text.
if cmd.Name() == "help" {
// all commands with children have their own "help" subcommand,
// which we do not generate docs for
continue
}
f := filepath.Join(dir, fullCmdFilename+".md")
_ = anonymizeHomedir(f) // it is possible that the file does not exist, so we ignore the error
continue
}
}

return nil
}

func newParentLinkSedCommand(parent string, file string) *exec.Cmd {
return exec.Command("sed", "-i", "", "-e", fmt.Sprintf("s:(./%s/):(../):i", parent), file)
}

var (
wd string
once sync.Once
)

func anonymizeHomedir(file string) (err error) {
once.Do(func() {
// Only do this once per run.
wd, err = os.Getwd()
})
if err != nil {
return err
}
if _, err := os.Stat(file); err != nil {
return nil
}

// We're replacing the stuff inside the square brackets in the example sed
// below:
// 's:Paths to search for config files in. (default \[.*\])$:Paths to search for config files in. (default \[<WORKDIR>\]):'
sed := exec.Command("sed", "-i", "", "-e", fmt.Sprintf("s:%s:%s:", wd, "<WORKDIR>"), file)
if out, err := sed.CombinedOutput(); err != nil {
return fmt.Errorf("%w: %s", err, out)
}

return nil
}

func recursivelyDisableAutoGenTags(root *cobra.Command) {
commands := []*cobra.Command{root}
for cmd := commands[0]; len(commands) > 0; cmd, commands = commands[0], commands[1:] {
Expand Down
37 changes: 1 addition & 36 deletions go/cmd/internal/docgen/docgen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ limitations under the License.
package docgen

import (
"strings"
"testing"

"github.com/spf13/cobra"
Expand All @@ -41,7 +40,7 @@ func TestGenerateMarkdownTree(t *testing.T) {
name: "current dir",
dir: "./",
cmd: &cobra.Command{},
expectErr: true,
expectErr: false,
},
{
name: "Permission denied",
Expand Down Expand Up @@ -149,40 +148,6 @@ func TestLinkHandler(t *testing.T) {
}
}

func TestNewParentLinkSedCommand(t *testing.T) {
tests := []struct {
name string
parentDir string
fileName string
expectedOutput string
}{
{
name: "Empty values",
expectedOutput: "sed -i -e s:(.//):(../):i ",
},
{
name: "Normal value",
parentDir: "./",
fileName: "Some_value",
expectedOutput: "sed -i -e s:(././/):(../):i Some_value",
},
{
name: "Abnormal value",
parentDir: "/root",
fileName: `./.jash13_24`,
expectedOutput: "sed -i -e s:(.//root/):(../):i ./.jash13_24",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cmd := newParentLinkSedCommand(tt.parentDir, tt.fileName)
// We only check for suffix because the sed command's actual path may differ on different machines.
require.True(t, strings.HasSuffix(cmd.String(), tt.expectedOutput))
})
}
}

func TestGetCommitID(t *testing.T) {
// This function should return an error when the reference is not in the
// git tree.
Expand Down
Loading