Browse Source

fixed search bug

pull/103/head
Lunny Xiao 11 years ago
parent
commit
ad2a96f279
  1. 2
      cmd/get.go
  2. 2
      cmd/search.go
  3. 111
      cmd/serve.go
  4. 11
      doc/google.go
  5. 18
      doc/utils.go
  6. 12
      gopm.go

2
cmd/get.go

@ -120,7 +120,7 @@ func runGet(cmd *Command, args []string) {
return return
} }
hd, err := doc.GetHomeDir() hd, err := com.HomeDir()
if err != nil { if err != nil {
com.ColorLog("[ERRO] Fail to get current user[ %s ]\n", err) com.ColorLog("[ERRO] Fail to get current user[ %s ]\n", err)
return return

2
cmd/search.go

@ -69,8 +69,6 @@ func runSearch(cmd *Command, args []string) {
host = "localhost" host = "localhost"
port = "8991" port = "8991"
autoRun()
if cmd.Flags["-e"] { if cmd.Flags["-e"] {
search(host, port, args[0], true) search(host, port, args[0], true)
} else { } else {

111
cmd/serve.go

@ -17,6 +17,7 @@ package cmd
import ( import (
serrors "errors" serrors "errors"
"fmt" "fmt"
"github.com/Unknwon/com"
"github.com/gpmgo/gopm/doc" "github.com/gpmgo/gopm/doc"
"github.com/syndtr/goleveldb/leveldb" "github.com/syndtr/goleveldb/leveldb"
"github.com/syndtr/goleveldb/leveldb/errors" "github.com/syndtr/goleveldb/leveldb/errors"
@ -25,10 +26,9 @@ import (
"net/http" "net/http"
"net/url" "net/url"
"os" "os"
"path"
"strconv" "strconv"
"strings" "strings"
"github.com/Unknwon/com"
) )
var ( var (
@ -85,10 +85,10 @@ func runServe(cmd *Command, args []string) {
var listen string var listen string
var port string var port string
if cmd.Flags["-l"] { if cmd.Flags["-l"] {
listen += "127.0.0.1:" listen += "127.0.0.1"
port = autoPort() port = autoPort()
} else { } else {
listen += "0.0.0.0:" listen += "0.0.0.0"
port = "8991" port = "8991"
} }
@ -97,7 +97,7 @@ func runServe(cmd *Command, args []string) {
port = args[0] port = args[0]
} }
startService(listen + port) startService(listen, port)
} }
func splitWord(word string, res *map[string]bool) { func splitWord(word string, res *map[string]bool) {
@ -154,20 +154,6 @@ func getServePort() string {
return "8991" return "8991"
} }
func BoolStr(b bool) string {
if b {
return "true"
}
return "false"
}
func StrBool(bStr string) bool {
if bStr == "true" {
return true
}
return false
}
// for exernal of serve to add node to db // for exernal of serve to add node to db
func saveNode(nod *doc.Node) error { func saveNode(nod *doc.Node) error {
urlPath := fmt.Sprintf("http://%v:%v/add", getServeHost(), getServePort()) urlPath := fmt.Sprintf("http://%v:%v/add", getServeHost(), getServePort())
@ -175,12 +161,12 @@ func saveNode(nod *doc.Node) error {
url.Values{"importPath": {nod.ImportPath}, url.Values{"importPath": {nod.ImportPath},
"synopsis": {nod.Synopsis}, "synopsis": {nod.Synopsis},
"downloadURL": {nod.DownloadURL}, "downloadURL": {nod.DownloadURL},
"isGetDeps": {BoolStr(nod.IsGetDeps)}, "isGetDeps": {strconv.FormatBool(nod.IsGetDeps)},
"type": {nod.Type}, "type": {nod.Type},
"value": {nod.Value}}) "value": {nod.Value}})
if err != nil { if err != nil {
com.ColorLog("%v\n", err.Error()) com.ColorLog("[ERRO] Fail to save node[ %s ]\n", err)
return err return err
} }
defer resp.Body.Close() defer resp.Body.Close()
@ -207,8 +193,6 @@ func addNode(nod *doc.Node) error {
return err return err
} }
fmt.Println("last id is ", strLastId)
lastId, err := strconv.ParseInt(strLastId, 0, 64) lastId, err := strconv.ParseInt(strLastId, 0, 64)
if err != nil { if err != nil {
return err return err
@ -234,7 +218,7 @@ func addNode(nod *doc.Node) error {
err = batchPut(batch, "down:"+id, nod.DownloadURL) err = batchPut(batch, "down:"+id, nod.DownloadURL)
} }
if err == nil { if err == nil {
err = batchPut(batch, "deps:"+id, BoolStr(nod.IsGetDeps)) err = batchPut(batch, "deps:"+id, strconv.FormatBool(nod.IsGetDeps))
} }
// save totals // save totals
@ -295,7 +279,7 @@ func addNode(nod *doc.Node) error {
return nil return nil
} }
// indexing // indexing package name
keys := splitPkgName(nod.ImportPath) keys := splitPkgName(nod.ImportPath)
for key, _ := range keys { for key, _ := range keys {
err = batchPut(batch, fmt.Sprintf("key:%v:%v", key, id), "") err = batchPut(batch, fmt.Sprintf("key:%v:%v", key, id), "")
@ -304,6 +288,11 @@ func addNode(nod *doc.Node) error {
} }
} }
// TODO: indexing desc
if nod.Synopsis != "" {
//fields := strings.FieldsFunc(nod.Synopsis, f)
}
return db.Write(batch, wo) return db.Write(batch, wo)
} }
@ -314,30 +303,37 @@ func rmPkg(nod *doc.Node) {
var db *leveldb.DB var db *leveldb.DB
// service should be run // service should be run
func autoRun() error { func AutoRun() error {
s, _, _ := runningStatus() s, _, _ := runningStatus()
if s == STOP { if s == STOP {
attr := &os.ProcAttr{ // current path
Files: make([]*os.File, 0), curPath, err := os.Getwd()
}
_, err := os.StartProcess("./gopm", []string{"serve", "-l"}, attr)
if err != nil { if err != nil {
return err return err
} }
/*f, err := os.OpenFile("~/.gopm/var/pid", os.O_CREATE, 0700) attr := &os.ProcAttr{
Dir: curPath,
Env: os.Environ(),
Files: []*os.File{nil, nil, nil},
}
p := path.Join(curPath, "gopm")
_, err = os.StartProcess(p, []string{"gopm", "serve", "-l"}, attr)
if err != nil { if err != nil {
return err return err
} }
f.WriteString(fmt.Sprintf("%v,%v,%v", RUNNING, , ))
fmt.Println(p.Pid)*/
} }
return nil return nil
} }
func runningStatus() (int, int, int) { func runningStatus() (int, int, int) {
contentByte, err := ioutil.ReadFile("~/.gopm/var/pid") pFile, err := getPidPath()
if err != nil {
return STOP, 0, 0
}
contentByte, err := ioutil.ReadFile(pFile)
if err != nil { if err != nil {
return STOP, 0, 0 return STOP, 0, 0
} }
@ -374,19 +370,39 @@ func runningStatus() (int, int, int) {
return status, pid, port return status, pid, port
} }
func startService(listen string) { func getPidPath() (string, error) {
homeDir, err := doc.GetHomeDir() homeDir, err := com.HomeDir()
if err != nil { if err != nil {
fmt.Println(err) return "", err
return
} }
pFile := strings.Replace("~/.gopm/var/pid", "~", homeDir, -1)
return pFile, nil
}
func startService(listen, port string) error {
homeDir, err := com.HomeDir()
if err != nil {
return err
}
pFile, err := getPidPath()
if err != nil {
return err
}
f, err := os.OpenFile(pFile, os.O_CREATE, 0700)
if err != nil {
return err
}
defer f.Close()
f.WriteString(fmt.Sprintf("%v,%v,%v", RUNNING, os.Getpid(), port))
dbDir = strings.Replace(dbDir, "~", homeDir, -1) dbDir = strings.Replace(dbDir, "~", homeDir, -1)
db, err = leveldb.OpenFile(dbDir, &opt.Options{Flag: opt.OFCreateIfMissing}) db, err = leveldb.OpenFile(dbDir, &opt.Options{Flag: opt.OFCreateIfMissing})
if err != nil { if err != nil {
fmt.Println(err) return err
return
} }
defer db.Close() defer db.Close()
@ -397,7 +413,8 @@ func startService(listen string) {
// these handlers can be accessed according listen's ip // these handlers can be accessed according listen's ip
http.HandleFunc("/search", searchHandler) http.HandleFunc("/search", searchHandler)
http.HandleFunc("/searche", searcheHandler) http.HandleFunc("/searche", searcheHandler)
http.ListenAndServe(listen, nil) http.ListenAndServe(listen+":"+port, nil)
return nil
} }
func searchHandler(w http.ResponseWriter, r *http.Request) { func searchHandler(w http.ResponseWriter, r *http.Request) {
@ -467,13 +484,17 @@ func addHandler(w http.ResponseWriter, r *http.Request) {
nod.ImportPath = r.FormValue("importPath") nod.ImportPath = r.FormValue("importPath")
nod.Synopsis = r.FormValue("synopsis") nod.Synopsis = r.FormValue("synopsis")
nod.DownloadURL = r.FormValue("downloadURL") nod.DownloadURL = r.FormValue("downloadURL")
nod.IsGetDeps = StrBool(r.FormValue("isGetDeps")) isGetDeps, err := strconv.ParseBool(r.FormValue("isGetDeps"))
if err != nil {
com.ColorLog("[ERRO] SEVER: Cannot get deps")
}
nod.IsGetDeps = isGetDeps
nod.Type = r.FormValue("type") nod.Type = r.FormValue("type")
nod.Value = r.FormValue("value") nod.Value = r.FormValue("value")
err := addNode(nod) err = addNode(nod)
if err != nil { if err != nil {
fmt.Println(err) com.ColorLog("[ERRO] SEVER: Cannot add node[ %s ]\n", err)
} }
//} //}
} }

11
doc/google.go

@ -15,6 +15,7 @@
package doc package doc
import ( import (
"errors"
"net/http" "net/http"
"os" "os"
"path" "path"
@ -37,16 +38,15 @@ func getGoogleDoc(client *http.Client, match map[string]string, installRepoPath
} }
var installPath string var installPath string
projectPath := GetProjectPath(nod.ImportPath)
if nod.ImportPath == nod.DownloadURL { if nod.ImportPath == nod.DownloadURL {
suf := "." + nod.Value suf := "." + nod.Value
if len(suf) == 1 { if len(suf) == 1 {
suf = "" suf = ""
} }
projectPath := expand("code.google.com/p/{repo}{dot}{subrepo}{dir}", match)
installPath = installRepoPath + "/" + projectPath + suf installPath = installRepoPath + "/" + projectPath + suf
nod.ImportPath = projectPath
} else { } else {
installPath = installRepoPath + "/" + nod.ImportPath installPath = installRepoPath + "/" + projectPath
} }
// Remove old files. // Remove old files.
@ -56,6 +56,7 @@ func getGoogleDoc(client *http.Client, match map[string]string, installRepoPath
ext := ".zip" ext := ".zip"
if match["vcs"] == "svn" { if match["vcs"] == "svn" {
ext = ".tar.gz" ext = ".tar.gz"
com.ColorLog("[WARN] SVN detected, may take very long time.\n")
} }
err := packer.PackToFile(match["importPath"], installPath+ext, match) err := packer.PackToFile(match["importPath"], installPath+ext, match)
@ -70,6 +71,10 @@ func getGoogleDoc(client *http.Client, match map[string]string, installRepoPath
dirs, err = com.UnTarGz(installPath+ext, path.Dir(installPath)) dirs, err = com.UnTarGz(installPath+ext, path.Dir(installPath))
} }
if len(dirs) == 0 {
return nil, errors.New("No file in repository")
}
if err != nil { if err != nil {
return nil, err return nil, err
} }

18
doc/utils.go

@ -16,10 +16,8 @@ package doc
import ( import (
"os" "os"
"os/user"
"path" "path"
"regexp" "regexp"
"runtime"
"strings" "strings"
"github.com/Unknwon/com" "github.com/Unknwon/com"
@ -620,22 +618,6 @@ func IsGoRepoPath(importPath string) bool {
return standardPath[importPath] return standardPath[importPath]
} }
func GetHomeDir() (string, error) {
if runtime.GOOS != "windows" {
curUser, err := user.Current()
if err != nil {
return "", err
}
return curUser.HomeDir, nil
} else {
hd, err := com.HomeDir()
if err != nil {
return "", err
}
return hd, nil
}
}
func CheckNodeValue(v string) string { func CheckNodeValue(v string) string {
if len(v) == 0 { if len(v) == 0 {
return "<UTD>" return "<UTD>"

12
gopm.go

@ -26,6 +26,7 @@ import (
"unicode" "unicode"
"unicode/utf8" "unicode/utf8"
"github.com/Unknwon/com"
"github.com/gpmgo/gopm/cmd" "github.com/gpmgo/gopm/cmd"
) )
@ -88,7 +89,16 @@ func main() {
// Check commands and run. // Check commands and run.
for _, comm := range commands { for _, comm := range commands {
if comm.Name() == args[0] && comm.Run != nil { if comm.Name() == args[0] && comm.Run != nil {
comm.Run(comm, args[1:]) if comm.Name() != "serve" {
err := cmd.AutoRun()
if err == nil {
comm.Run(comm, args[1:])
} else {
com.ColorLog("[ERRO] %v", err)
}
} else {
comm.Run(comm, args[1:])
}
exit() exit()
return return
} }

Loading…
Cancel
Save