Skip to content

Commit

Permalink
first init
Browse files Browse the repository at this point in the history
Change-Id: I09e398d59628256e2b11c4ba064476e1be1d22f6
  • Loading branch information
zhuzhenfeng.code committed Jun 7, 2021
0 parents commit 46f003e
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 0 deletions.
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module github.com/zzfup/garn

go 1.16

require github.com/PuerkitoBio/goquery v1.6.1
10 changes: 10 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
github.com/PuerkitoBio/goquery v1.6.1 h1:FgjbQZKl5HTmcn4sKBgvx8vv63nhyhIpv7lJpFGCWpk=
github.com/PuerkitoBio/goquery v1.6.1/go.mod h1:GsLWisAFVj4WgDibEWF4pvYnkVQBpKBKeU+7zCJoLcc=
github.com/andybalholm/cascadia v1.1.0 h1:BuuO6sSfQNFRu1LppgbD25Hr2vLYW25JvxHs5zzsLTo=
github.com/andybalholm/cascadia v1.1.0/go.mod h1:GsXiBklL0woXo1j/WYWtSYYC4ouU9PqHO0sqidkEA4Y=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/net v0.0.0-20180218175443-cbe0f9307d01/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20200202094626-16171245cfb2 h1:CCH4IOTTfewWjGOlSp+zGcjutRKlBEZQ6wTn8ozI/nI=
golang.org/x/net v0.0.0-20200202094626-16171245cfb2/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
86 changes: 86 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package main

import (
"fmt"
"io"
"net/http"
"os"
"os/exec"
"strings"
"time"

"github.com/PuerkitoBio/goquery"
)

func crawl(q string) {
timeout := time.Duration(5 * time.Second) //超时时间5s
client := &http.Client{
Timeout: timeout,
}
url := "https://pkg.go.dev/search?q=" + q
var Body io.Reader
request, err := http.NewRequest("GET", url, Body)
if err != nil {
fmt.Println("抓取" + q + "失败")
return
}
request.Header.Add("User-Agent", `Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.77 Safari/537.36`)

res, err := client.Do(request)
if err != nil {
fmt.Println("抓取"+q+"失败", err)
return
}
defer res.Body.Close()

document, err := goquery.NewDocumentFromReader(res.Body)
if err != nil {
fmt.Println("抓取" + q + "失败")
return
}

allData := []map[string]string{}
document.Find(".SearchResults .LegacySearchSnippet").Each(func(i int, selection *goquery.Selection) {
pkgName := selection.Find("h2").Text()
desc := selection.Find("p").Text()
allData = append(allData, map[string]string{
"pkg": strings.TrimSpace(pkgName),
"desc": desc,
})
})

first := allData[0]
for _, v := range allData {
if strings.Contains(v["pkg"], q) {
first = v
break
}
}

fmt.Println("找到包:", first["pkg"])
fmt.Println(first["desc"])

path, _ := os.Getwd()
fmt.Println("工作目录:", path)

cmd := exec.Command("go", "get", "-u", "-v", first["pkg"])
fmt.Println("开始执行:", cmd)
out, err := cmd.CombinedOutput()
if err != nil {
fmt.Println(string(out), err)
return
}
fmt.Println(string(out))
}

func main() {
args := os.Args

if args[1] != "add" {
fmt.Println("gopm add xxx")
}
q := args[2]

fmt.Println("开始抓取:", q)
crawl(q)
}
10 changes: 10 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# garn

仿照yarn的命令格式。

```
garn add xxx
```

# 优点
用户不用去记录整个包,只需要记得是哪个包的名字,即可添加依赖。

0 comments on commit 46f003e

Please sign in to comment.