MCPcopy Index your code
hub / github.com/cli/cli / readDirRun

Function readDirRun

pkg/cmd/repo/read-dir/read_dir.go:101–150  ·  view source on GitHub ↗
(opts *ReadDirOptions)

Source from the content-addressed store, hash-verified

99}
100
101func 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.

Callers 2

NewCmdReadDirFunction · 0.85
Test_readDirRunFunction · 0.85

Calls 10

FullNameFunction · 0.92
fetchTreeFunction · 0.85
writeTSVFunction · 0.85
writeTableFunction · 0.85
IsStdoutTTYMethod · 0.80
StartPagerMethod · 0.80
StopPagerMethod · 0.80
BaseRepoMethod · 0.65
ErrorfMethod · 0.65
WriteMethod · 0.65

Tested by 1

Test_readDirRunFunction · 0.68