Skip to content

Commit

Permalink
Success notification for credential validation
Browse files Browse the repository at this point in the history
  • Loading branch information
xeome committed Sep 27, 2023
1 parent 1468f58 commit 1bab65a
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 32 deletions.
1 change: 1 addition & 0 deletions internal/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
58 changes: 58 additions & 0 deletions internal/sync.go
Original file line number Diff line number Diff line change
@@ -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
}
26 changes: 0 additions & 26 deletions internal/transfer.go

This file was deleted.

2 changes: 1 addition & 1 deletion internal/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}
Expand Down
10 changes: 5 additions & 5 deletions templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@
<!-- imap sync form -->
<div class="container p-4 mx-auto flex gap-4 mt-4">


<!-- TODO: Single form only, use input instead of button, value for text -->

<!-- Source details -->
<div class="w-1/2 rounded-xl border border-border bg-backgroundSecondary rounded-lg p-4 shadow-md">
<h2 class="text-2xl font-semibold mb-4">Source Details</h2>
Expand Down Expand Up @@ -117,13 +120,10 @@ <h2 class="text-2xl font-semibold mb-4">Destination Details</h2>
<div class="w-full bg-backgroundSecondary p-4 shadow-md border border-border rounded-lg">
<div class="flex justify-between items-center mb-4">
<h2 class="text-2xl font-semibold">Log Output</h2>
<button class="bg-blue-500 hover:bg-blue-700 text-white font-semibold py-2 px-4 rounded"
id="start-sync-btn">Start synchronization</button>
<button class="btn btn-primary" id="start-sync-btn" form="">Start synchronization</button>
</div>
<div class="grid gap-4">
<div>
<textarea class="textarea-block textarea" rows="12" readonly></textarea>
</div>
<textarea class="textarea-block textarea" rows="12" readonly></textarea>
</div>
</div>
</div>
Expand Down
20 changes: 20 additions & 0 deletions templates/success.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<div class="alert alert-success max-w-sm absolute top-0 right-0 mt-4 mr-4 cursor-pointer" id="error-notification">
<svg width="48" height="48" viewBox="0 0 48 48" fill="none" xmlns="http://www.w3.org/2000/svg">
<path fill-rule="evenodd" clip-rule="evenodd"
d="M24 4C12.96 4 4 12.96 4 24C4 35.04 12.96 44 24 44C35.04 44 44 35.04 44 24C44 12.96 35.04 4 24 4ZM18.58 32.58L11.4 25.4C10.62 24.62 10.62 23.36 11.4 22.58C12.18 21.8 13.44 21.8 14.22 22.58L20 28.34L33.76 14.58C34.54 13.8 35.8 13.8 36.58 14.58C37.36 15.36 37.36 16.62 36.58 17.4L21.4 32.58C20.64 33.36 19.36 33.36 18.58 32.58Z"
fill="#00BA34" />
</svg>

<div class="flex w-full justify-between">
<div class="flex flex-col">
<span>Success</span>
<span class="text-content2">{{if .Account}}Authentication successful for:
{{.Account}}{{else}}{{.}}{{end}}</span>
</div>
<svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
<path fill-rule="evenodd" clip-rule="evenodd"
d="M18.3007 5.71C17.9107 5.32 17.2807 5.32 16.8907 5.71L12.0007 10.59L7.1107 5.7C6.7207 5.31 6.0907 5.31 5.7007 5.7C5.3107 6.09 5.3107 6.72 5.7007 7.11L10.5907 12L5.7007 16.89C5.3107 17.28 5.3107 17.91 5.7007 18.3C6.0907 18.69 6.7207 18.69 7.1107 18.3L12.0007 13.41L16.8907 18.3C17.2807 18.69 17.9107 18.69 18.3007 18.3C18.6907 17.91 18.6907 17.28 18.3007 16.89L13.4107 12L18.3007 7.11C18.6807 6.73 18.6807 6.09 18.3007 5.71Z"
fill="#969696" />
</svg>
</div>
</div>

0 comments on commit 1bab65a

Please sign in to comment.