Skip to content

Commit

Permalink
🐛 Bubbletea initialization error handling (#164)
Browse files Browse the repository at this point in the history
* Bubbletea initialization error handling

* Safe guard for charmbracelet/bubbletea#964
  • Loading branch information
cardil committed Mar 26, 2024
1 parent cf1573f commit ca64d2d
Show file tree
Hide file tree
Showing 9 changed files with 162 additions and 22 deletions.
2 changes: 1 addition & 1 deletion go.mod
Expand Up @@ -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
Expand Down Expand Up @@ -116,7 +117,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
Expand Down
2 changes: 1 addition & 1 deletion pkg/output/logging/context.go
Expand Up @@ -132,7 +132,7 @@ func createDefaultLogger(ctx context.Context) *zap.Logger {
zapcore.AddSync(errout),
lvl,
))
if !term.IsTerminal(errout) {
if !term.IsFancy(errout) {
ec = zap.NewProductionEncoderConfig()
logger = zap.New(zapcore.NewCore(
zapcore.NewJSONEncoder(ec),
Expand Down
14 changes: 10 additions & 4 deletions pkg/output/term/io.go
Expand Up @@ -30,11 +30,17 @@ func IsReaderTerminal(r io.Reader) bool {
return ok && term.IsTerminal(int(f.Fd()))
}

// IsTerminal returns true if the given writer is a real terminal.
func IsTerminal(w io.Writer) bool {
// IsWriterTerminal returns true if the given writer is a real terminal.
func IsWriterTerminal(w io.Writer) bool {
f, ok := w.(*os.File)
return ok && term.IsTerminal(int(f.Fd()))
}

// IsFancy returns true if the given writer is a real terminal, or the color
// is forced by the environment variables.
func IsFancy(w io.Writer) bool {
if environment.ColorIsForced() {
return true
}
f, ok := w.(*os.File)
return ok && environment.NonColorRequested() && term.IsTerminal(int(f.Fd()))
return !environment.NonColorRequested() && IsWriterTerminal(w)
}
49 changes: 49 additions & 0 deletions 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
}
77 changes: 77 additions & 0 deletions 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
}
17 changes: 12 additions & 5 deletions pkg/output/tui/progress.go
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/charmbracelet/bubbles/progress"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"go.uber.org/multierr"
"knative.dev/client-pkg/pkg/output"
"knative.dev/client-pkg/pkg/output/term"
)
Expand Down Expand Up @@ -79,12 +80,16 @@ type BubbleProgress struct {
prevSpeed []int
err error
quitChan chan struct{}
teaErr error
}

func (b *BubbleProgress) With(fn func(ProgressControl) error) error {
b.start()
defer b.stop()
return fn(b)
err := func() error {
defer b.stop()
return fn(b)
}()
return multierr.Combine(err, b.err, b.teaErr)
}

func (b *BubbleProgress) Error(err error) {
Expand Down Expand Up @@ -238,15 +243,17 @@ 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{})
go func() {
t := b.tea
_, _ = t.Run()
if _, err := t.Run(); err != nil {
b.teaErr = err
}
close(b.quitChan)
if term.IsTerminal(out) {
if term.IsWriterTerminal(out) {
if err := t.ReleaseTerminal(); err != nil {
panic(err)
}
Expand Down
3 changes: 0 additions & 3 deletions pkg/output/tui/progress_test.go
Expand Up @@ -33,9 +33,6 @@ import (
)

func TestProgress(t *testing.T) {
if testing.Short() {
t.Skip("skipping test in short mode.")
}
ctx := context.TestContext(t)
prt := output.NewTestPrinter()
ctx = output.WithContext(ctx, prt)
Expand Down
17 changes: 12 additions & 5 deletions pkg/output/tui/spinner.go
Expand Up @@ -22,6 +22,7 @@ import (
"github.com/charmbracelet/bubbles/spinner"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"go.uber.org/multierr"
"knative.dev/client-pkg/pkg/output"
"knative.dev/client-pkg/pkg/output/term"
)
Expand All @@ -46,12 +47,16 @@ type BubbleSpinner struct {
spin spinner.Model
tea *tea.Program
quitChan chan struct{}
teaErr error
}

func (b *BubbleSpinner) With(fn func(Spinner) error) error {
b.start()
defer b.stop()
return fn(b)
err := func() error {
defer b.stop()
return fn(b)
}()
return multierr.Combine(err, b.teaErr)
}

func (b *BubbleSpinner) Init() tea.Cmd {
Expand All @@ -75,15 +80,17 @@ 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{})
go func() {
t := b.tea
_, _ = t.Run()
if _, err := t.Run(); err != nil {
b.teaErr = err
}
close(b.quitChan)
if term.IsTerminal(out) {
if term.IsWriterTerminal(out) {
_ = t.ReleaseTerminal()
}
}()
Expand Down
3 changes: 0 additions & 3 deletions pkg/output/tui/spinner_test.go
Expand Up @@ -26,9 +26,6 @@ import (
)

func TestSpinner(t *testing.T) {
if testing.Short() {
t.Skip("skipping test in short mode.")
}
ctx := context.TestContext(t)
prt := output.NewTestPrinter()
ctx = output.WithContext(ctx, prt)
Expand Down

0 comments on commit ca64d2d

Please sign in to comment.