/
utils.go
116 lines (97 loc) · 2.24 KB
/
utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package gosvelt
import (
"fmt"
"io"
"os"
"strings"
cp "github.com/otiai10/copy"
)
// copy an file to another file
//
// CopyFile take an input file and an output file
func copyFile(inFile, outFile string) error {
file, err := os.Open(inFile)
if err != nil {
return err
}
defer file.Close()
newFile, err := os.Create(outFile)
if err != nil {
return err
}
defer newFile.Close()
_, err = io.Copy(newFile, file)
if err != nil {
return fmt.Errorf("utils: cannot copy %s to %s (%s)", inFile, outFile, err)
}
return nil
}
// copy directory to another directory
//
// CopyDir take an input dir and an output dir
func copyDir(srcDir, destDir string) error {
err := cp.Copy(srcDir, destDir, cp.Options{
Skip: func(srcinfo os.FileInfo, src, dest string) (bool, error) {
// todo: add some suffix
return strings.HasSuffix(src, ".git"), nil
},
})
if err != nil {
return fmt.Errorf("utils: cannot copy dir %s to %s (%s)", srcDir, destDir, err)
}
return nil
}
// this will clean an directory
//
// for real, this will remove the dir
// and recreate an new
func cleanDir(dir string) error {
err := os.RemoveAll(dir)
if err != nil {
return err
}
return os.MkdirAll(dir, 0755)
}
// get the file of an path
// like:
// /path/to/filename.svelte -> filename.svelte
func file(file string) string {
fileS := strings.Split(file, "/")
return fileS[len(fileS)-1]
}
// get the filename of an path
// like:
// /path/to/filename.svelte -> filename
func fileName(file string) string {
fileS := strings.Split(file, "/")
fileF := fileS[len(fileS)-1]
return strings.Split(fileF, ".")[0]
}
// This method takes a file path as input
// and returns a boolean value indicating whether
// the path represents a file or not, as well
// as an error if any.
func isFile(path string) (bool, error) {
fi, err := os.Stat(path)
if err != nil {
return false, err
}
return !fi.IsDir(), nil
}
// this will check if path exist
func exist(path string) bool {
_, err := os.Stat(path)
return !os.IsNotExist(err)
}
// write text that will be cleaned when chan is closed
func temporaryText(textChan chan struct{}, msg string) {
fmt.Print(msg)
select {
case <-textChan:
fmt.Print("\r")
for i := 0; i < len(msg); i++ {
fmt.Print(" ")
}
fmt.Print("\r")
}
}