-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
81 lines (69 loc) · 1.73 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
package main
import (
"fmt"
"github.com/gernest/wow"
"github.com/gernest/wow/spin"
"github.com/manifoldco/promptui"
"io/ioutil"
"log"
"net/http"
"os"
"strings"
)
func getEnvironments() []string{
loadingSpinner := wow.New(os.Stdout, spin.Get(spin.Dots), " Getting environments")
loadingSpinner.Start()
resp, err := http.Get("https://www.gitignore.io/api/list")
if err != nil {
log.Fatalln(err)
}
//We Read the response body on the line below
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatalln(err)
}
//Convert the body to type string
sb := string(body)
items := strings.Split(sb,",")
loadingSpinner.Stop()
return items
}
func writeFile(language string){
addingSpinner := wow.New(os.Stdout, spin.Get(spin.Dots), "Adding gitignore")
giResponse, err := http.Get(fmt.Sprintf("https://www.gitignore.io/api/%s",language))
if err != nil {
log.Fatalln(err)
}
//We Read the response body on the line below.
giBody, err := ioutil.ReadAll(giResponse.Body)
if err != nil {
log.Fatalln(err)
}
//Convert the body to type string
addingSpinner.Stop()
err = ioutil.WriteFile(".gitignore", giBody, 0644)
if err != nil {
log.Fatal(err)
}
log.Printf("Successfully created gitignore for %s\n", language)
}
func main() {
items := getEnvironments()
prompt := promptui.Select{
Label: "Select environment",
Items: items,
StartInSearchMode : true,
Searcher : func(input string, index int) bool {
pepper := items[index]
name := strings.Replace(strings.ToLower(pepper), " ", "", -1)
input = strings.Replace(strings.ToLower(input), " ", "", -1)
return strings.Contains(name, input)
},
}
_, result, err := prompt.Run()
if err != nil {
fmt.Printf("Prompt failed %v\n", err)
return
}
writeFile(result)
}