Skip to content

Commit

Permalink
feat: check file path and turncate long file names
Browse files Browse the repository at this point in the history
  • Loading branch information
kha7iq committed May 22, 2023
1 parent 16b5165 commit bfe17b1
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 82 deletions.
47 changes: 6 additions & 41 deletions cmd/from/from.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@ package from
import (
"bytes"
"crypto/sha256"
"fmt"
"io"
"log"
"os"
"path/filepath"

"github.com/go-nfs/nfsv3/nfs"
"github.com/go-nfs/nfsv3/nfs/rpc"
"github.com/schollz/progressbar/v3"
"github.com/kha7iq/ncp/internal/helper"
"github.com/urfave/cli/v2"
)

Expand Down Expand Up @@ -46,7 +45,7 @@ func FromServer() *cli.Command {
Action: func(ctx *cli.Context) error {
u := ctx.Int("uid")
g := ctx.Int("gid")
uid, gid := checkUID(u, g)
uid, gid := helper.CheckUID(u, g)

rootDir := filepath.Dir(nc.nfsMountFolder)
dir := filepath.Base(nc.nfsMountFolder)
Expand Down Expand Up @@ -102,13 +101,7 @@ func FromServer() *cli.Command {
}
}

func checkMark() func() {
return func() {
fmt.Printf("%s ✔ %s\n", "\033[32m", "\033[0m")
}
}

// transferFile will take a source file path and target file path and transfer file
// transferFile will take a source and target file path along with *nfs.Targe to transfer file
func transferFile(nfs *nfs.Target, srcfile string, targetfile string) error {
sourceFile, err := nfs.Open(srcfile)
if err != nil {
Expand All @@ -123,24 +116,9 @@ func transferFile(nfs *nfs.Target, srcfile string, targetfile string) error {

defer sourceFile.Close()

// Customize the progress bar theme
theme := progressbar.Theme{
Saucer: "\x1b[38;5;215m▖[reset][cyan]",
SaucerPadding: " ",
}
progress := progressbar.NewOptions64(
size,
progressbar.OptionEnableColorCodes(true),
progressbar.OptionSetTheme(theme),
progressbar.OptionSetDescription("Copying"+" "+"[green]"+srcfile+"[reset]"),
progressbar.OptionSetWidth(20),
progressbar.OptionShowBytes(true),
progressbar.OptionOnCompletion(checkMark()),
progressbar.OptionShowCount(),
progressbar.OptionShowElapsedTimeOnFinish(),
progressbar.OptionSetPredictTime(false),
progressbar.OptionSpinnerType(14),
)
turncatedFilePath := helper.TruncateFileName(srcfile)

progress := helper.ProgressBar(size, turncatedFilePath, helper.CheckMark())

wr, err := os.Create(targetfile)
if err != nil {
Expand Down Expand Up @@ -234,16 +212,3 @@ func isDirectory(v *nfs.Target, dir string) bool {
}
return false
}

// checkUID will check the int to see if a value is provided via
// flags and convert it to uint32 and returns the values
func checkUID(u int, g int) (uid, gid uint32) {
if u == 0 {
uid = uint32(0)
gid = uint32(0)
} else {
uid = uint32(u)
gid = uint32(g)
}
return uid, gid
}
46 changes: 5 additions & 41 deletions cmd/to/to.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@ package to
import (
"bytes"
"crypto/sha256"
"fmt"
"io"
"log"
"os"
"path/filepath"

"github.com/go-nfs/nfsv3/nfs"
"github.com/go-nfs/nfsv3/nfs/rpc"
"github.com/schollz/progressbar/v3"
"github.com/kha7iq/ncp/internal/helper"
"github.com/urfave/cli/v2"
)

Expand Down Expand Up @@ -55,7 +54,7 @@ func ToServer() *cli.Command {
Action: func(ctx *cli.Context) error {
u := ctx.Int("uid")
g := ctx.Int("gid")
uid, gid := checkUID(u, g)
uid, gid := helper.CheckUID(u, g)

basePath := filepath.Dir(nc.inputPath)
mount, err := nfs.DialMount(nc.nfsHost, false)
Expand Down Expand Up @@ -104,12 +103,6 @@ func ToServer() *cli.Command {
}
}

func checkMark() func() {
return func() {
fmt.Printf("%s ✔ %s\n", "\033[32m", "\033[0m")
}
}

// isDirectory takes a path strings and check the attributes if givin path
// is a dirctory or not
func isDirectory(path string) (bool, error) {
Expand Down Expand Up @@ -174,7 +167,7 @@ func getFoldersAndFiles(path string, basePath string) ([]string, []string, error
return folders, files, nil
}

// transferFile will take a source file path and target file path and transfer file
// transferFile will take a source and target file path along with *nfs.Targe to transfer file
func transferFile(nfs *nfs.Target, srcfile string, targetfile string) error {

sourceFile, err := os.Open(srcfile)
Expand All @@ -190,25 +183,9 @@ func transferFile(nfs *nfs.Target, srcfile string, targetfile string) error {

defer sourceFile.Close()

// Customize the progress bar theme
theme := progressbar.Theme{
Saucer: "\x1b[38;5;215m▖[reset][cyan]",
SaucerPadding: " ",
}
turncatedFilePath := helper.TruncateFileName(srcfile)

progress := progressbar.NewOptions64(
size,
progressbar.OptionEnableColorCodes(true),
progressbar.OptionSetTheme(theme),
progressbar.OptionSetDescription("Copying"+" "+"[green]"+srcfile+"[reset]"),
progressbar.OptionSetWidth(20),
progressbar.OptionShowBytes(true),
progressbar.OptionOnCompletion(checkMark()),
progressbar.OptionShowCount(),
progressbar.OptionShowElapsedTimeOnFinish(),
progressbar.OptionSetPredictTime(false),
progressbar.OptionSpinnerType(14),
)
progress := helper.ProgressBar(size, turncatedFilePath, helper.CheckMark())

wr, err := nfs.OpenFile(targetfile, os.ModePerm)
if err != nil {
Expand Down Expand Up @@ -250,16 +227,3 @@ func transferFile(nfs *nfs.Target, srcfile string, targetfile string) error {
progress.Finish()
return nil
}

// checkUID will check the int to see if a value is provided via
// flags and convert it to uint32 and returns the values
func checkUID(u int, g int) (uid, gid uint32) {
if u == 0 {
uid = uint32(0)
gid = uint32(0)
} else {
uid = uint32(u)
gid = uint32(g)
}
return uid, gid
}

0 comments on commit bfe17b1

Please sign in to comment.