-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodtool.go
118 lines (89 loc) · 1.95 KB
/
modtool.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
117
118
package main
import (
"fmt"
"io/ioutil"
"os"
"log"
"strings"
"bytes"
"path/filepath"
)
var mainFolder string
func main(){
//Get all the files and directories
files, err := ioutil.ReadDir(os.Args[1])
if err != nil{
log.Fatal(err)
}
//Get Current folder name
pwd, _ := os.Getwd()
cwd := strings.Split(pwd, "/")
mainFolder = cwd[len(cwd)-1]
//Get Directories present in the current list of files obtained from line 18
dir, mod := contains(files)
//Check if the dir has go.mod if not declare one
check(mod)
list := dir
//Iterate over the list of dir
for ; ; {
if (len(list) == 0){
break
}
newList := CheckForSubAndAdd(list[0])
//Add any sub dir that was found to the list
list = append(list, newList...)
//Remove the first entry as it was iterated.
list = list[1:]
}
}
func CheckForSubAndAdd(name string)([] string ){
err := os.Chdir(name)
if err != nil{
fmt.Println(err)
}
pwd,_ := os.Getwd()
files ,_ :=ioutil.ReadDir(pwd)
dir, mod := contains(files)
check(mod)
return dir
}
func check(val bool){
if !val{
f, err := os.OpenFile("go.mod", os.O_RDWR|os.O_CREATE ,0755)
var buffer bytes.Buffer
if err != nil{
fmt.Println(err)
}
buffer.WriteString("module ")
buffer.WriteString(os.Args[2])
pwd, _ := os.Getwd()
//cwd := strings.Split(pwd, "/")
index := strings.Index(pwd,mainFolder)
buffer.WriteString(pwd[index:])
f.Write([]byte(buffer.String()))
}else{
}
}
func isGoFile(name string) bool{
return strings.Contains(name,".go")
}
func contains(arr []os.FileInfo) (name []string,mod bool){
mod = false
for _, file := range arr{
if (file.Name()=="go.mod"){
mod = true
}
if file.IsDir() && isNotHidden(file.Name()){
path , err := filepath.Abs(file.Name())
if err != nil {
fmt.Println("Error in getting absolute path")
}
name = append(name, path)
}
}
return name, mod
}
func isNotHidden(name string)bool{
return !strings.Contains(name,".")
}
func