github.com/lalkh/containerd@v1.4.3/cmd/gen-manpages/main.go (about) 1 /* 2 Copyright The containerd Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package main 18 19 import ( 20 "flag" 21 "fmt" 22 "io/ioutil" 23 "os" 24 "path/filepath" 25 "strings" 26 27 "github.com/containerd/containerd/cmd/containerd/command" 28 "github.com/containerd/containerd/cmd/ctr/app" 29 "github.com/urfave/cli" 30 ) 31 32 func main() { 33 if err := run(); err != nil { 34 fmt.Fprint(os.Stderr, err) 35 os.Exit(1) 36 } 37 } 38 39 func run() error { 40 flag.Parse() 41 apps := map[string]*cli.App{ 42 "containerd": command.App(), 43 "ctr": app.New(), 44 } 45 dir := flag.Arg(1) 46 47 parts := strings.SplitN(flag.Arg(0), ".", 2) 48 if len(parts) != 2 { 49 return fmt.Errorf("invalid name '%s': name does not contain man page section", flag.Arg(0)) 50 } 51 name, section := parts[0], parts[1] 52 53 appName, ok := apps[name] 54 if !ok { 55 return fmt.Errorf("invalid application '%s'", name) 56 } 57 58 // clear out the usage as we use banners that do not display in man pages 59 appName.Usage = "" 60 appName.ToMan() 61 data, err := appName.ToMan() 62 if err != nil { 63 return err 64 } 65 _ = os.MkdirAll(dir, os.ModePerm) 66 if err := ioutil.WriteFile(filepath.Join(dir, fmt.Sprintf("%s.%s", name, section)), []byte(data), 0644); err != nil { 67 return err 68 } 69 return nil 70 }