Skip to content

Commit

Permalink
Add file download feature
Browse files Browse the repository at this point in the history
  • Loading branch information
Greg0 committed Sep 13, 2021
1 parent aa6253b commit a96b0a3
Showing 1 changed file with 38 additions and 1 deletion.
39 changes: 38 additions & 1 deletion poddy.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func main() {
cmdType := ""
cmdTypePrompt := &survey.Select{
Message: "Choose action:",
Options: []string{"logs", "exec", "fileUpload"},
Options: []string{"logs", "exec", "fileUpload", "fileDownload"},
}
err = survey.AskOne(cmdTypePrompt, &cmdType)

Expand All @@ -56,6 +56,43 @@ func main() {
if cmdType == "fileUpload" {
uploadFile(selectedPods, cmd)
}
if cmdType == "fileDownload" {
downloadFile(selectedPods, cmd)
}
}

func downloadFile(selectedPods []string, cmd *exec.Cmd) {
targetLocation := "/"
sourceFile := ""
prompt := &survey.Input{
Message: "Remote file path:",
Default: sourceFile,
}
survey.AskOne(prompt, &sourceFile)

prompt = &survey.Input{
Message: "Target dir:",
Suggest: ListDirectories,
}
survey.AskOne(prompt, &targetLocation)
targetFilename := filepath.Base(sourceFile)

for _, podName := range selectedPods {
target := targetLocation + "/" + podName + "/" + targetFilename
commandToExec := "kubectl cp " + podName + ":" + sourceFile + " " + target
splitCommand := strings.Split(commandToExec, " ")
cmd = exec.Command(splitCommand[0], splitCommand[1:]...)
var out bytes.Buffer
var stderr bytes.Buffer
cmd.Stdout = &out
cmd.Stderr = &stderr
err := cmd.Run()
fmt.Println("[" + podName + "]")
if err != nil {
fmt.Println(fmt.Sprint(err) + ": " + stderr.String())
}
fmt.Println("File " + sourceFile + " successfully downloaded to " + target)
}
}

func uploadFile(selectedPods []string, cmd *exec.Cmd) {
Expand Down

0 comments on commit a96b0a3

Please sign in to comment.