X Tutup
Skip to content

Commit edfac42

Browse files
committed
Set up iostreams in factory default
1 parent 53fac59 commit edfac42

File tree

4 files changed

+147
-16
lines changed

4 files changed

+147
-16
lines changed

cmd/gh/main.go

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -93,15 +93,6 @@ func mainRun() exitCode {
9393
return exitError
9494
}
9595

96-
if prompt, _ := cfg.Get("", "prompt"); prompt == "disabled" {
97-
cmdFactory.IOStreams.SetNeverPrompt(true)
98-
}
99-
if ghPager, ghPagerExists := os.LookupEnv("GH_PAGER"); ghPagerExists {
100-
cmdFactory.IOStreams.SetPager(ghPager)
101-
} else if pager, _ := cfg.Get("", "pager"); pager != "" {
102-
cmdFactory.IOStreams.SetPager(pager)
103-
}
104-
10596
// TODO: remove after FromFullName has been revisited
10697
if host, err := cfg.DefaultHost(); err == nil {
10798
ghrepo.SetDefaultHost(host)

pkg/cmd/factory/default.go

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ import (
1717

1818
func New(appVersion string) *cmdutil.Factory {
1919
f := &cmdutil.Factory{
20-
IOStreams: iostreams.System(), // No factory dependencies
21-
Config: configFunc(), // No factory dependencies
22-
Branch: branchFunc(), // No factory dependencies
23-
Executable: executable(), // No factory dependencies
20+
Config: configFunc(), // No factory dependencies
21+
Branch: branchFunc(), // No factory dependencies
22+
Executable: executable(), // No factory dependencies
2423
}
2524

25+
f.IOStreams = ioStreams(f) // Depends on Config
2626
f.HttpClient = httpClientFunc(f, appVersion) // Depends on Config, IOStreams, and appVersion
2727
f.Remotes = remotesFunc(f) // Depends on Config
2828
f.BaseRepo = BaseRepoFunc(f) // Depends on Remotes
@@ -125,3 +125,27 @@ func branchFunc() func() (string, error) {
125125
return currentBranch, nil
126126
}
127127
}
128+
129+
func ioStreams(f *cmdutil.Factory) *iostreams.IOStreams {
130+
io := iostreams.System()
131+
cfg, err := f.Config()
132+
if err != nil {
133+
return io
134+
}
135+
136+
if prompt, _ := cfg.Get("", "prompt"); prompt == "disabled" {
137+
io.SetNeverPrompt(true)
138+
}
139+
140+
// Pager precedence
141+
// 1. GH_PAGER
142+
// 2. pager from config
143+
// 3. PAGER
144+
if ghPager, ghPagerExists := os.LookupEnv("GH_PAGER"); ghPagerExists {
145+
io.SetPager(ghPager)
146+
} else if pager, _ := cfg.Get("", "pager"); pager != "" {
147+
io.SetPager(pager)
148+
}
149+
150+
return io
151+
}

pkg/cmd/factory/default_test.go

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,10 +272,120 @@ func Test_OverrideBaseRepo(t *testing.T) {
272272
}
273273
}
274274

275+
func Test_ioStreams_pager(t *testing.T) {
276+
tests := []struct {
277+
name string
278+
env map[string]string
279+
config config.Config
280+
wantPager string
281+
}{
282+
{
283+
name: "GH_PAGER and PAGER set",
284+
env: map[string]string{
285+
"GH_PAGER": "GH_PAGER",
286+
"PAGER": "PAGER",
287+
},
288+
wantPager: "GH_PAGER",
289+
},
290+
{
291+
name: "GH_PAGER and config pager set",
292+
env: map[string]string{
293+
"GH_PAGER": "GH_PAGER",
294+
},
295+
config: pagerConfig(),
296+
wantPager: "GH_PAGER",
297+
},
298+
{
299+
name: "config pager and PAGER set",
300+
env: map[string]string{
301+
"PAGER": "PAGER",
302+
},
303+
config: pagerConfig(),
304+
wantPager: "CONFIG_PAGER",
305+
},
306+
{
307+
name: "only PAGER set",
308+
env: map[string]string{
309+
"PAGER": "PAGER",
310+
},
311+
wantPager: "PAGER",
312+
},
313+
{
314+
name: "GH_PAGER set to blank string",
315+
env: map[string]string{
316+
"GH_PAGER": "",
317+
"PAGER": "PAGER",
318+
},
319+
wantPager: "",
320+
},
321+
}
322+
for _, tt := range tests {
323+
t.Run(tt.name, func(t *testing.T) {
324+
if tt.env != nil {
325+
for k, v := range tt.env {
326+
old := os.Getenv(k)
327+
os.Setenv(k, v)
328+
if k == "GH_PAGER" {
329+
defer os.Unsetenv(k)
330+
} else {
331+
defer os.Setenv(k, old)
332+
}
333+
}
334+
}
335+
f := New("1")
336+
if tt.config != nil {
337+
f.Config = func() (config.Config, error) {
338+
return tt.config, nil
339+
}
340+
}
341+
io := ioStreams(f)
342+
assert.Equal(t, tt.wantPager, io.GetPager())
343+
})
344+
}
345+
}
346+
347+
func Test_ioStreams_prompt(t *testing.T) {
348+
tests := []struct {
349+
name string
350+
config config.Config
351+
promptDisabled bool
352+
}{
353+
{
354+
name: "default config",
355+
promptDisabled: false,
356+
},
357+
{
358+
name: "config with prompt disabled",
359+
config: disablePromptConfig(),
360+
promptDisabled: true,
361+
},
362+
}
363+
for _, tt := range tests {
364+
t.Run(tt.name, func(t *testing.T) {
365+
f := New("1")
366+
if tt.config != nil {
367+
f.Config = func() (config.Config, error) {
368+
return tt.config, nil
369+
}
370+
}
371+
io := ioStreams(f)
372+
assert.Equal(t, tt.promptDisabled, io.GetNeverPrompt())
373+
})
374+
}
375+
}
376+
275377
func defaultConfig() config.Config {
276378
return config.InheritEnv(config.NewFromString(heredoc.Doc(`
277379
hosts:
278380
nonsense.com:
279381
oauth_token: BLAH
280382
`)))
281383
}
384+
385+
func pagerConfig() config.Config {
386+
return config.NewFromString("pager: CONFIG_PAGER")
387+
}
388+
389+
func disablePromptConfig() config.Config {
390+
return config.NewFromString("prompt: disabled")
391+
}

pkg/iostreams/iostreams.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,10 @@ func (s *IOStreams) SetPager(cmd string) {
140140
s.pagerCommand = cmd
141141
}
142142

143+
func (s *IOStreams) GetPager() string {
144+
return s.pagerCommand
145+
}
146+
143147
func (s *IOStreams) StartPager() error {
144148
if s.pagerCommand == "" || s.pagerCommand == "cat" || !s.IsStdoutTTY() {
145149
return nil
@@ -202,6 +206,10 @@ func (s *IOStreams) CanPrompt() bool {
202206
return s.IsStdinTTY() && s.IsStdoutTTY()
203207
}
204208

209+
func (s *IOStreams) GetNeverPrompt() bool {
210+
return s.neverPrompt
211+
}
212+
205213
func (s *IOStreams) SetNeverPrompt(v bool) {
206214
s.neverPrompt = v
207215
}
@@ -281,16 +289,14 @@ func System() *IOStreams {
281289
stdoutIsTTY := isTerminal(os.Stdout)
282290
stderrIsTTY := isTerminal(os.Stderr)
283291

284-
pagerCommand := os.Getenv("PAGER")
285-
286292
io := &IOStreams{
287293
In: os.Stdin,
288294
originalOut: os.Stdout,
289295
Out: colorable.NewColorable(os.Stdout),
290296
ErrOut: colorable.NewColorable(os.Stderr),
291297
colorEnabled: EnvColorForced() || (!EnvColorDisabled() && stdoutIsTTY),
292298
is256enabled: Is256ColorSupported(),
293-
pagerCommand: pagerCommand,
299+
pagerCommand: os.Getenv("PAGER"),
294300
}
295301

296302
if stdoutIsTTY && stderrIsTTY {

0 commit comments

Comments
 (0)
X Tutup