Skip to content

Commit

Permalink
fix: fix #3 for windows WSL when operate accross different file system
Browse files Browse the repository at this point in the history
  • Loading branch information
fredliang44 committed Jul 27, 2022
1 parent 74e0229 commit 5c5f006
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 48 deletions.
47 changes: 3 additions & 44 deletions cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,9 @@ package cmd
import (
"errors"
"fmt"
"io"
"github.com/let-sh/cli/utils"
"io/ioutil"
"os"
"path/filepath"
"strings"

"github.com/let-sh/cli/log"
Expand Down Expand Up @@ -81,7 +80,8 @@ e.g.:
return
}
// mv to current folder
err := moveDirectory(fmt.Sprintf("%s/%s", tempDir, projectType), fmt.Sprintf("%s/%s", currentDir, folderName))
err := utils.Move(fmt.Sprintf("%s/%s", tempDir, projectType), fmt.Sprintf("%s/%s", currentDir,
folderName))
if err != nil {
log.Error(errors.New("cannot init project to current folder: " + err.Error()))
//logrus.Debug("current project dir: ", pwd)
Expand All @@ -97,47 +97,6 @@ e.g.:
},
}

func moveDirectory(src, dst string) error {
err := os.MkdirAll(dst, 0755)
if err != nil {
return err
}
files, err := ioutil.ReadDir(src)
if err != nil {
return err
}
for _, file := range files {
srcfp := filepath.Join(src, file.Name())
dstfp := filepath.Join(dst, file.Name())
if file.IsDir() {
moveDirectory(srcfp, dstfp)
} else {
moveFile(srcfp, dstfp)
}
}
return nil
}

func moveFile(src, dst string) error {
in, err := os.Open(src)
if err != nil {
return err
}
defer in.Close()

out, err := os.Create(dst)
if err != nil {
return err
}
defer out.Close()

_, err = io.Copy(out, in)
if err != nil {
return err
}
return out.Close()
}

func init() {
rootCmd.AddCommand(initCmd)

Expand Down
61 changes: 61 additions & 0 deletions utils/files.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package utils

import (
"io"
"io/ioutil"
"os"
"path/filepath"
)

func GetFilesSize(paths []string) (int64, error) {
Expand All @@ -24,3 +27,61 @@ func fileSize(path string) (int64, error) {

return fi.Size(), nil
}

func Move(src, dst string) error {
var err error
// This returns an *os.FileInfo type
fileInfo, err := os.Stat(src)
if err != nil {
return err
}

// IsDir is short for fileInfo.Mode().IsDir()
if fileInfo.IsDir() {
err = MoveDirectory(src, dst)
} else {
err = MoveFile(src, dst)
}
return err
}

func MoveDirectory(src, dst string) error {
err := os.MkdirAll(dst, 0755)
if err != nil {
return err
}
files, err := ioutil.ReadDir(src)
if err != nil {
return err
}
for _, file := range files {
srcfp := filepath.Join(src, file.Name())
dstfp := filepath.Join(dst, file.Name())
if file.IsDir() {
MoveDirectory(srcfp, dstfp)
} else {
MoveFile(srcfp, dstfp)
}
}
return nil
}

func MoveFile(src, dst string) error {
in, err := os.Open(src)
if err != nil {
return err
}
defer in.Close()

out, err := os.Create(dst)
if err != nil {
return err
}
defer out.Close()

_, err = io.Copy(out, in)
if err != nil {
return err
}
return out.Close()
}
7 changes: 4 additions & 3 deletions utils/update/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/let-sh/cli/info"
"github.com/let-sh/cli/log"
"github.com/let-sh/cli/requests"
"github.com/let-sh/cli/utils"
"github.com/sirupsen/logrus"
"github.com/vbauerster/mpb/v7"
"github.com/vbauerster/mpb/v7/decor"
Expand Down Expand Up @@ -101,11 +102,11 @@ func UpgradeCli(force bool, channel string) {
if runtime.GOOS == "windows" {
// handle process error
// go further: https://stackoverflow.com/questions/9162969/how-can-a-c-binary-replace-itself
err = os.Rename(path, path+".old")
err = os.Rename(filepath.Join(unzipedDir, binaryName), path)
utils.Move(path, path+".old")
utils.Move(filepath.Join(unzipedDir, binaryName), path)
os.RemoveAll(path + ".old")
} else {
err = os.Rename(filepath.Join(unzipedDir, binaryName), path)
err = utils.Move(filepath.Join(unzipedDir, binaryName), path)
}

if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion utils/update/upgrades_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package update
import (
"github.com/let-sh/cli/info"
"github.com/let-sh/cli/requests"
"github.com/let-sh/cli/utils"
"github.com/sirupsen/logrus"
"os"
"os/exec"
Expand Down Expand Up @@ -57,7 +58,7 @@ func TestUpgradeCli(t *testing.T) {
return
}

err = os.Rename(filepath.Join(tempDir, "lets"), path)
utils.MoveFile(filepath.Join(tempDir, "lets"), path)
if err != nil {
logrus.WithError(err).Debugln("get compressed file")
return
Expand Down

0 comments on commit 5c5f006

Please sign in to comment.