|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "strings" |
| 8 | + |
| 9 | + "github.com/formancehq/fctl/cmd" |
| 10 | + "github.com/spf13/cobra" |
| 11 | + "github.com/spf13/pflag" |
| 12 | +) |
| 13 | + |
| 14 | +var ( |
| 15 | + file = "inventory.json" |
| 16 | +) |
| 17 | + |
| 18 | +type Flag struct { |
| 19 | + Name string `json:"name"` |
| 20 | + Description string `json:"description"` |
| 21 | + Deprecated string `json:"deprecated,omitempty"` |
| 22 | + Usage string `json:"usage"` |
| 23 | + DefaultValue string `json:"default_value,omitempty"` |
| 24 | + Type string `json:"type,omitempty"` |
| 25 | +} |
| 26 | + |
| 27 | +type DocCommand struct { |
| 28 | + Name string `json:"name"` |
| 29 | + Usage string `json:"usage"` |
| 30 | + Description string `json:"description"` |
| 31 | + Deprecated string `json:"deprecated,omitempty"` |
| 32 | + Aliases []string `json:"aliases,omitempty"` |
| 33 | + |
| 34 | + Flags []Flag `json:"flags,omitempty"` |
| 35 | + GlobalFlags []Flag `json:"global_flags,omitempty"` |
| 36 | + |
| 37 | + SubCommands []DocCommand `json:"subcommands,omitempty"` |
| 38 | +} |
| 39 | + |
| 40 | +func flagSetToFlags(flagSet *pflag.FlagSet) []Flag { |
| 41 | + flags := make([]Flag, 0) |
| 42 | + |
| 43 | + flagSet.VisitAll(func(f *pflag.Flag) { |
| 44 | + if f.Hidden { |
| 45 | + return |
| 46 | + } |
| 47 | + |
| 48 | + userHomeDir, err := os.UserHomeDir() |
| 49 | + if err != nil { |
| 50 | + panic(err) |
| 51 | + } |
| 52 | + if strings.Contains(f.DefValue, userHomeDir) { |
| 53 | + f.DefValue = strings.ReplaceAll(f.DefValue, userHomeDir, "~") |
| 54 | + } |
| 55 | + |
| 56 | + flags = append(flags, Flag{ |
| 57 | + Name: f.Name, |
| 58 | + Description: f.Usage, |
| 59 | + Deprecated: f.Deprecated, |
| 60 | + DefaultValue: f.DefValue, |
| 61 | + Usage: f.Usage, |
| 62 | + Type: f.Value.Type(), |
| 63 | + }) |
| 64 | + }) |
| 65 | + |
| 66 | + return flags |
| 67 | +} |
| 68 | + |
| 69 | +func cobraCommandAsDocCommand(parentCommandName string, command *cobra.Command) DocCommand { |
| 70 | + return DocCommand{ |
| 71 | + Name: func() string { |
| 72 | + if parentCommandName != "" { |
| 73 | + return fmt.Sprintf("%s %s", parentCommandName, command.Name()) |
| 74 | + } |
| 75 | + return command.Name() |
| 76 | + }(), |
| 77 | + Usage: command.Use, |
| 78 | + Description: command.Short, |
| 79 | + Deprecated: command.Deprecated, |
| 80 | + Aliases: command.Aliases, |
| 81 | + Flags: flagSetToFlags(command.Flags()), |
| 82 | + GlobalFlags: flagSetToFlags(command.Root().PersistentFlags()), |
| 83 | + } |
| 84 | + |
| 85 | +} |
| 86 | + |
| 87 | +func getFullCommandPath(cmd *cobra.Command) string { |
| 88 | + if cmd == nil || cmd == cmd.Root() { |
| 89 | + return "" |
| 90 | + } |
| 91 | + parent := getFullCommandPath(cmd.Parent()) |
| 92 | + if parent == "" { |
| 93 | + return cmd.Name() |
| 94 | + } |
| 95 | + return parent + " " + cmd.Name() |
| 96 | +} |
| 97 | + |
| 98 | +func visitSubCommands(command *cobra.Command) []DocCommand { |
| 99 | + parentName := getFullCommandPath(command.Parent()) |
| 100 | + docs := []DocCommand{cobraCommandAsDocCommand(parentName, command)} |
| 101 | + for _, subCommand := range command.Commands() { |
| 102 | + docs = append(docs, visitSubCommands(subCommand)...) |
| 103 | + } |
| 104 | + return docs |
| 105 | +} |
| 106 | + |
| 107 | +//go:generate rm -rf docs |
| 108 | +//go:generate go run ./ |
| 109 | +func main() { |
| 110 | + _, err := os.Stat(file) |
| 111 | + if err == nil { |
| 112 | + if err := os.Remove(file); err != nil { |
| 113 | + panic(err) |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + commands := visitSubCommands(cmd.NewRootCommand()) |
| 118 | + file, err := os.OpenFile(file, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0600) |
| 119 | + if err != nil { |
| 120 | + panic(err) |
| 121 | + } |
| 122 | + defer file.Close() |
| 123 | + |
| 124 | + b, err := json.Marshal(commands) |
| 125 | + if err != nil { |
| 126 | + panic(err) |
| 127 | + } |
| 128 | + if _, err := file.Write(b); err != nil { |
| 129 | + panic(err) |
| 130 | + } |
| 131 | +} |
0 commit comments