containerd/cmd/gen-manpages/main.go
Sebastiaan van Stijn eaedadbed0
replace strings.Split(N) for strings.Cut() or alternatives
Go 1.18 and up now provides a strings.Cut() which is better suited for
splitting key/value pairs (and similar constructs), and performs better:

```go
func BenchmarkSplit(b *testing.B) {
        b.ReportAllocs()
        data := []string{"12hello=world", "12hello=", "12=hello", "12hello"}
        for i := 0; i < b.N; i++ {
                for _, s := range data {
                        _ = strings.SplitN(s, "=", 2)[0]
                }
        }
}

func BenchmarkCut(b *testing.B) {
        b.ReportAllocs()
        data := []string{"12hello=world", "12hello=", "12=hello", "12hello"}
        for i := 0; i < b.N; i++ {
                for _, s := range data {
                        _, _, _ = strings.Cut(s, "=")
                }
        }
}
```

    BenchmarkSplit
    BenchmarkSplit-10            8244206               128.0 ns/op           128 B/op          4 allocs/op
    BenchmarkCut
    BenchmarkCut-10             54411998                21.80 ns/op            0 B/op          0 allocs/op

While looking at occurrences of `strings.Split()`, I also updated some for alternatives,
or added some constraints; for cases where an specific number of items is expected, I used `strings.SplitN()`
with a suitable limit. This prevents (theoretical) unlimited splits.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
2022-11-07 10:02:25 +01:00

68 lines
1.6 KiB
Go

/*
Copyright The containerd Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"flag"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/containerd/containerd/cmd/containerd/command"
"github.com/containerd/containerd/cmd/ctr/app"
"github.com/urfave/cli"
)
func main() {
if err := run(); err != nil {
fmt.Fprint(os.Stderr, err)
os.Exit(1)
}
}
func run() error {
flag.Parse()
apps := map[string]*cli.App{
"containerd": command.App(),
"ctr": app.New(),
}
dir := flag.Arg(1)
name, section, ok := strings.Cut(flag.Arg(0), ".")
if !ok {
return fmt.Errorf("invalid name '%s': name does not contain man page section", flag.Arg(0))
}
appName, ok := apps[name]
if !ok {
return fmt.Errorf("invalid application '%s'", name)
}
// clear out the usage as we use banners that do not display in man pages
appName.Usage = ""
data, err := appName.ToMan()
if err != nil {
return err
}
_ = os.MkdirAll(dir, os.ModePerm)
if err := os.WriteFile(filepath.Join(dir, fmt.Sprintf("%s.%s", name, section)), []byte(data), 0644); err != nil {
return err
}
return nil
}