From ce1a5489f8eb2677ff210806db64eb1d12b79561 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Chris=20Suszy=C5=84ski?= Date: Tue, 26 Mar 2024 12:16:17 +0100 Subject: [PATCH] Safe guard for charmbracelet/bubbletea#964 --- pkg/output/tui/bubbletea964.go | 49 ++++++++++++++++++ pkg/output/tui/bubbletea964_test.go | 77 +++++++++++++++++++++++++++++ pkg/output/tui/progress.go | 2 +- pkg/output/tui/spinner.go | 2 +- 4 files changed, 128 insertions(+), 2 deletions(-) create mode 100644 pkg/output/tui/bubbletea964.go create mode 100644 pkg/output/tui/bubbletea964_test.go diff --git a/pkg/output/tui/bubbletea964.go b/pkg/output/tui/bubbletea964.go new file mode 100644 index 00000000..76c2d628 --- /dev/null +++ b/pkg/output/tui/bubbletea964.go @@ -0,0 +1,49 @@ +/* + Copyright 2024 The Knative Authors + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +package tui + +import ( + "io" + "log" + "os" +) + +// safeguardBubbletea964 will safeguard the io.Reader by returning a new +// io.Reader that will prevent the +// https://github.com/charmbracelet/bubbletea/issues/964 issue. +// +// TODO: Remove this function once the issue is resolved. +func safeguardBubbletea964(in io.Reader) io.Reader { + if in == nil { + return nil + } + if f, ok := in.(*os.File); ok { + if st, err := f.Stat(); err != nil { + log.Fatal("unexpected: ", err) + } else { + if !st.Mode().IsRegular() { + if st.Name() != os.DevNull { + log.Println("WARN: non-regular file given as input: ", + st.Name(), " (mode: ", st.Mode(), + "). Using `nil` as input.") + } + return nil + } + } + } + return in +} diff --git a/pkg/output/tui/bubbletea964_test.go b/pkg/output/tui/bubbletea964_test.go new file mode 100644 index 00000000..4c66247b --- /dev/null +++ b/pkg/output/tui/bubbletea964_test.go @@ -0,0 +1,77 @@ +/* + Copyright 2024 The Knative Authors + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +*/ + +package tui + +import ( + "io" + "os" + "testing" + + "gotest.tools/v3/assert" +) + +func TestSafeguardBubbletea964(t *testing.T) { + t.Parallel() + tmp := t.TempDir() + assert.NilError(t, os.WriteFile(tmp+"/file", []byte("test"), 0o600)) + tf := openFile(t, tmp+"/file") + tcs := []safeguardBubbletea964TestCase{{ + name: "nil input", + in: nil, + want: nil, + }, { + name: "non-regular file", + in: os.NewFile(0, "/"), + want: nil, + }, { + name: "regular file", + in: tf, + want: tf, + }, { + name: "dev null", + in: openFile(t, os.DevNull), + want: nil, + }} + for _, tc := range tcs { + tc := tc + t.Run(tc.name, func(t *testing.T) { + t.Parallel() + if tf, ok := tc.in.(*os.File); ok { + defer func(tf *os.File) { + _ = tf.Close() + }(tf) + } + + got := safeguardBubbletea964(tc.in) + assert.Equal(t, got, tc.want) + }) + + } +} + +type safeguardBubbletea964TestCase struct { + name string + in io.Reader + want io.Reader +} + +func openFile(tb testing.TB, name string) *os.File { + tb.Helper() + f, err := os.Open(name) + assert.NilError(tb, err) + return f +} diff --git a/pkg/output/tui/progress.go b/pkg/output/tui/progress.go index 925bdd47..51d66b67 100644 --- a/pkg/output/tui/progress.go +++ b/pkg/output/tui/progress.go @@ -243,7 +243,7 @@ func (b *BubbleProgress) start() { b.prog = progress.New(progress.WithDefaultGradient()) out := b.OutOrStdout() b.tea = tea.NewProgram(b, - tea.WithInput(b.InOrStdin()), + tea.WithInput(safeguardBubbletea964(b.InOrStdin())), tea.WithOutput(out), ) b.quitChan = make(chan struct{}) diff --git a/pkg/output/tui/spinner.go b/pkg/output/tui/spinner.go index 6f44c4f5..9c0665bb 100644 --- a/pkg/output/tui/spinner.go +++ b/pkg/output/tui/spinner.go @@ -80,7 +80,7 @@ func (b *BubbleSpinner) start() { ) out := b.OutOrStdout() b.tea = tea.NewProgram(b, - tea.WithInput(b.InOrStdin()), + tea.WithInput(safeguardBubbletea964(b.InOrStdin())), tea.WithOutput(out), ) b.quitChan = make(chan struct{})