Skip to content

Commit

Permalink
Merge pull request #8 from kha7iq/add-global-flags-disable-turncate
Browse files Browse the repository at this point in the history
add global flags to enable file name turncation
  • Loading branch information
kha7iq authored May 22, 2023
2 parents b71dfd0 + 14a4654 commit 9f84d52
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 10 deletions.
16 changes: 11 additions & 5 deletions cmd/from/from.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func FromServer() *cli.Command {
},
},
Action: func(ctx *cli.Context) error {
truncate := ctx.Bool("turncate")
u := ctx.Int("uid")
g := ctx.Int("gid")
uid, gid := helper.CheckUID(u, g)
Expand Down Expand Up @@ -85,14 +86,14 @@ func FromServer() *cli.Command {
}

for _, sf := range files {
if err = transferFile(nfs, sf, sf); err != nil {
if err = transferFile(nfs, sf, sf, truncate); err != nil {
log.Fatalf("fail to copy files with error %V", err)
}
}
}
if !isDirectory(nfs, dir) {

if err = transferFile(nfs, dir, dir); err != nil {
if err = transferFile(nfs, dir, dir, truncate); err != nil {
log.Fatalf("fail to transfer files %v", err)
}
}
Expand All @@ -102,7 +103,8 @@ func FromServer() *cli.Command {
}

// 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 {
func transferFile(nfs *nfs.Target, srcfile string, targetfile string, truncate bool) error {
var filePath string
sourceFile, err := nfs.Open(srcfile)
if err != nil {
log.Fatalf("error opening source file: %s", err.Error())
Expand All @@ -116,9 +118,13 @@ func transferFile(nfs *nfs.Target, srcfile string, targetfile string) error {

defer sourceFile.Close()

turncatedFilePath := helper.TruncateFileName(srcfile)
if !truncate {
filePath = srcfile
} else {
filePath = helper.TruncateFileName(srcfile)
}

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

wr, err := os.Create(targetfile)
if err != nil {
Expand Down
15 changes: 10 additions & 5 deletions cmd/to/to.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ func ToServer() *cli.Command {
},
},
Action: func(ctx *cli.Context) error {
truncate := ctx.Bool("turncate")
u := ctx.Int("uid")
g := ctx.Int("gid")
uid, gid := helper.CheckUID(u, g)
Expand Down Expand Up @@ -94,7 +95,7 @@ func ToServer() *cli.Command {
for _, targetfile := range files {
sf := filepath.Join(basePath, targetfile)
// Copy file to destination
if err = transferFile(nfs, sf, targetfile); err != nil {
if err = transferFile(nfs, sf, targetfile, truncate); err != nil {
log.Fatalf("fail to transfer files %v", err)
}
}
Expand Down Expand Up @@ -168,8 +169,8 @@ func getFoldersAndFiles(path string, basePath string) ([]string, []string, error
}

// 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 {

func transferFile(nfs *nfs.Target, srcfile string, targetfile string, turnication bool) error {
var filePath string
sourceFile, err := os.Open(srcfile)
if err != nil {
log.Fatalf("error opening source file: %s", err.Error())
Expand All @@ -183,9 +184,13 @@ func transferFile(nfs *nfs.Target, srcfile string, targetfile string) error {

defer sourceFile.Close()

turncatedFilePath := helper.TruncateFileName(srcfile)
if !turnication {
filePath = srcfile
} else {
filePath = helper.TruncateFileName(srcfile)
}

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

wr, err := nfs.OpenFile(targetfile, os.ModePerm)
if err != nil {
Expand Down
7 changes: 7 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ func main() {
Aliases: []string{"g"},
Usage: "GID is a globally applicable flag that can be utilized for write operations.",
},
&cli.BoolFlag{
Name: "turncate",
Aliases: []string{"tr"},
Usage: "Enable or disable truncation of long file names in progress bar",
Value: true,
EnvVars: []string{"NCP_FILENAME_TURNICATE"},
},
}
app.Version = version + " BuildDate: " + buildDate + " " + " CommitSHA: " + commitSHA
app.Usage = "provides a straightforward and efficient way to handle file transfers between the local machine and a NFS server."
Expand Down

0 comments on commit 9f84d52

Please sign in to comment.