Skip to content

Commit

Permalink
🐛 Fix terminal width always being set to 80
Browse files Browse the repository at this point in the history
  • Loading branch information
gabe565 committed Mar 24, 2022
1 parent b788539 commit d7c2d5d
Show file tree
Hide file tree
Showing 8 changed files with 266 additions and 48 deletions.
27 changes: 21 additions & 6 deletions cmd/dump/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@ import (
"github.com/clevyr/kubedb/internal/database/sqlformat"
"github.com/clevyr/kubedb/internal/progressbar"
"github.com/clevyr/kubedb/internal/util"
"github.com/docker/cli/cli/streams"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"io"
_ "k8s.io/client-go/plugin/pkg/client/auth"
"k8s.io/client-go/tools/remotecommand"
"k8s.io/kubectl/pkg/util/term"
"os"
"path/filepath"
"strconv"
Expand Down Expand Up @@ -155,13 +156,27 @@ func run(cmd *cobra.Command, args []string) (err error) {
}
}()

stdin := streams.NewIn(os.Stdin)
if err := stdin.SetRawTerminal(); err != nil {
return err
t := term.TTY{
In: os.Stdin,
Out: pw,
}
t.Raw = t.IsTerminalIn()
var sizeQueue remotecommand.TerminalSizeQueue
if t.Raw {
sizeQueue = t.MonitorSize(t.GetSize())
}

err = conf.Client.Exec(conf.Pod, buildCommand(conf.Grammar, conf), stdin, pw, stdin.IsTerminal())
stdin.RestoreTerminal()
err = t.Safe(func() error {
return conf.Client.Exec(
conf.Pod,
buildCommand(conf.Grammar, conf),
t.In,
t.Out,
os.Stderr,
t.IsTerminalIn(),
sizeQueue,
)
})
if err != nil {
_ = pw.CloseWithError(err)
return err
Expand Down
27 changes: 21 additions & 6 deletions cmd/exec/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import (
"github.com/clevyr/kubedb/internal/command"
"github.com/clevyr/kubedb/internal/config"
"github.com/clevyr/kubedb/internal/util"
"github.com/docker/cli/cli/streams"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
_ "k8s.io/client-go/plugin/pkg/client/auth"
"k8s.io/client-go/tools/remotecommand"
"k8s.io/kubectl/pkg/util/term"
"os"
)

Expand All @@ -28,13 +29,27 @@ func preRun(cmd *cobra.Command, args []string) error {
func run(cmd *cobra.Command, args []string) (err error) {
log.WithField("pod", conf.Pod.Name).Info("exec into pod")

stdin := streams.NewIn(os.Stdin)
if err := stdin.SetRawTerminal(); err != nil {
return err
t := term.TTY{
In: os.Stdin,
Out: os.Stdout,
}
t.Raw = t.IsTerminalIn()
var sizeQueue remotecommand.TerminalSizeQueue
if t.Raw {
sizeQueue = t.MonitorSize(t.GetSize())
}
defer stdin.RestoreTerminal()

return conf.Client.Exec(conf.Pod, buildCommand(conf.Grammar, conf, args), stdin, os.Stdout, stdin.IsTerminal())
return t.Safe(func() error {
return conf.Client.Exec(
conf.Pod,
buildCommand(conf.Grammar, conf, args),
t.In,
t.Out,
os.Stderr,
t.IsTerminalIn(),
sizeQueue,
)
})
}

func buildCommand(db config.Databaser, conf config.Exec, args []string) []string {
Expand Down
2 changes: 1 addition & 1 deletion cmd/restore/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ func runInDatabasePod(r *io.PipeReader, ch chan error, inputFormat sqlformat.For
_ = pr.Close()
}(r)

err = conf.Client.Exec(conf.Pod, buildCommand(conf, inputFormat), r, os.Stdout, false)
err = conf.Client.Exec(conf.Pod, buildCommand(conf, inputFormat), r, os.Stdout, os.Stderr, false, nil)
if err != nil {
_ = r.CloseWithError(err)
ch <- err
Expand Down
14 changes: 7 additions & 7 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,18 @@ go 1.18

require (
github.com/AlecAivazis/survey/v2 v2.3.2
github.com/docker/cli v20.10.11+incompatible
github.com/jedib0t/go-pretty/v6 v6.2.7
github.com/schollz/progressbar/v3 v3.8.6
github.com/sirupsen/logrus v1.8.1
github.com/spf13/cobra v1.2.1
github.com/spf13/pflag v1.0.5
github.com/spf13/viper v1.8.1
golang.org/x/sys v0.0.0-20220128215802-99c3d69c2c27
k8s.io/api v0.23.0
k8s.io/apimachinery v0.23.0
k8s.io/client-go v0.23.0
gopkg.in/alessio/shellescape.v1 v1.0.0-20170105083845-52074bc9df61
k8s.io/api v0.23.5
k8s.io/apimachinery v0.23.5
k8s.io/client-go v0.23.5
k8s.io/kubectl v0.23.5
)

require (
Expand Down Expand Up @@ -48,6 +49,7 @@ require (
github.com/mattn/go-runewidth v0.0.13 // indirect
github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b // indirect
github.com/mitchellh/colorstring v0.0.0-20190213212951-d06e56a500db // indirect
github.com/mitchellh/go-wordwrap v1.0.0 // indirect
github.com/mitchellh/mapstructure v1.4.1 // indirect
github.com/moby/spdystream v0.2.0 // indirect
github.com/moby/term v0.0.0-20210619224110-3f7ff695adc6 // indirect
Expand All @@ -69,16 +71,14 @@ require (
golang.org/x/time v0.0.0-20211116232009-f0f3c7e86c11 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/protobuf v1.27.1 // indirect
gopkg.in/alessio/shellescape.v1 v1.0.0-20170105083845-52074bc9df61 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/ini.v1 v1.62.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
gotest.tools/v3 v3.0.3 // indirect
k8s.io/klog/v2 v2.30.0 // indirect
k8s.io/kube-openapi v0.0.0-20211115234752-e816edb12b65 // indirect
k8s.io/utils v0.0.0-20211208161948-7d6a63dca704 // indirect
sigs.k8s.io/json v0.0.0-20211208200746-9f7c6b3444d2 // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.2.0 // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.2.1 // indirect
sigs.k8s.io/yaml v1.3.0 // indirect
)
Loading

0 comments on commit d7c2d5d

Please sign in to comment.