-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
192 lines (160 loc) · 3.98 KB
/
main.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package main
import (
"fmt"
"github.com/manifoldco/promptui"
"github.com/pelletier/go-toml/v2"
"github.com/urfave/cli/v2"
"io"
"log"
"os"
"strings"
)
var registryPath = os.Getenv("BF_REGISTRY")
const registryFilename = ".bf-registry"
func main() {
if registryPath == "" {
fmt.Println("Please set the BF_REGISTRY environment variable")
os.Exit(1)
}
app := &cli.App{
Commands: []*cli.Command{
{
Name: "new",
Aliases: []string{"n"},
Usage: "create a new bf project",
Action: func(*cli.Context) error {
currentPath, _ := os.Getwd()
bf := Bf{
Path: currentPath,
}
prompt := promptui.Prompt{
Label: "Project name",
Validate: Validate(3),
}
result, _ := prompt.Run()
bf.Global.Name = result
prompt = promptui.Prompt{
Label: "Description",
Validate: Validate(8),
}
result, _ = prompt.Run()
bf.Global.Description = result
promptSelect := promptui.Select{
Label: "Programming language",
Items: []string{"Go", "Python", "JavaScript", "Ruby", "Rust", "Java", "Kotlin", "Swift", "C", "C++"},
Size: 10,
}
_, result, _ = promptSelect.Run()
bf.Global.Language = result
AppendToFile(fmt.Sprintf("%s/%s", registryPath, registryFilename), currentPath)
b, err := toml.Marshal(bf)
if err != nil {
log.Fatal(err)
}
AppendToFile(fmt.Sprintf("%s/%s", currentPath, ".bf"), string(b))
return nil
},
},
{
Name: "list",
Aliases: []string{"l"},
Usage: "list all bf projects",
Action: func(*cli.Context) error {
bytes, err := ReadFile(fmt.Sprintf("%s/%s", registryPath, registryFilename))
if err != nil {
log.Fatal(err)
}
paths := strings.Split(string(bytes), "\n")
projects := make([]Bf, 0, len(paths)-1)
for _, p := range paths {
if p == "" {
continue
}
content, err := ReadFile(fmt.Sprintf("%s/%s", p, ".bf"))
if err != nil {
fmt.Println("Errors reading")
fmt.Println(err.Error())
continue
}
bf := Bf{
Path: p,
}
err = toml.Unmarshal(content, &bf)
if err != nil {
fmt.Println("Errors")
fmt.Println(err.Error())
continue
}
projects = append(projects, bf)
}
if len(projects) < 1 {
fmt.Println("No projects found")
return nil
}
fmt.Println(fmt.Sprintf("Found %d projects", len(projects)))
names := make([]string, 0, len(projects))
for _, p := range projects {
names = append(names, p.Global.Name)
}
promptSelect := promptui.Select{
Label: "Projects",
Items: names,
Size: len(names),
}
_, result, _ := promptSelect.Run()
for _, p := range projects {
if p.Global.Name == result {
fmt.Println(p.Global.Description)
fmt.Println(p.Path)
}
}
return nil
},
},
},
}
if err := app.Run(os.Args); err != nil {
log.Fatal(err)
}
}
func ReadFile(filepath string) ([]byte, error) {
f, err := os.OpenFile(filepath, os.O_RDONLY, 0644)
if err != nil {
fmt.Println(err.Error())
return nil, err
}
defer f.Close()
bytes, err := io.ReadAll(f)
return bytes, err
}
func AppendToFile(filename, data string) {
f, err := os.OpenFile(filename, os.O_APPEND|os.O_CREATE|os.O_RDWR, 0644)
defer f.Close()
if err != nil {
fmt.Println(err.Error())
return
}
content, err := io.ReadAll(f)
if err != nil {
fmt.Println(err.Error())
return
}
// No need to append path if it already exists
if strings.Contains(string(content), data) {
return
}
if _, err := f.Write([]byte(fmt.Sprintf("%s\n", data))); err != nil {
fmt.Println(err.Error())
}
}
func Validate(minChars int) func(s string) error {
return func(s string) error {
if len(s) < minChars {
return fmt.Errorf("Name must be at least %d characters", minChars)
}
if s[0] < 65 || s[0] > 90 {
return fmt.Errorf("Name must start with a capital letter")
}
return nil
}
}