forked from oxequa/realize
/
utils.go
86 lines (76 loc) · 1.65 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
package realize
import (
"errors"
"log"
"os"
"path"
"strings"
"github.com/urfave/cli/v2"
)
// Params parse one by one the given argumentes
func params(params *cli.Context) []string {
argsN := params.NArg()
if argsN > 0 {
var args []string
for i := 0; i <= argsN-1; i++ {
args = append(args, params.Args().Get(i))
}
return args
}
return nil
}
// Split each arguments in multiple fields
func split(args, fields []string) []string {
for _, arg := range fields {
arr := strings.Fields(arg)
args = append(args, arr...)
}
return args
}
// Duplicates check projects with same name or same combinations of main/path
func duplicates(value Project, arr []Project) (Project, error) {
for _, val := range arr {
if value.Name == val.Name {
return val, errors.New("There is already a project with name '" + val.Name + "'. Check your config file!")
}
}
return Project{}, nil
}
// Get file extensions
func ext(path string) string {
var ext string
for i := len(path) - 1; i >= 0 && !os.IsPathSeparator(path[i]); i-- {
if path[i] == '.' {
ext = path[i:]
if index := strings.LastIndex(ext, "."); index > 0 {
ext = ext[index:]
}
}
}
if ext != "" {
return ext[1:]
}
return ""
}
// Replace if isn't empty and create a new array
func replace(a []string, b string) []string {
if len(b) > 0 {
return strings.Fields(b)
}
return a
}
// Wdir return current working directory
func Wdir() string {
dir, err := os.Getwd()
if err != nil {
log.Fatal(err.Error())
}
return dir
}
func hasGoMod(dir string) bool {
filename := path.Join(dir, "go.mod")
if _, err := os.Stat(filename); os.IsNotExist(err) {
return false
}
return true
}