-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
196 lines (165 loc) · 4.46 KB
/
main.go
File metadata and controls
196 lines (165 loc) · 4.46 KB
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
// package main
// import (
// "fmt"
// "io"
// "net/http"
// "os"
// "strings"
// "golang.org/x/net/html"
// )
// func main() {
// if len(os.Args) < 2 {
// fmt.Println("Usage: gpm <package-name>")
// os.Exit(1)
// }
// packageName := os.Args[1]
// fmt.Printf("Package Name: %s\n", packageName)
// searchURL := "https://www.gpm.com/search?q=" + packageName
// fmt.Printf("Searching at URL: %s\n", searchURL)
// // Step 1: Search the website for the package
// resp, err := http.Get(searchURL)
// if err != nil {
// fmt.Printf("Error searching for package: %v\n", err)
// os.Exit(1)
// }
// defer resp.Body.Close()
// func() {
// }()
// fmt.Printf("Received HTTP response, status code: %d\n", resp.StatusCode)
// // Step 2: Parse the HTML to find the download link
// doc := html.NewTokenizer(resp.Body)
// var downloadURL string
// for {
// tokenType := doc.Next()
// switch tokenType {
// case html.ErrorToken:
// if downloadURL == "" {
// fmt.Println("Download link not found")
// os.Exit(1)
// }
// goto Download
// case html.StartTagToken, html.SelfClosingTagToken:
// token := doc.Token()
// if token.Data == "a" {
// for _, attr := range token.Attr {
// if attr.Key == "href" && strings.Contains(attr.Val, packageName) {
// downloadURL = attr.Val
// fmt.Printf("Found download URL: %s\n", downloadURL)
// break
// }
// }
// }
// }
// }
// Download:
// if downloadURL == "" {
// fmt.Println("No download URL found")
// os.Exit(1)
// }
// // Step 3: Download the package using the retrieved URL
// fmt.Printf("Downloading from %s...\n", downloadURL)
// resp, err = http.Get(downloadURL)
// if err != nil {
// fmt.Printf("Error downloading package: %v\n", err)
// os.Exit(1)
// }
// defer resp.Body.Close()
// out, err := os.Create(packageName + ".tar.gz")
// if err != nil {
// fmt.Printf("Error creating file: %v\n", err)
// os.Exit(1)
// }
// defer out.Close()
// _, err = io.Copy(out, resp.Body)
// if err != nil {
// fmt.Printf("Error saving file: %v\n", err)
// os.Exit(1)
// }
// fmt.Println("Package downloaded successfully!")
// }
package main
import (
"bytes"
"fmt"
"os"
"os/exec"
"time"
)
var packageMap = map[string]string{
// Web Frameworks
"gin": "github.com/gin-gonic/gin",
"echo": "github.com/labstack/echo",
"fiber": "github.com/gofiber/fiber",
"chi": "github.com/go-chi/chi",
"mux": "github.com/gorilla/mux",
// Database
"gorm": "gorm.io/gorm",
"sqlx": "github.com/jmoiron/sqlx",
"ent": "github.com/ent/ent",
"pgx": "github.com/jackc/pgx/v4",
// Authentication
"jwt-go": "github.com/dgrijalva/jwt-go",
"oauth2": "golang.org/x/oauth2",
"casbin": "github.com/casbin/casbin",
// HTTP Clients
"axios": "github.com/imroc/req",
"resty": "github.com/go-resty/resty/v2",
// Utilities
"viper": "github.com/spf13/viper",
"zap": "go.uber.org/zap",
"logrus": "github.com/sirupsen/logrus",
"cobra": "github.com/spf13/cobra",
"uuid": "github.com/google/uuid",
// Testing
"testify": "github.com/stretchr/testify",
"gock": "github.com/h2non/gock",
"mockery": "github.com/vektra/mockery/v2",
}
func main() {
if len(os.Args) < 2 {
fmt.Println("Usage: gpm <package-name>")
os.Exit(1)
}
packageName := os.Args[1]
fmt.Printf("Package Name: %s\n", packageName)
// Step 1: Lookup the URL for the package
urlPath, found := packageMap[packageName]
if !found {
fmt.Printf("Package %s not found in mapping\n", packageName)
os.Exit(1)
}
// Channel to signal when the go get command is done
done := make(chan bool)
// Goroutine for showing the loading animation
go func() {
for {
select {
case <-done:
fmt.Printf("\r%s\r", " ") // Clear the line after loading
return
default:
for _, r := range `-\|/` {
fmt.Printf("\r%c Fetching package...", r)
time.Sleep(100 * time.Millisecond)
}
}
}
}()
// Run `go get` command
goGetCmd := exec.Command("go", "get", urlPath)
var out bytes.Buffer
var stderr bytes.Buffer
goGetCmd.Stdout = &out
goGetCmd.Stderr = &stderr
err := goGetCmd.Run()
// Signal the loading animation to stop
done <- true
if err != nil {
fmt.Printf("\nError running go get command: %v\n", err)
fmt.Printf("Stdout: %s\n", out.String())
fmt.Printf("Stderr: %s\n", stderr.String())
os.Exit(1)
}
fmt.Println("Package successfully fetched using go get.")
fmt.Println("You can now import the package in your Go project using the package name.")
}