Skip to content

Commit

Permalink
refactor: rename 'bar' package to 'ui', move AskPassword func
Browse files Browse the repository at this point in the history
  • Loading branch information
70sh1 committed Mar 15, 2024
1 parent ba0d79e commit e1da26d
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 34 deletions.
43 changes: 9 additions & 34 deletions eddy.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,17 @@ import (
"log"
"os"
"path/filepath"
"slices"
"strings"
"sync"
"sync/atomic"
"syscall"
"time"

"github.com/70sh1/eddy/bars"
"github.com/70sh1/eddy/core"
"github.com/70sh1/eddy/format"
"github.com/70sh1/eddy/pathutils"
"github.com/70sh1/eddy/ui"
"github.com/fatih/color"
"github.com/urfave/cli/v2"
"golang.org/x/term"
)

func main() {
Expand Down Expand Up @@ -92,28 +89,6 @@ func main() {
}
}

func askPassword(mode core.Mode, noEmojiAndColor bool) (string, error) {
fmt.Print(format.CondPrefix("🔑 ", "Password: ", noEmojiAndColor))
password, err := term.ReadPassword(int(syscall.Stdin))
if err != nil {
return "", err
}
fmt.Print("\r")
if mode == core.Encryption {
fmt.Print(format.CondPrefix("🔑 ", "Confirm password: ", noEmojiAndColor))
password2, err := term.ReadPassword(int(syscall.Stdin))
if err != nil {
return "", err
}
if !slices.Equal(password, password2) {
fmt.Print("\r")
return "", errors.New("passwords do not match")
}
}

return string(password), nil
}

func printDoneMessage(startTime time.Time, noEmojiAndColor bool) {
fmt.Println()
deltaTime := time.Since(startTime).Round(time.Millisecond)
Expand All @@ -136,7 +111,7 @@ func encrypt(cCtx *cli.Context) error {
return err
}
if password == "" && passGenLen == 0 {
if password, err = askPassword(core.Encryption, noEmojiAndColor); err != nil {
if password, err = ui.AskPassword(core.Encryption, noEmojiAndColor); err != nil {
return err
}
passGenLen = 6
Expand Down Expand Up @@ -169,7 +144,7 @@ func EncryptFiles(paths []string, outputDir, password string, overwrite, noEmoji
var wg sync.WaitGroup
var numProcessed atomic.Uint64

barPool, pbars := bars.NewPool(paths, noEmojiAndColor)
barPool, pbars := ui.NewBarPool(paths, noEmojiAndColor)
if err := barPool.Start(); err != nil {
return 0, err
}
Expand All @@ -186,11 +161,11 @@ func EncryptFiles(paths []string, outputDir, password string, overwrite, noEmoji
fileOut = filepath.Join(outputDir, filepath.Base(fileOut))
}
if _, err := os.Stat(fileOut); !errors.Is(err, os.ErrNotExist) && !overwrite {
bars.Fail(bar, errors.New("output already exists"), noEmojiAndColor)
ui.BarFail(bar, errors.New("output already exists"), noEmojiAndColor)
return
}
if err := core.EncryptFile(fileIn, fileOut, password, bar); err != nil {
bars.Fail(bar, err, noEmojiAndColor)
ui.BarFail(bar, err, noEmojiAndColor)
return
}
bar.SetCurrent(bar.Total())
Expand All @@ -217,7 +192,7 @@ func decrypt(cCtx *cli.Context) error {
return err
}
if password == "" {
if password, err = askPassword(core.Decryption, noEmojiAndColor); err != nil {
if password, err = ui.AskPassword(core.Decryption, noEmojiAndColor); err != nil {
return err
}
}
Expand All @@ -234,7 +209,7 @@ func decrypt(cCtx *cli.Context) error {
func decryptFiles(paths []string, outputDir, password string, overwrite, noEmojiAndColor bool) error {
var wg sync.WaitGroup

barPool, pbars := bars.NewPool(paths, noEmojiAndColor)
barPool, pbars := ui.NewBarPool(paths, noEmojiAndColor)
if err := barPool.Start(); err != nil {
return err
}
Expand All @@ -251,11 +226,11 @@ func decryptFiles(paths []string, outputDir, password string, overwrite, noEmoji
fileOut = filepath.Join(outputDir, filepath.Base(fileOut))
}
if _, err := os.Stat(fileOut); !errors.Is(err, os.ErrNotExist) && !overwrite {
bars.Fail(bar, errors.New("output already exists"), noEmojiAndColor)
ui.BarFail(bar, errors.New("output already exists"), noEmojiAndColor)
return
}
if err := core.DecryptFile(fileIn, fileOut, password, bar); err != nil {
bars.Fail(bar, err, noEmojiAndColor)
ui.BarFail(bar, err, noEmojiAndColor)
return
}
bar.SetCurrent(bar.Total())
Expand Down
59 changes: 59 additions & 0 deletions ui/ui.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package ui

import (
"errors"
"fmt"
"path/filepath"
"slices"
"syscall"

"github.com/70sh1/eddy/core"
"github.com/70sh1/eddy/format"
"github.com/70sh1/eddy/pathutils"
"github.com/cheggaaa/pb/v3"
"github.com/fatih/color"
"golang.org/x/term"
)

// Creates new progress bar pool.
func NewBarPool(paths []string, noEmojiAndColor bool) (pool *pb.Pool, bars []*pb.ProgressBar) {
barTmpl := `{{ string . "status" }} {{ string . "filename" }} {{ string . "filesize" }} {{ bar . "[" "-" ">" " " "]" }} {{ string . "error" }}`
for _, path := range paths {
bar := pb.New64(1).SetTemplateString(barTmpl).SetWidth(90)
bar.Set("status", format.CondPrefix(" ", "", noEmojiAndColor))
bar.Set("filename", pathutils.FilenameOverflow(filepath.Base(path), 25))
bars = append(bars, bar)
}
return pb.NewPool(bars...), bars
}

func BarFail(bar *pb.ProgressBar, err error, noEmojiAndColor bool) {
errText := err.Error()
if !noEmojiAndColor {
errText = color.RedString(errText)
}
bar.Set("status", format.CondPrefix("❌", "", noEmojiAndColor))
bar.Set("error", errText)
}

func AskPassword(mode core.Mode, noEmojiAndColor bool) (string, error) {
fmt.Print(format.CondPrefix("🔑 ", "Password: ", noEmojiAndColor))
password, err := term.ReadPassword(int(syscall.Stdin))
if err != nil {
return "", err
}
fmt.Print("\r")
if mode == core.Encryption {
fmt.Print(format.CondPrefix("🔑 ", "Confirm password: ", noEmojiAndColor))
password2, err := term.ReadPassword(int(syscall.Stdin))
if err != nil {
return "", err
}
if !slices.Equal(password, password2) {
fmt.Print("\r")
return "", errors.New("passwords do not match")
}
}

return string(password), nil
}
24 changes: 24 additions & 0 deletions ui/ui_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package ui

import (
"path/filepath"
"testing"

"github.com/stretchr/testify/require"
)

func TestNewPool(t *testing.T) {
cases := [][]string{
{"file1", "path/file2.dat", "home/user/docs/file2"},
{"C:/some/dir/file1.txt", "path/file3", "home/user/docs/file2"},
{"file5"},
}
for _, tCase := range cases {
barPool, bars := NewBarPool(tCase, false)
require.Len(t, bars, len(tCase))
require.NotNil(t, barPool)
for i := 0; i < len(tCase); i++ {
require.Contains(t, bars[i].String(), filepath.Base(tCase[i]))
}
}
}

0 comments on commit e1da26d

Please sign in to comment.