GenTree generates a JSON tree for the given root command
(root *cobra.Command)
| 123 | |
| 124 | // GenTree generates a JSON tree for the given root command |
| 125 | func GenJSONTree(root *cobra.Command) *cobra.Command { |
| 126 | cmd := &cobra.Command{ |
| 127 | Use: "gen-json-tree", |
| 128 | Hidden: true, |
| 129 | Short: fmt.Sprintf("Generate JSON tree for %s", root.Name()), |
| 130 | RunE: func(cmd *cobra.Command, args []string) error { |
| 131 | dir, _ := cmd.Flags().GetString("out") |
| 132 | |
| 133 | out := filepath.Join(dir, root.Name()+".json") |
| 134 | |
| 135 | f, err := os.Create(out) |
| 136 | if err != nil { |
| 137 | return err |
| 138 | } |
| 139 | defer f.Close() |
| 140 | |
| 141 | enc := json.NewEncoder(f) |
| 142 | enc.SetIndent("", " ") |
| 143 | return enc.Encode(map[string]command{ |
| 144 | cmd.Root().Name(): commandTree(cmd.Root()), |
| 145 | }) |
| 146 | }, |
| 147 | } |
| 148 | cmd.Flags().StringP("out", "o", "doc", "output directory") |
| 149 | return cmd |
| 150 | } |
no test coverage detected