NewCmdReadDir creates the `gh repo read-dir` command.
(f *cmdutil.Factory, runF func(*ReadDirOptions) error)
| 42 | |
| 43 | // NewCmdReadDir creates the `gh repo read-dir` command. |
| 44 | func NewCmdReadDir(f *cmdutil.Factory, runF func(*ReadDirOptions) error) *cobra.Command { |
| 45 | opts := &ReadDirOptions{ |
| 46 | IO: f.IOStreams, |
| 47 | HttpClient: f.HttpClient, |
| 48 | BaseRepo: f.BaseRepo, |
| 49 | } |
| 50 | |
| 51 | cmd := &cobra.Command{ |
| 52 | Use: "read-dir [<path>] [flags]", |
| 53 | Short: "List a directory in a repository (preview)", |
| 54 | Long: heredoc.Docf(` |
| 55 | List the contents of a directory in a GitHub repository without cloning it. |
| 56 | |
| 57 | This command is in preview and subject to change without notice. |
| 58 | |
| 59 | By default, the directory is listed from the default branch. Use the %[1]s--ref%[1]s flag to |
| 60 | list from a specific branch, tag, or commit. When no path is given, the repository root |
| 61 | is listed. |
| 62 | `, "`"), |
| 63 | Example: heredoc.Doc(` |
| 64 | # List the root of the default branch |
| 65 | $ gh repo read-dir --repo cli/cli |
| 66 | |
| 67 | # List a subdirectory |
| 68 | $ gh repo read-dir docs --repo cli/cli |
| 69 | |
| 70 | # List a directory at a specific ref |
| 71 | $ gh repo read-dir docs --repo cli/cli --ref v2.50.0 |
| 72 | |
| 73 | # Print selected fields as JSON |
| 74 | $ gh repo read-dir docs --repo cli/cli --json name,path,type,size |
| 75 | `), |
| 76 | Args: cobra.MaximumNArgs(1), |
| 77 | RunE: func(cmd *cobra.Command, args []string) error { |
| 78 | opts.BaseRepo = f.BaseRepo |
| 79 | |
| 80 | if len(args) > 0 { |
| 81 | opts.Path = args[0] |
| 82 | } |
| 83 | |
| 84 | if runF != nil { |
| 85 | return runF(opts) |
| 86 | } |
| 87 | |
| 88 | return readDirRun(opts) |
| 89 | }, |
| 90 | } |
| 91 | |
| 92 | cmd.Flags().StringVar(&opts.Ref, "ref", "", "The branch, tag, or commit to list from") |
| 93 | |
| 94 | cmdutil.AddJSONFlags(cmd, &opts.Exporter, dirEntryFields) |
| 95 | |
| 96 | cmdutil.EnableRepoOverride(cmd, f) |
| 97 | |
| 98 | return cmd |
| 99 | } |
| 100 | |
| 101 | func readDirRun(opts *ReadDirOptions) error { |