diff --git a/go.mod b/go.mod index c5d52594..cfdb8606 100644 --- a/go.mod +++ b/go.mod @@ -16,6 +16,7 @@ require ( github.com/spf13/cobra v1.7.0 github.com/spf13/pflag v1.0.5 github.com/spf13/viper v1.13.0 + go.uber.org/multierr v1.11.0 go.uber.org/zap v1.27.0 golang.org/x/term v0.18.0 gotest.tools/v3 v3.3.0 @@ -25,6 +26,7 @@ require ( k8s.io/cli-runtime v0.29.2 k8s.io/client-go v0.29.2 k8s.io/code-generator v0.29.2 + k8s.io/klog/v2 v2.120.1 knative.dev/eventing v0.40.1-0.20240312170432-bbb9051e9fd0 knative.dev/hack v0.0.0-20240302114326-e6dedc74dc47 knative.dev/pkg v0.0.0-20240311204931-2c15a6fd07af @@ -116,7 +118,6 @@ require ( github.com/xlab/treeprint v1.2.0 // indirect go.opencensus.io v0.24.0 // indirect go.starlark.net v0.0.0-20230525235612-a134d8f9ddca // indirect - go.uber.org/multierr v1.11.0 // indirect golang.org/x/mod v0.16.0 // indirect golang.org/x/net v0.22.0 // indirect golang.org/x/oauth2 v0.18.0 // indirect @@ -137,7 +138,6 @@ require ( gopkg.in/yaml.v2 v2.4.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect k8s.io/gengo v0.0.0-20240129211411-f967bbeff4b4 // indirect - k8s.io/klog/v2 v2.120.1 // indirect k8s.io/kube-openapi v0.0.0-20231010175941-2dd684a91f00 // indirect k8s.io/utils v0.0.0-20240102154912-e7106e64919e // indirect knative.dev/networking v0.0.0-20240311132944-3252e63e3239 // indirect diff --git a/pkg/output/tui/bubbletee964.go b/pkg/output/tui/bubbletee964.go new file mode 100644 index 00000000..29b1146e --- /dev/null +++ b/pkg/output/tui/bubbletee964.go @@ -0,0 +1,51 @@ +/* + 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 ( + "bytes" + "io" + "os" + + "k8s.io/klog/v2" +) + +// safeguardBubbletee964 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 safeguardBubbletee964(in io.Reader) io.Reader { + if in == nil { + return nil + } + if f, ok := in.(*os.File); ok { + if st, err := f.Stat(); err == nil { + klog.Fatal("unexpected: ", err) + } else { + if !st.Mode().IsRegular() { + if st.Name() != os.DevNull { + klog.Warning("non-regular file given as input: ", + st.Name(), " (mode: ", st.Mode(), + "). Using `nil` as input.") + } + return bytes.NewBufferString("") + } + } + } + return in +} diff --git a/pkg/output/tui/progress.go b/pkg/output/tui/progress.go index 403816d0..7e7c5364 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(safeguardBubbletee964(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..76d5a34d 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(safeguardBubbletee964(b.InOrStdin())), tea.WithOutput(out), ) b.quitChan = make(chan struct{})