Skip to content

Commit

Permalink
[Bug] Fix #2584
Browse files Browse the repository at this point in the history
  • Loading branch information
derailed committed Mar 5, 2024
1 parent 0f2894e commit 05e4a0a
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 47 deletions.
53 changes: 33 additions & 20 deletions internal/ui/dialog/transfer.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package dialog

import (
"strconv"
"strings"

"github.com/derailed/k9s/internal/config"
Expand All @@ -13,12 +14,19 @@ import (

const confirmKey = "confirm"

type TransferFn func(from, to, co string, download, no_preserve bool) bool
type TransferFn func(TransferArgs) bool

type TransferArgs struct {
From, To, CO string
Download, NoPreserve bool
Retries int
}

type TransferDialogOpts struct {
Containers []string
Pod string
Title, Message string
Retries int
Ack TransferFn
Cancel cancelFunc
}
Expand All @@ -38,44 +46,49 @@ func ShowUploads(styles config.Dialog, pages *ui.Pages, opts TransferDialogOpts)

modal := tview.NewModalForm("<"+opts.Title+">", f)

from, to := opts.Pod, ""
args := TransferArgs{
From: opts.Pod,
Retries: opts.Retries,
}
var fromField, toField *tview.InputField
download := true
f.AddCheckbox("Download:", download, func(_ string, flag bool) {
args.Download = true
f.AddCheckbox("Download:", args.Download, func(_ string, flag bool) {
if flag {
modal.SetText(strings.Replace(opts.Message, "Upload", "Download", 1))
} else {
modal.SetText(strings.Replace(opts.Message, "Download", "Upload", 1))
}
download = flag
from, to = to, from
fromField.SetText(from)
toField.SetText(to)
args.Download = flag
args.From, args.To = args.To, args.From
fromField.SetText(args.From)
toField.SetText(args.To)
})

f.AddInputField("From:", from, 40, nil, func(t string) {
from = t
f.AddInputField("From:", args.From, 40, nil, func(v string) {
args.From = v
})
f.AddInputField("To:", to, 40, nil, func(t string) {
to = t
f.AddInputField("To:", args.To, 40, nil, func(v string) {
args.To = v
})
fromField, _ = f.GetFormItemByLabel("From:").(*tview.InputField)
toField, _ = f.GetFormItemByLabel("To:").(*tview.InputField)

var no_preserve bool
f.AddCheckbox("NoPreserve:", no_preserve, func(_ string, f bool) {
no_preserve = f
f.AddCheckbox("NoPreserve:", args.NoPreserve, func(_ string, f bool) {
args.NoPreserve = f
})
var co string
if len(opts.Containers) > 0 {
co = opts.Containers[0]
args.CO = opts.Containers[0]
}
f.AddInputField("Container:", co, 30, nil, func(t string) {
co = t
f.AddInputField("Container:", args.CO, 30, nil, func(v string) {
args.CO = v
})
retries := strconv.Itoa(opts.Retries)
f.AddInputField("Retries:", retries, 30, nil, func(v string) {
retries = v
})

f.AddButton("OK", func() {
if !opts.Ack(from, to, co, download, no_preserve) {
if !opts.Ack(args) {
return
}
dismissConfirm(pages)
Expand Down
12 changes: 10 additions & 2 deletions internal/view/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,13 @@ func runK(a *App, opts shellOpts) error {
}
opts.binary = bin

suspended, errChan, _ := run(a, opts)
suspended, errChan, stChan := run(a, opts)
if !suspended {
return fmt.Errorf("unable to run command")
}
for v := range stChan {
log.Debug().Msgf(" - %s", v)
}
var errs error
for e := range errChan {
errs = errors.Join(errs, e)
Expand Down Expand Up @@ -474,7 +477,7 @@ func asResource(r config.Limits) v1.ResourceRequirements {
}
}

func pipe(_ context.Context, opts shellOpts, statusChan chan<- string, w, e io.Writer, cmds ...*exec.Cmd) error {
func pipe(_ context.Context, opts shellOpts, statusChan chan<- string, w, e *bytes.Buffer, cmds ...*exec.Cmd) error {
if len(cmds) == 0 {
return nil
}
Expand All @@ -487,6 +490,11 @@ func pipe(_ context.Context, opts shellOpts, statusChan chan<- string, w, e io.W
if err := cmd.Run(); err != nil {
log.Error().Err(err).Msgf("Command failed: %s", err)
} else {
for _, l := range strings.Split(w.String(), "\n") {
if l != "" {
statusChan <- fmt.Sprintf("[output] %s", l)
}
}
statusChan <- fmt.Sprintf("Command completed successfully: %q", render.Truncate(cmd.String(), 20))
log.Info().Msgf("Command completed successfully: %q", cmd.String())
}
Expand Down
2 changes: 1 addition & 1 deletion internal/view/pf_dialog.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ func ShowPortForwards(v ResourceViewer, path string, ports port.ContainerPortSpe
log.Error().Err(err).Msgf("No active context detected")
return
}
address := ct.PortForwardAddress

pf, err := aa.PreferredPorts(ports)
if err != nil {
Expand All @@ -62,6 +61,7 @@ func ShowPortForwards(v ResourceViewer, path string, ports port.ContainerPortSpe
if loField.GetText() == "" {
loField.SetPlaceholder("Enter a local port")
}
address := ct.PortForwardAddress
f.AddInputField("Address:", address, fieldLen, nil, func(h string) {
address = h
})
Expand Down
50 changes: 26 additions & 24 deletions internal/view/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,14 @@ import (
)

const (
windowsOS = "windows"
powerShell = "powershell"
osSelector = "kubernetes.io/os"
osBetaSelector = "beta." + osSelector
trUpload = "Upload"
trDownload = "Download"
pfIndicator = "[orange::b]Ⓕ"
windowsOS = "windows"
powerShell = "powershell"
osSelector = "kubernetes.io/os"
osBetaSelector = "beta." + osSelector
trUpload = "Upload"
trDownload = "Download"
pfIndicator = "[orange::b]Ⓕ"
defaultTxRetries = 999
)

// Pod represents a pod viewer.
Expand Down Expand Up @@ -310,36 +311,36 @@ func (p *Pod) transferCmd(evt *tcell.EventKey) *tcell.EventKey {
}

ns, n := client.Namespaced(path)
ack := func(from, to, co string, download, no_preserve bool) bool {
local := to
if !download {
local = from
ack := func(args dialog.TransferArgs) bool {
local := args.To
if !args.Download {
local = args.From
}
if _, err := os.Stat(local); !download && errors.Is(err, fs.ErrNotExist) {
if _, err := os.Stat(local); !args.Download && errors.Is(err, fs.ErrNotExist) {
p.App().Flash().Err(err)
return false
}

args := make([]string, 0, 10)
args = append(args, "cp")
args = append(args, strings.TrimSpace(from))
args = append(args, strings.TrimSpace(to))
args = append(args, fmt.Sprintf("--no-preserve=%t", no_preserve))
if co != "" {
args = append(args, "-c="+co)
opts := make([]string, 0, 10)
opts = append(opts, "cp")
opts = append(opts, strings.TrimSpace(args.From))
opts = append(opts, strings.TrimSpace(args.To))
opts = append(opts, fmt.Sprintf("--no-preserve=%t", args.NoPreserve))
if args.CO != "" {
opts = append(opts, "-c="+args.CO)
}

opts := shellOpts{
cliOpts := shellOpts{
background: true,
args: args,
args: opts,
}
op := trUpload
if download {
if args.Download {
op = trDownload
}

fqn := path + ":" + co
if err := runK(p.App(), opts); err != nil {
fqn := path + ":" + args.CO
if err := runK(p.App(), cliOpts); err != nil {
p.App().cowCmd(err.Error())
} else {
p.App().Flash().Infof("%s successful on %s!", op, fqn)
Expand All @@ -359,6 +360,7 @@ func (p *Pod) transferCmd(evt *tcell.EventKey) *tcell.EventKey {
Message: "Download Files",
Pod: fmt.Sprintf("%s/%s:", ns, n),
Ack: ack,
Retries: defaultTxRetries,
Cancel: func() {},
}
dialog.ShowUploads(p.App().Styles.Dialog(), p.App().Content.Pages, opts)
Expand Down

0 comments on commit 05e4a0a

Please sign in to comment.