Skip to content
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
37 changes: 37 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,13 +253,50 @@ func bugReport(ctx context.Context, version string) error {
return nil
}

// ipPortRe is used only by the man-page generator to sanitize printed defaults. It matches IPv4 (optionally with :port)
var ipPortRe = regexp.MustCompile(`\b(?:\d{1,3}\.){3}\d{1,3}(?::\d+)?\b`)

// Keep scope tight: only redact flags that look like address defaults.
func looksLikeAddrFlag(name string) bool {
return strings.HasSuffix(name, "-addr") || strings.HasSuffix(name, "-address")
}

func sanitizeFlagSet(fs *pflag.FlagSet) {
// For manpage generation only: show a neutral, deterministic doc default.
const docAddrPlaceholder = "[auto-detected IP]"
if fs == nil {
return
}
fs.VisitAll(func(f *pflag.Flag) {
if !looksLikeAddrFlag(f.Name) {
return
}
if ipPortRe.MatchString(f.DefValue) {
f.DefValue = docAddrPlaceholder
}
})
}

// Sanitize this command and its subcommands for doc generation only for --man-page. This changes printed defaults, not runtime behavior.
func sanitizeFlagDefaultsForDocs(cmd *cobra.Command) {
if cmd == nil {
return
}
sanitizeFlagSet(cmd.PersistentFlags())
sanitizeFlagSet(cmd.Flags())
for _, child := range cmd.Commands() {
sanitizeFlagDefaultsForDocs(child)
}
}

func generateManPage(cmd *cobra.Command) error {
header := &doc.GenManHeader{
Title: "act",
Section: "1",
Source: fmt.Sprintf("act %s", cmd.Version),
}
buf := new(bytes.Buffer)
sanitizeFlagDefaultsForDocs(cmd.Root())
cobra.CheckErr(doc.GenMan(cmd, header, buf))
fmt.Print(buf.String())
return nil
Expand Down
17 changes: 17 additions & 0 deletions cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"path"
"testing"

"github.com/spf13/cobra"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -123,3 +124,19 @@ func TestReadArgsFile(t *testing.T) {
})
}
}

func TestSanitizeFlagDefaultsForDocs(t *testing.T) {
root := &cobra.Command{Use: "act"}
child := &cobra.Command{Use: "child"}
root.AddCommand(child)
root.PersistentFlags().String("artifact-server-addr", "192.168.0.10:34567", "addr")
root.PersistentFlags().String("cache-server-address", "10.0.0.5", "addr")
child.Flags().String("custom-addr", "172.16.0.2:1234", "addr")
root.PersistentFlags().String("actor", "nektos/act", "user that triggered the event")
sanitizeFlagDefaultsForDocs(root)
const docAddrPlaceholder = "[auto-detected IP]"
assert.Equal(t, docAddrPlaceholder, root.PersistentFlags().Lookup("artifact-server-addr").DefValue)
assert.Equal(t, docAddrPlaceholder, root.PersistentFlags().Lookup("cache-server-address").DefValue)
assert.Equal(t, docAddrPlaceholder, child.Flags().Lookup("custom-addr").DefValue)
assert.Equal(t, "nektos/act", root.PersistentFlags().Lookup("actor").DefValue)
}