Skip to content

Commit

Permalink
refactor(client): change output flag behavior for multiple file and s…
Browse files Browse the repository at this point in the history
…ingle file
  • Loading branch information
ikr4-m committed Aug 16, 2024
1 parent cb8f3b7 commit 1bafc1f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
7 changes: 4 additions & 3 deletions cmd/mdrop-client/get_command.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func GetCommand(args []string) {
flag := flag.NewFlagSet("mdrop get", flag.ExitOnError)
var (
help = flag.Bool("help", false, "Print this message")
fileNameOpt = flag.String("output", "", "Set filename output")
fileNameOpt = flag.String("output", "", "Set file or directory output")
localPort = flag.Int("local-port", 6000, "Specified sender port remoted on local")
)
flag.Parse(args)
Expand Down Expand Up @@ -71,11 +71,12 @@ func GetCommand(args []string) {
GetTcpReadyConnect(*localPort)

// Downloading file
isSingleFile := len(sender.Files) < 2
for _, uuid := range sender.Files {
// No error checking needed.
fileName := GetPrompt(*localPort, uuid, *fileNameOpt)
fileName := GetPrompt(*localPort, uuid, *fileNameOpt, isSingleFile)
checksum := GetChecksum(*localPort, uuid)
filePath := GetDownload(*localPort, uuid, fileName)
filePath := GetDownload(*localPort, uuid, fileName, *fileNameOpt, isSingleFile)

// Check checksum
fmt.Println("Checking checksum...")
Expand Down
17 changes: 12 additions & 5 deletions cmd/mdrop-client/get_download.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func GetChecksum(localPort int, uuid string) string {
return string(checksumBytes)
}

func GetPrompt(localPort int, uuid string, fileNameOpt string) (filePath string) {
func GetPrompt(localPort int, uuid string, fileNameOpt string, isSingleFile bool) (filePath string) {
reader := bufio.NewReader(os.Stdin)
client := http.Client{}

Expand Down Expand Up @@ -72,9 +72,12 @@ func GetPrompt(localPort int, uuid string, fileNameOpt string) (filePath string)
if err != nil {
internal.PrintErrorWithExit("sendFileWorkDir", err, 1)
}
if fileNameOpt != "" {
fmt.Println("Changing filename to", fileNameOpt)
fileName = fileNameOpt
// Check if it's single file
if isSingleFile {
if fileNameOpt != "" {
fmt.Println("Changing filename to", fileNameOpt)
fileName = fileNameOpt
}
}
if fileStatus, _ := os.Stat(filePath+"/"+fileName); fileStatus != nil {
fmt.Print("There's duplicate file. Action? [(R)eplace/R(e)name/(C)ancel] [Default: R] -> ")
Expand Down Expand Up @@ -103,7 +106,7 @@ func GetPrompt(localPort int, uuid string, fileNameOpt string) (filePath string)
return fileName
}

func GetDownload(localPort int, uuid string, fileName string) string {
func GetDownload(localPort int, uuid string, fileName string, fileNameOpt string, isSingleFile bool) string {
client := http.Client{}

resp, err := client.Post(
Expand All @@ -126,6 +129,10 @@ func GetDownload(localPort int, uuid string, fileName string) string {
if err != nil {
internal.PrintErrorWithExit("sendFileWorkDir", err, 1)
}
if !isSingleFile {
filePath = fileNameOpt
fmt.Println("Changing working directory to", filePath)
}
filePath += "/"+fileName

// Create file
Expand Down

0 comments on commit 1bafc1f

Please sign in to comment.