Skip to content

Commit 5c5f006

Browse files
committed
fix: fix #3 for windows WSL when operate accross different file system
1 parent 74e0229 commit 5c5f006

File tree

4 files changed

+70
-48
lines changed

4 files changed

+70
-48
lines changed

cmd/init.go

Lines changed: 3 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,9 @@ package cmd
1818
import (
1919
"errors"
2020
"fmt"
21-
"io"
21+
"github.com/let-sh/cli/utils"
2222
"io/ioutil"
2323
"os"
24-
"path/filepath"
2524
"strings"
2625

2726
"github.com/let-sh/cli/log"
@@ -81,7 +80,8 @@ e.g.:
8180
return
8281
}
8382
// mv to current folder
84-
err := moveDirectory(fmt.Sprintf("%s/%s", tempDir, projectType), fmt.Sprintf("%s/%s", currentDir, folderName))
83+
err := utils.Move(fmt.Sprintf("%s/%s", tempDir, projectType), fmt.Sprintf("%s/%s", currentDir,
84+
folderName))
8585
if err != nil {
8686
log.Error(errors.New("cannot init project to current folder: " + err.Error()))
8787
//logrus.Debug("current project dir: ", pwd)
@@ -97,47 +97,6 @@ e.g.:
9797
},
9898
}
9999

100-
func moveDirectory(src, dst string) error {
101-
err := os.MkdirAll(dst, 0755)
102-
if err != nil {
103-
return err
104-
}
105-
files, err := ioutil.ReadDir(src)
106-
if err != nil {
107-
return err
108-
}
109-
for _, file := range files {
110-
srcfp := filepath.Join(src, file.Name())
111-
dstfp := filepath.Join(dst, file.Name())
112-
if file.IsDir() {
113-
moveDirectory(srcfp, dstfp)
114-
} else {
115-
moveFile(srcfp, dstfp)
116-
}
117-
}
118-
return nil
119-
}
120-
121-
func moveFile(src, dst string) error {
122-
in, err := os.Open(src)
123-
if err != nil {
124-
return err
125-
}
126-
defer in.Close()
127-
128-
out, err := os.Create(dst)
129-
if err != nil {
130-
return err
131-
}
132-
defer out.Close()
133-
134-
_, err = io.Copy(out, in)
135-
if err != nil {
136-
return err
137-
}
138-
return out.Close()
139-
}
140-
141100
func init() {
142101
rootCmd.AddCommand(initCmd)
143102

utils/files.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package utils
22

33
import (
4+
"io"
5+
"io/ioutil"
46
"os"
7+
"path/filepath"
58
)
69

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

2528
return fi.Size(), nil
2629
}
30+
31+
func Move(src, dst string) error {
32+
var err error
33+
// This returns an *os.FileInfo type
34+
fileInfo, err := os.Stat(src)
35+
if err != nil {
36+
return err
37+
}
38+
39+
// IsDir is short for fileInfo.Mode().IsDir()
40+
if fileInfo.IsDir() {
41+
err = MoveDirectory(src, dst)
42+
} else {
43+
err = MoveFile(src, dst)
44+
}
45+
return err
46+
}
47+
48+
func MoveDirectory(src, dst string) error {
49+
err := os.MkdirAll(dst, 0755)
50+
if err != nil {
51+
return err
52+
}
53+
files, err := ioutil.ReadDir(src)
54+
if err != nil {
55+
return err
56+
}
57+
for _, file := range files {
58+
srcfp := filepath.Join(src, file.Name())
59+
dstfp := filepath.Join(dst, file.Name())
60+
if file.IsDir() {
61+
MoveDirectory(srcfp, dstfp)
62+
} else {
63+
MoveFile(srcfp, dstfp)
64+
}
65+
}
66+
return nil
67+
}
68+
69+
func MoveFile(src, dst string) error {
70+
in, err := os.Open(src)
71+
if err != nil {
72+
return err
73+
}
74+
defer in.Close()
75+
76+
out, err := os.Create(dst)
77+
if err != nil {
78+
return err
79+
}
80+
defer out.Close()
81+
82+
_, err = io.Copy(out, in)
83+
if err != nil {
84+
return err
85+
}
86+
return out.Close()
87+
}

utils/update/upgrade.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/let-sh/cli/info"
99
"github.com/let-sh/cli/log"
1010
"github.com/let-sh/cli/requests"
11+
"github.com/let-sh/cli/utils"
1112
"github.com/sirupsen/logrus"
1213
"github.com/vbauerster/mpb/v7"
1314
"github.com/vbauerster/mpb/v7/decor"
@@ -101,11 +102,11 @@ func UpgradeCli(force bool, channel string) {
101102
if runtime.GOOS == "windows" {
102103
// handle process error
103104
// go further: https://stackoverflow.com/questions/9162969/how-can-a-c-binary-replace-itself
104-
err = os.Rename(path, path+".old")
105-
err = os.Rename(filepath.Join(unzipedDir, binaryName), path)
105+
utils.Move(path, path+".old")
106+
utils.Move(filepath.Join(unzipedDir, binaryName), path)
106107
os.RemoveAll(path + ".old")
107108
} else {
108-
err = os.Rename(filepath.Join(unzipedDir, binaryName), path)
109+
err = utils.Move(filepath.Join(unzipedDir, binaryName), path)
109110
}
110111

111112
if err != nil {

utils/update/upgrades_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package update
33
import (
44
"github.com/let-sh/cli/info"
55
"github.com/let-sh/cli/requests"
6+
"github.com/let-sh/cli/utils"
67
"github.com/sirupsen/logrus"
78
"os"
89
"os/exec"
@@ -57,7 +58,7 @@ func TestUpgradeCli(t *testing.T) {
5758
return
5859
}
5960

60-
err = os.Rename(filepath.Join(tempDir, "lets"), path)
61+
utils.MoveFile(filepath.Join(tempDir, "lets"), path)
6162
if err != nil {
6263
logrus.WithError(err).Debugln("get compressed file")
6364
return

0 commit comments

Comments
 (0)