-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
201 lines (162 loc) · 4.03 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
193
194
195
196
197
198
199
200
201
package main
import (
"bufio"
"fmt"
"log"
"os"
"os/exec"
"regexp"
"strings"
)
type Account struct {
account string
authToken string
domain string
}
func main() {
printLogo()
accounts := extractAccounts()
chosenAccount := getChoice(accounts)
port := getPort()
setupAuth(chosenAccount)
updateEnv(chosenAccount)
runNgrok(chosenAccount, port)
}
func printLogo() {
gorock := `%s
__
____ ___________ ____ ____ | | __
/ ___\ / _ \_ __ \/ _ \_/ ___\| |/ /
/ /_/ > <_> ) | \( <_> ) \___| <
\___ / \____/|__| \____/ \___ >__|_ \
/_____/ \/ \/
%s`
blue := "\033[34m"
fmt.Println(fmt.Sprintf(gorock, blue, "\033[0m"))
fmt.Print("Welcome to GoRock! ngrok account management made easier 🗿\n\n")
}
func extractAccounts() []Account {
// Open accounts file
file, err := os.Open("accounts.txt")
accounts := make([]Account, 0)
if err != nil {
log.Fatalf("Failed to open file: %v", err)
panic(err)
}
defer file.Close()
scanner := bufio.NewScanner(file)
scanner.Scan()
// Read accounts from file
for scanner.Scan() {
line := scanner.Text()
fields := strings.Split(line, ",")
if len(fields) != 3 {
log.Fatalf("Invalid line format: " + line + "\nUse format: account, authToken, domain")
continue
}
account := Account{
account: strings.TrimSpace(fields[0]),
authToken: strings.TrimSpace(fields[1]),
domain: strings.TrimSpace(fields[2]),
}
accounts = append(accounts, account)
}
return accounts
}
func getChoice(accounts []Account) Account {
fmt.Println("Choose an account:")
fmt.Println("0. Exit")
for i, account := range accounts {
fmt.Printf("%d. %s: %s\n", i+1, account.account, account.domain)
}
var choice int
// Get user choice
for {
fmt.Print("\nEnter your choice: ")
_, err := fmt.Scanln(&choice)
if err != nil {
fmt.Println("Please enter a number.")
var discard string
fmt.Scanln(&discard)
continue
}
if choice == 0 {
os.Exit(0)
}
if choice < 0 || choice > len(accounts) {
fmt.Println("Invalid index")
continue
} else {
break
}
}
return accounts[choice-1]
}
func getPort() string {
fmt.Print("Enter port number (default 3000): ")
var portNumber string
fmt.Scanln(&portNumber)
if portNumber == "" {
portNumber = "3000"
}
return portNumber
}
func setupAuth(a Account) {
// Set up auth token
fmt.Println("Setting up auth token...")
addTokenCommand := "ngrok"
addTokenArgs := []string{"config", "add-authtoken", a.authToken}
cmd := exec.Command(addTokenCommand, addTokenArgs...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
log.Fatalf("Failed to set up auth token: %v", err)
}
}
func updateEnv(a Account) {
// Update .env file
envFile, err := os.Open(".env")
if err != nil {
log.Fatalf("Failed to open .env file: %v", err)
panic(err)
}
defer envFile.Close()
tempFile, err := os.Create("temp.env")
if err != nil {
log.Fatalf("Failed to create temp file: %v", err)
panic(err)
}
defer tempFile.Close()
scanner := bufio.NewScanner(envFile)
// Replace domains in .env
for scanner.Scan() {
line := scanner.Text()
r, _ := regexp.Compile(`[a-z0-9-]+\.ngrok-free.app`)
if r.MatchString(line) {
// Replace domain while keeping the rest of the line
start, end := r.FindStringIndex(line)[0], r.FindStringIndex(line)[1]
line = line[:start] + a.domain + line[end:]
}
_, err := tempFile.WriteString(line + "\n")
if err != nil {
log.Fatalf("Failed to write to stdout: %v", err)
}
}
envFile.Close()
tempFile.Close()
if err := os.Rename("temp.env", ".env"); err != nil {
log.Fatalf("Failed to rename temp file: %v", err)
}
}
func runNgrok(a Account, p string) {
// Run ngrok
runDomainCommand := "ngrok"
runDomainArgs := []string{"http", "--domain=" + a.domain, p}
fmt.Println("Running ngrok...")
cmd := exec.Command(runDomainCommand, runDomainArgs...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
log.Fatalf("Failed to run ngrok: %v", err)
}
}