From 1bab65ad6758f7f262e20683c36a7de192f46d27 Mon Sep 17 00:00:00 2001 From: Emin Date: Wed, 27 Sep 2023 17:17:31 +0300 Subject: [PATCH] Success notification for credential validation --- internal/router.go | 1 + internal/sync.go | 58 ++++++++++++++++++++++++++++++++++++++++++ internal/transfer.go | 26 ------------------- internal/validate.go | 2 +- templates/index.html | 10 ++++---- templates/success.html | 20 +++++++++++++++ 6 files changed, 85 insertions(+), 32 deletions(-) create mode 100644 internal/sync.go delete mode 100644 internal/transfer.go create mode 100644 templates/success.html diff --git a/internal/router.go b/internal/router.go index 34e8e77..af91ecf 100644 --- a/internal/router.go +++ b/internal/router.go @@ -25,6 +25,7 @@ func InitServer() { router.GET("/api/pagination", handlePagination) router.POST("/api/validate", handleValidate) router.POST("/api/search", handleSearch) + router.POST("/api/sync", handleSync) log.Info("Server starting on http://localhost:" + *port) diff --git a/internal/sync.go b/internal/sync.go new file mode 100644 index 0000000..b98a153 --- /dev/null +++ b/internal/sync.go @@ -0,0 +1,58 @@ +package internal + +import ( + "bytes" + "io" + "os" + "os/exec" + + "github.com/gin-gonic/gin" +) + +func handleSync(ctx *gin.Context) { + sourceServer := ctx.PostForm("sourceServer") + sourceAccount := ctx.PostForm("sourceAccount") + sourcePassword := ctx.PostForm("sourcePassword") + destinationServer := ctx.PostForm("destinationServer") + destinationAccount := ctx.PostForm("destinationAccount") + destinationPassword := ctx.PostForm("destinationPassword") + + sourceDetails := Credentials{ + Server: sourceServer, + Account: sourceAccount, + Password: sourcePassword, + } + + destinationDetails := Credentials{ + Server: destinationServer, + Account: destinationAccount, + Password: destinationPassword, + } + + log.Infof("Syncing %s to %s", sourceDetails.Account, destinationDetails.Account) + + err := syncIMAP(sourceDetails, destinationDetails) + if err != nil { + ctx.HTML(200, "error.html", err.Error()) + return + } + ctx.HTML(200, "success.html", "Synced "+sourceDetails.Account+" to "+destinationDetails.Account) +} + +func syncIMAP(sourceDetails Credentials, destinationDetails Credentials) error { + cmd := exec.Command("imapsync", "--host1", sourceDetails.Server, "--user1", sourceDetails.Account, "--password1", sourceDetails.Password, "--host2", destinationDetails.Server, "--user2", destinationDetails.Account, "--password2", destinationDetails.Password) + var stdBuffer bytes.Buffer + mw := io.MultiWriter(os.Stdout, &stdBuffer) + + cmd.Stdout = mw + cmd.Stderr = mw + + if err := cmd.Run(); err != nil { + return err + } + + // Command output realtime + // log.Println(stdBuffer.String()) + + return nil +} diff --git a/internal/transfer.go b/internal/transfer.go deleted file mode 100644 index da8d5ff..0000000 --- a/internal/transfer.go +++ /dev/null @@ -1,26 +0,0 @@ -package internal - -import ( - "bytes" - "io" - "os" - "os/exec" -) - -func syncIMAP(sourceDetails Credentials, destinationDetails Credentials) error { - cmd := exec.Command("imapsync", "--host1", sourceDetails.Server, "--user1", sourceDetails.Account, "--password1", sourceDetails.Password, "--host2", destinationDetails.Server, "--user2", destinationDetails.Account, "--password2", destinationDetails.Password) - var stdBuffer bytes.Buffer - mw := io.MultiWriter(os.Stdout, &stdBuffer) - - cmd.Stdout = mw - cmd.Stderr = mw - - if err := cmd.Run(); err != nil { - return err - } - - // Command output realtime - // log.Println(stdBuffer.String()) - - return nil -} diff --git a/internal/validate.go b/internal/validate.go index 87593fd..7d734ce 100644 --- a/internal/validate.go +++ b/internal/validate.go @@ -25,6 +25,7 @@ func handleValidate(ctx *gin.Context) { ctx.HTML(200, "error.html", err.Error()) return } + ctx.HTML(200, "success.html", creds) } func validateCredentials(creds Credentials) error { @@ -49,7 +50,6 @@ func validateCredentials(creds Credentials) error { // Login if err := c.Login(creds.Account, creds.Password); err != nil { - log.Info(c) log.Error(err) return err } diff --git a/templates/index.html b/templates/index.html index 86d4faa..0e7df40 100644 --- a/templates/index.html +++ b/templates/index.html @@ -56,6 +56,9 @@
+ + +

Source Details

@@ -117,13 +120,10 @@

Destination Details

Log Output

- +
-
- -
+
diff --git a/templates/success.html b/templates/success.html new file mode 100644 index 0000000..77b436c --- /dev/null +++ b/templates/success.html @@ -0,0 +1,20 @@ +
+ + + + +
+
+ Success + {{if .Account}}Authentication successful for: + {{.Account}}{{else}}{{.}}{{end}} +
+ + + +
+
\ No newline at end of file