-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinteractive.go
141 lines (113 loc) · 2.56 KB
/
interactive.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
package main
import (
"fmt"
"github.com/manifoldco/promptui"
"strconv"
"encoding/json"
"io/ioutil"
"log"
"net/http"
"time"
)
// image sample
// curl https://bitclouds.sh/images
//{
// "images": [
// "ubuntu",
// "bitcoind",
// "centos",
// "clightning",
// "bsdjail",
// "lnd",
// "freebsd",
// "debian",
// "freebsd-ufs",
// "netbsd",
// "openbsd"
// ]
//}
type images struct {
Images []string
}
func createJailDialogue(pubkey string, name string) {
prompt := promptui.Select{
Label: "Select disk size",
Items: []string{"10g", "20g", "40g", "80g", "160g"},
}
_, disksize, err := prompt.Run()
if err != nil {
fmt.Printf("Prompt failed %v\n", err)
return
}
createJail(disksize, pubkey, name)
}
func createVmDialogue(pubkey string, name string) {
prompt := promptui.Select{
Label: "Select how many CPUs you need",
Items: []int{1, 2, 4, 8},
}
_, cpus, err := prompt.Run()
if err != nil {
fmt.Printf("Prompt failed %v\n", err)
return
}
prompt = promptui.Select{
Label: "Select how much RAM you need",
Items: []string{"512m", "2g", "4g", "8g"},
}
_, ram, err := prompt.Run()
if err != nil {
fmt.Printf("Prompt failed %v\n", err)
return
}
prompt = promptui.Select{
Label: "Select disk size",
Items: []string{"20g", "60g", "180g", "300g"},
}
_, disksize, err := prompt.Run()
if err != nil {
fmt.Printf("Prompt failed %v\n", err)
return
}
// Start dynamic image list by CLOUD_URL/images URL
// get image list from cloudurl/images
var imageurl string
imageurl = cloudUrl + "/images"
nubeClient := http.Client{
Timeout: time.Second * 2, // Timeout after 2 seconds
}
req, err := http.NewRequest(http.MethodGet, imageurl, nil)
if err != nil {
log.Fatal(err)
}
req.Header.Set("User-Agent", "nubectl")
res, getErr := nubeClient.Do(req)
if getErr != nil {
log.Fatal(getErr)
}
if res.Body != nil {
defer res.Body.Close()
}
body, readErr := ioutil.ReadAll(res.Body)
if readErr != nil {
log.Fatal(readErr)
}
images1 := images{}
jsonErr := json.Unmarshal([]byte(body), &images1)
if jsonErr != nil {
log.Fatal(jsonErr)
}
// End of Start dnynamic image list by CLOUD_URL/images URL
prompt = promptui.Select{
Label: "Select VM image",
// Items: []string{"centos7", "centos8", "ubuntu", "debian", "freebsd_ufs", "freebsd_zfs", "openbsd", "netbsd"},
Items: images1.Images,
}
_, image, err := prompt.Run()
if err != nil {
fmt.Printf("Prompt failed %v\n", err)
return
}
cpucount, _ := strconv.Atoi(cpus)
createVM(image, cpucount, ram, disksize, pubkey, name)
}