Skip to content

Commit f1f4203

Browse files
refactor: add structure to data
add: argumnet based support
1 parent 51f6c3d commit f1f4203

File tree

4 files changed

+163
-55
lines changed

4 files changed

+163
-55
lines changed

ftpcli.go

Lines changed: 0 additions & 55 deletions
This file was deleted.

main.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package main
2+
3+
import (
4+
"flag"
5+
"gufcli/utils"
6+
"log"
7+
)
8+
9+
func main() {
10+
log.Println("Starting Connection")
11+
// Define flags
12+
source_ftp := flag.String("src", "", "Source ftp endpoint Example: ftp://user:pass@host/filepath")
13+
destination_ftp := flag.String("dest", "", "Destination ftp endpoint Example: ftp://user:pass@host/filepath")
14+
15+
flag.Parse()
16+
17+
utils.NullChecker("source", *source_ftp)
18+
utils.NullChecker("destination", *destination_ftp)
19+
20+
source_map_kv := utils.ParseInput("source", *source_ftp)
21+
destination_map_kv := utils.ParseInput("destination", *destination_ftp)
22+
23+
if utils.SchemeValidator(source_map_kv["scheme"]) && utils.SchemeValidator(destination_map_kv["scheme"]) {
24+
log.Println("Good Boy ")
25+
26+
// validate ftp parameters : panic on failure
27+
utils.FtpParamsValidator(source_map_kv)
28+
utils.FtpParamsValidator(destination_map_kv)
29+
30+
// Create Coonection and start streaming
31+
32+
utils.FtpClientHandler(source_map_kv, destination_map_kv)
33+
34+
} else {
35+
log.Panicln("Maradchod :: ? ftp hai dono")
36+
}
37+
}

utils/ftphandler.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package utils
2+
3+
import (
4+
"bytes"
5+
"io"
6+
"log"
7+
"time"
8+
9+
"github.com/jlaffaye/ftp"
10+
)
11+
12+
func FtpClientHandler(source map[string]string, destination map[string]string) {
13+
// Make connections
14+
source_ftp, err := ftp.Dial(source["host"], ftp.DialWithTimeout(5*time.Second))
15+
if err != nil {
16+
log.Fatal(err)
17+
} else {
18+
log.Println("Source Connected Successfully")
19+
}
20+
destination_ftp, err := ftp.Dial(destination["host"], ftp.DialWithTimeout(5*time.Second))
21+
if err != nil {
22+
log.Fatal(err)
23+
} else {
24+
log.Println("Destination Connected Successfully")
25+
}
26+
27+
// Login in using creds
28+
err = source_ftp.Login(source["username"], source["password"])
29+
if err != nil {
30+
log.Fatal(err)
31+
}
32+
err = destination_ftp.Login(destination["username"], destination["password"])
33+
if err != nil {
34+
log.Fatal(err)
35+
}
36+
37+
// start Reading from Source
38+
reader_ftp, err := source_ftp.Retr(source["path"])
39+
if err != nil {
40+
log.Fatal(err)
41+
}
42+
43+
// initalize a buffer of size 64Kb
44+
p := make([]byte, 64*1024)
45+
46+
for {
47+
n, err := reader_ftp.Read(p)
48+
49+
if err == io.EOF {
50+
break
51+
}
52+
53+
wdata := bytes.NewBufferString(string(p[:n]))
54+
err = destination_ftp.Append(destination["path"], wdata)
55+
if err != nil {
56+
log.Fatal(err)
57+
}
58+
59+
}
60+
// Close Source FTP
61+
defer reader_ftp.Close()
62+
}

utils/parsers.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package utils
2+
3+
import (
4+
"log"
5+
"net/url"
6+
)
7+
8+
func ParseInput(key string, furl string) map[string]string {
9+
// Define return variable
10+
11+
params := make(map[string]string)
12+
//
13+
14+
// Parse the URLs
15+
sourceURL, err := url.Parse(furl)
16+
if err != nil {
17+
log.Panicf("Error parsing source URL: %v\n", err)
18+
}
19+
20+
// Output the parsed URLs
21+
22+
// Extract and print the protocol (scheme)
23+
params["scheme"] = sourceURL.Scheme
24+
params["username"] = sourceURL.User.Username()
25+
sourcePassword, _ := sourceURL.User.Password()
26+
params["password"] = sourcePassword
27+
params["host"] = sourceURL.Host
28+
params["path"] = sourceURL.Path
29+
30+
// log the paramters used for connection
31+
// ToDo: sorted map implementation
32+
parameters := ""
33+
34+
for k, v := range params {
35+
parameters = parameters + k + ": " + v + ", "
36+
}
37+
38+
log.Println(key + ":: " + parameters)
39+
40+
return params
41+
}
42+
43+
func NullChecker(key string, ftpurl string) {
44+
// Check if string is null
45+
if ftpurl == "" {
46+
log.Panicf("Error: Empty URL for %s", key)
47+
}
48+
}
49+
50+
func SchemeValidator(scheme string) bool {
51+
log.Println("scheme: %s", scheme)
52+
if scheme == "ftp" {
53+
return true
54+
}
55+
return false
56+
}
57+
58+
func FtpParamsValidator(params map[string]string) {
59+
for k, v := range params {
60+
if v == "" {
61+
log.Panicf("Null Values passed for %v", k)
62+
}
63+
}
64+
}

0 commit comments

Comments
 (0)