| 99 | } |
| 100 | |
| 101 | func readDirRun(opts *ReadDirOptions) error { |
| 102 | httpClient, err := opts.HttpClient() |
| 103 | if err != nil { |
| 104 | return err |
| 105 | } |
| 106 | |
| 107 | repo, err := opts.BaseRepo() |
| 108 | if err != nil { |
| 109 | return fmt.Errorf("%w. Run this command from within a git repository, or use the `--repo` flag to specify one", err) |
| 110 | } |
| 111 | |
| 112 | dir, err := fetchTree(httpClient, repo, opts.Path, opts.Ref) |
| 113 | if err != nil { |
| 114 | return err |
| 115 | } |
| 116 | |
| 117 | if opts.Exporter != nil { |
| 118 | return opts.Exporter.Write(opts.IO, dir) |
| 119 | } |
| 120 | |
| 121 | if len(dir.Entries) == 0 { |
| 122 | location := ghrepo.FullName(repo) |
| 123 | if opts.Path != "" { |
| 124 | location = fmt.Sprintf("%s/%s", location, strings.TrimPrefix(opts.Path, "/")) |
| 125 | } |
| 126 | fmt.Fprintf(opts.IO.ErrOut, "No entries found in %s\n", location) |
| 127 | return nil |
| 128 | } |
| 129 | |
| 130 | if !opts.IO.IsStdoutTTY() { |
| 131 | return writeTSV(opts.IO, dir) |
| 132 | } |
| 133 | |
| 134 | if err := opts.IO.StartPager(); err != nil { |
| 135 | fmt.Fprintf(opts.IO.ErrOut, "error starting pager: %v\n", err) |
| 136 | } |
| 137 | defer opts.IO.StopPager() |
| 138 | |
| 139 | location := ghrepo.FullName(repo) |
| 140 | if opts.Path != "" { |
| 141 | location = fmt.Sprintf("%s/%s", location, strings.TrimPrefix(opts.Path, "/")) |
| 142 | } |
| 143 | noun := "entries" |
| 144 | if len(dir.Entries) == 1 { |
| 145 | noun = "entry" |
| 146 | } |
| 147 | fmt.Fprintf(opts.IO.Out, "Showing %d %s in %s\n\n", len(dir.Entries), noun, location) |
| 148 | |
| 149 | return writeTable(opts.IO, dir) |
| 150 | } |
| 151 | |
| 152 | // writeTSV writes a tab-separated listing for non-TTY output: type, name, |
| 153 | // octal mode, and raw byte size, with no header. |