diff --git a/cmd/get.go b/cmd/get.go index 70a91fdf5..6ae456e8c 100644 --- a/cmd/get.go +++ b/cmd/get.go @@ -120,7 +120,7 @@ func runGet(cmd *Command, args []string) { return } - hd, err := doc.GetHomeDir() + hd, err := com.HomeDir() if err != nil { com.ColorLog("[ERRO] Fail to get current user[ %s ]\n", err) return diff --git a/cmd/search.go b/cmd/search.go index 0b4487555..70dbeb7f1 100644 --- a/cmd/search.go +++ b/cmd/search.go @@ -69,8 +69,6 @@ func runSearch(cmd *Command, args []string) { host = "localhost" port = "8991" - autoRun() - if cmd.Flags["-e"] { search(host, port, args[0], true) } else { diff --git a/cmd/serve.go b/cmd/serve.go index ba15ca5f3..bdc76b95e 100644 --- a/cmd/serve.go +++ b/cmd/serve.go @@ -17,6 +17,7 @@ package cmd import ( serrors "errors" "fmt" + "github.com/Unknwon/com" "github.com/gpmgo/gopm/doc" "github.com/syndtr/goleveldb/leveldb" "github.com/syndtr/goleveldb/leveldb/errors" @@ -25,10 +26,9 @@ import ( "net/http" "net/url" "os" + "path" "strconv" "strings" - - "github.com/Unknwon/com" ) var ( @@ -85,10 +85,10 @@ func runServe(cmd *Command, args []string) { var listen string var port string if cmd.Flags["-l"] { - listen += "127.0.0.1:" + listen += "127.0.0.1" port = autoPort() } else { - listen += "0.0.0.0:" + listen += "0.0.0.0" port = "8991" } @@ -97,7 +97,7 @@ func runServe(cmd *Command, args []string) { port = args[0] } - startService(listen + port) + startService(listen, port) } func splitWord(word string, res *map[string]bool) { @@ -154,20 +154,6 @@ func getServePort() string { 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 func saveNode(nod *doc.Node) error { urlPath := fmt.Sprintf("http://%v:%v/add", getServeHost(), getServePort()) @@ -175,12 +161,12 @@ func saveNode(nod *doc.Node) error { url.Values{"importPath": {nod.ImportPath}, "synopsis": {nod.Synopsis}, "downloadURL": {nod.DownloadURL}, - "isGetDeps": {BoolStr(nod.IsGetDeps)}, + "isGetDeps": {strconv.FormatBool(nod.IsGetDeps)}, "type": {nod.Type}, "value": {nod.Value}}) if err != nil { - com.ColorLog("%v\n", err.Error()) + com.ColorLog("[ERRO] Fail to save node[ %s ]\n", err) return err } defer resp.Body.Close() @@ -207,8 +193,6 @@ func addNode(nod *doc.Node) error { return err } - fmt.Println("last id is ", strLastId) - lastId, err := strconv.ParseInt(strLastId, 0, 64) if err != nil { return err @@ -234,7 +218,7 @@ func addNode(nod *doc.Node) error { err = batchPut(batch, "down:"+id, nod.DownloadURL) } if err == nil { - err = batchPut(batch, "deps:"+id, BoolStr(nod.IsGetDeps)) + err = batchPut(batch, "deps:"+id, strconv.FormatBool(nod.IsGetDeps)) } // save totals @@ -295,7 +279,7 @@ func addNode(nod *doc.Node) error { return nil } - // indexing + // indexing package name keys := splitPkgName(nod.ImportPath) for key, _ := range keys { 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) } @@ -314,30 +303,37 @@ func rmPkg(nod *doc.Node) { var db *leveldb.DB // service should be run -func autoRun() error { +func AutoRun() error { s, _, _ := runningStatus() if s == STOP { - attr := &os.ProcAttr{ - Files: make([]*os.File, 0), - } - _, err := os.StartProcess("./gopm", []string{"serve", "-l"}, attr) + // current path + curPath, err := os.Getwd() if err != nil { 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 { return err } - f.WriteString(fmt.Sprintf("%v,%v,%v", RUNNING, , )) - - fmt.Println(p.Pid)*/ } return nil } 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 { return STOP, 0, 0 } @@ -374,19 +370,39 @@ func runningStatus() (int, int, int) { return status, pid, port } -func startService(listen string) { - homeDir, err := doc.GetHomeDir() +func getPidPath() (string, error) { + homeDir, err := com.HomeDir() if err != nil { - fmt.Println(err) - return + return "", err } + 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) db, err = leveldb.OpenFile(dbDir, &opt.Options{Flag: opt.OFCreateIfMissing}) if err != nil { - fmt.Println(err) - return + return err } defer db.Close() @@ -397,7 +413,8 @@ func startService(listen string) { // these handlers can be accessed according listen's ip http.HandleFunc("/search", searchHandler) http.HandleFunc("/searche", searcheHandler) - http.ListenAndServe(listen, nil) + http.ListenAndServe(listen+":"+port, nil) + return nil } 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.Synopsis = r.FormValue("synopsis") 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.Value = r.FormValue("value") - err := addNode(nod) + err = addNode(nod) if err != nil { - fmt.Println(err) + com.ColorLog("[ERRO] SEVER: Cannot add node[ %s ]\n", err) } //} } diff --git a/doc/google.go b/doc/google.go index 849abbe9d..f363a3b23 100644 --- a/doc/google.go +++ b/doc/google.go @@ -15,6 +15,7 @@ package doc import ( + "errors" "net/http" "os" "path" @@ -37,16 +38,15 @@ func getGoogleDoc(client *http.Client, match map[string]string, installRepoPath } var installPath string + projectPath := GetProjectPath(nod.ImportPath) if nod.ImportPath == nod.DownloadURL { suf := "." + nod.Value if len(suf) == 1 { suf = "" } - projectPath := expand("code.google.com/p/{repo}{dot}{subrepo}{dir}", match) installPath = installRepoPath + "/" + projectPath + suf - nod.ImportPath = projectPath } else { - installPath = installRepoPath + "/" + nod.ImportPath + installPath = installRepoPath + "/" + projectPath } // Remove old files. @@ -56,6 +56,7 @@ func getGoogleDoc(client *http.Client, match map[string]string, installRepoPath ext := ".zip" if match["vcs"] == "svn" { ext = ".tar.gz" + com.ColorLog("[WARN] SVN detected, may take very long time.\n") } 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)) } + if len(dirs) == 0 { + return nil, errors.New("No file in repository") + } + if err != nil { return nil, err } diff --git a/doc/utils.go b/doc/utils.go index e550e3d5c..ebcbf5d72 100644 --- a/doc/utils.go +++ b/doc/utils.go @@ -16,10 +16,8 @@ package doc import ( "os" - "os/user" "path" "regexp" - "runtime" "strings" "github.com/Unknwon/com" @@ -620,22 +618,6 @@ func IsGoRepoPath(importPath string) bool { 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 { if len(v) == 0 { return "" diff --git a/gopm.go b/gopm.go index c8255c175..1eff152f0 100644 --- a/gopm.go +++ b/gopm.go @@ -26,6 +26,7 @@ import ( "unicode" "unicode/utf8" + "github.com/Unknwon/com" "github.com/gpmgo/gopm/cmd" ) @@ -88,7 +89,16 @@ func main() { // Check commands and run. for _, comm := range commands { 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() return }