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
6 changes: 3 additions & 3 deletions docs/v3/binary-size.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,22 @@
Go removes unreachable code during compilation, so the first step is to
measure the binary you ship instead of assuming a specific feature is expensive.

```sh
```sh-session
go build -trimpath -o myapp ./cmd/myapp
ls -lh myapp
```

For a release-style build, combine reproducible paths with stripped symbol and
debug information:

```sh
```sh-session
go build -trimpath -ldflags="-s -w" -o myapp ./cmd/myapp
ls -lh myapp
```

Use the Go toolchain to inspect what is in the binary:

```sh
```sh-session
go version -m myapp
go tool nm -size myapp | sort -nr | head -40
```
Expand Down
25 changes: 13 additions & 12 deletions docs/v3/path-and-walk.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,24 @@ package main
import (
"context"
"fmt"
"os"
"strings"

"github.com/urfave/cli/v3"
)

func main() {
subSubCmd := &cli.Command{Name: "bottom", Action: func(context.Context, *cli.Command) error { return nil }}
subCmd := &cli.Command{Name: "mid", Subcommands: []*cli.Command{subSubCmd}, Action: func(context.Context, *cli.Command) error { return nil }}
cmd := &cli.Command{
Name: "top",
Subcommands: []*cli.Command{subCmd},
subSubCmd := &cli.Command{
Name: "bottom",
Action: func(ctx context.Context, c *cli.Command) error {
fmt.Println(strings.Join(c.Path(), " "))
return nil
},
}
subCmd := &cli.Command{Name: "mid", Commands: []*cli.Command{subSubCmd}, Action: func(context.Context, *cli.Command) error { return nil }}
cmd := &cli.Command{
Name: "top",
Commands: []*cli.Command{subCmd},
}

cmd.Run(context.Background(), []string{"top", "mid", "bottom"})
}
Expand All @@ -66,17 +68,16 @@ package main
import (
"context"
"fmt"
"os"

"github.com/urfave/cli/v3"
)

func main() {
subSubCmd := &cli.Command{Name: "bottom", Action: func(context.Context, *cli.Command) error { return nil }}
subCmd := &cli.Command{Name: "mid", Subcommands: []*cli.Command{subSubCmd}, Action: func(context.Context, *cli.Command) error { return nil }}
subCmd := &cli.Command{Name: "mid", Commands: []*cli.Command{subSubCmd}, Action: func(context.Context, *cli.Command) error { return nil }}
cmd := &cli.Command{
Name: "top",
Subcommands: []*cli.Command{subCmd},
Commands: []*cli.Command{subCmd},
Action: func(ctx context.Context, c *cli.Command) error { return nil },
}

Expand Down Expand Up @@ -105,19 +106,19 @@ Return a non-nil error from the walk function to stop traversal early.
package main

import (
"context"
"errors"
"fmt"
"os"

"github.com/urfave/cli/v3"
)

func main() {
subSubCmd := &cli.Command{Name: "bottom", Action: func(context.Context, *cli.Command) error { return nil }}
subCmd := &cli.Command{Name: "mid", Subcommands: []*cli.Command{subSubCmd}, Action: func(context.Context, *cli.Command) error { return nil }}
subCmd := &cli.Command{Name: "mid", Commands: []*cli.Command{subSubCmd}, Action: func(context.Context, *cli.Command) error { return nil }}
cmd := &cli.Command{
Name: "top",
Subcommands: []*cli.Command{subCmd},
Commands: []*cli.Command{subCmd},
Action: func(ctx context.Context, c *cli.Command) error { return nil },
}

Expand Down
32 changes: 31 additions & 1 deletion scripts/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"os"
"os/exec"
"path/filepath"
"regexp"
"runtime"
"strings"
"time"
Expand Down Expand Up @@ -362,6 +363,9 @@ func GfmrunActionFunc(ctx context.Context, cmd *cli.Command) error {
}

var counter int
inGoBlock := false
goHasPkgMain := false
fenceRe := regexp.MustCompile("^(`{3,}|~{3,})")

for _, src := range sources {
file, err := os.Open(src)
Expand All @@ -372,7 +376,33 @@ func GfmrunActionFunc(ctx context.Context, cmd *cli.Command) error {

scanner := bufio.NewScanner(file)
for scanner.Scan() {
if strings.Contains(scanner.Text(), "package main") {
line := scanner.Text()
fenceMatch := fenceRe.FindString(line)
if fenceMatch == "" {
if inGoBlock && strings.HasPrefix(line, "package main") {
goHasPkgMain = true
}
continue
}

lang := strings.TrimSpace(line[len(fenceMatch):])

if inGoBlock {
if goHasPkgMain {
counter++
}
inGoBlock = false
goHasPkgMain = false
continue
}

switch lang {
case "go":
inGoBlock = true
goHasPkgMain = false
case "sh":
counter++
case "bash":
counter++
}
}
Expand Down