From 260b0a1c7c30ec56fe9db95118333223c824c942 Mon Sep 17 00:00:00 2001 From: Unknown Date: Thu, 23 May 2013 13:32:07 -0400 Subject: [PATCH] add command search --- README.md | 2 +- doc/bitbucket.go | 2 +- doc/github.go | 2 +- doc/google.go | 6 ++-- doc/http.go | 5 ++-- doc/launchpad.go | 2 +- gpm.go | 3 +- i18n/en-US/prompt.txt | 5 ++-- i18n/en-US/usage_search.txt | 8 ++++++ i18n/zh-CN/prompt.txt | 5 ++-- i18n/zh-CN/usage_search.txt | 8 ++++++ install.go | 2 +- search.go | 56 +++++++++++++++++++++++++++++++++++++ 13 files changed, 90 insertions(+), 16 deletions(-) create mode 100644 i18n/en-US/usage_search.txt create mode 100644 i18n/zh-CN/usage_search.txt create mode 100644 search.go diff --git a/README.md b/README.md index 8cd867cd3..82269e7d0 100644 --- a/README.md +++ b/README.md @@ -31,7 +31,6 @@ This application still in experiment, any change could happen, but it doesn't af ### v0.2.* -- Command `search` is for searching packages. - Add gpm working principle design. - Command `install` and `remove` Add bundle and snapshot parser code for downloading or removing by bundle or snapshot. - Command `check` is for checking and downloading all missing dependencies. @@ -44,6 +43,7 @@ This application still in experiment, any change could happen, but it doesn't af ### Future +- Command `search` add flags `-n` and `-o` for number of list items and offset. - Command `sync` is for sync packages in two computers, support client and server mode through ssh. - Command `init` is for auto-configuring Go develop environment. - Command `install` and `remove` and `update` backup data(up to 100 records) before executing. diff --git a/doc/bitbucket.go b/doc/bitbucket.go index 322eb0577..24229ec0c 100644 --- a/doc/bitbucket.go +++ b/doc/bitbucket.go @@ -81,7 +81,7 @@ func GetBitbucketDoc(client *http.Client, match map[string]string, installGOPATH // tarball : https://bitbucket.org/{owner}/{repo}/get/{commit}.tar.gz // Downlaod archive. - p, err := httpGetBytes(client, expand("https://bitbucket.org/{owner}/{repo}/get/{commit}.tar.gz", match), nil) + p, err := HttpGetBytes(client, expand("https://bitbucket.org/{owner}/{repo}/get/{commit}.tar.gz", match), nil) if err != nil { return nil, err } diff --git a/doc/github.go b/doc/github.go index 033d6e407..d89ff2239 100644 --- a/doc/github.go +++ b/doc/github.go @@ -92,7 +92,7 @@ func GetGithubDoc(client *http.Client, match map[string]string, installGOPATH st // tarball : https://github.com/{owner}/{repo}/tarball/{sha} // Downlaod archive. - p, err := httpGetBytes(client, expand("https://github.com/{owner}/{repo}/archive/{sha}.zip", match), nil) + p, err := HttpGetBytes(client, expand("https://github.com/{owner}/{repo}/archive/{sha}.zip", match), nil) if err != nil { return nil, err } diff --git a/doc/google.go b/doc/google.go index 1aeedf930..fddb11219 100644 --- a/doc/google.go +++ b/doc/google.go @@ -34,7 +34,7 @@ func setupGoogleMatch(match map[string]string) { func getGoogleVCS(client *http.Client, match map[string]string) error { // Scrape the HTML project page to find the VCS. - p, err := httpGetBytes(client, expand("http://code.google.com/p/{repo}/source/checkout", match), nil) + p, err := HttpGetBytes(client, expand("http://code.google.com/p/{repo}/source/checkout", match), nil) if err != nil { return err } @@ -67,7 +67,7 @@ func GetGoogleDoc(client *http.Client, match map[string]string, installGOPATH st rootPath := expand("http://{subrepo}{dot}{repo}.googlecode.com/{vcs}{dir}/", match) // Scrape the repo browser to find the project revision and individual Go files. - p, err := httpGetBytes(client, rootPath+"?r="+node.Value, nil) + p, err := HttpGetBytes(client, rootPath+"?r="+node.Value, nil) if err != nil { return nil, err } @@ -171,7 +171,7 @@ func GetGoogleDoc(client *http.Client, match map[string]string, installGOPATH st func downloadFiles(client *http.Client, match map[string]string, rootPath, installPath, commit string, dirs []string) error { for _, d := range dirs { - p, err := httpGetBytes(client, rootPath+d+"?r="+commit, nil) + p, err := HttpGetBytes(client, rootPath+d+"?r="+commit, nil) if err != nil { return err } diff --git a/doc/http.go b/doc/http.go index 58e574735..f0506ac12 100644 --- a/doc/http.go +++ b/doc/http.go @@ -50,9 +50,8 @@ var ( HttpClient = &http.Client{Transport: httpTransport} ) -// httpGet gets the specified resource. ErrNotFound is returned if the server -// responds with status 404. -func httpGetBytes(client *http.Client, url string, header http.Header) ([]byte, error) { +// HttpGetBytes returns page data in []byte. +func HttpGetBytes(client *http.Client, url string, header http.Header) ([]byte, error) { rc, err := httpGet(client, url, header) if err != nil { return nil, err diff --git a/doc/launchpad.go b/doc/launchpad.go index 8dc8d7eda..4e2c9abe3 100644 --- a/doc/launchpad.go +++ b/doc/launchpad.go @@ -50,7 +50,7 @@ func GetLaunchpadDoc(client *http.Client, match map[string]string, installGOPATH } // Scrape the repo browser to find the project revision and individual Go files. - p, err := httpGetBytes(client, downloadPath, nil) + p, err := HttpGetBytes(client, downloadPath, nil) if err != nil { return nil, err } diff --git a/gpm.go b/gpm.go index 2331ddd0f..0eb017d69 100644 --- a/gpm.go +++ b/gpm.go @@ -46,7 +46,7 @@ type tomlConfig struct { } type flagEnable struct { - Build, Install []string + Build, Install, Search []string } type account struct { @@ -101,6 +101,7 @@ func (c *Command) Runnable() bool { // The order here is the order in which they are printed by 'gpm help'. var commands = []*Command{ cmdBuild, + cmdSearch, cmdInstall, cmdRemove, } diff --git a/i18n/en-US/prompt.txt b/i18n/en-US/prompt.txt index 9583c253a..76d95eeae 100644 --- a/i18n/en-US/prompt.txt +++ b/i18n/en-US/prompt.txt @@ -12,7 +12,7 @@ NotFoundError=Import path prefix matches known service, but regexp does not. ErrNoMatch=Unsupported project hosting. PackageNotFound=Cannot find package: %s. -MovedFile=Moved file from $GOPATH(%s) to current directory(%s). +MovedFile=SUCCESS: Moved file from $GOPATH(%s) to current directory(%s). PureDownload=You enabled pure download. DownloadOnly=You enabled download without installing. DownloadExDeps=You enabled download dependencies in example. @@ -27,4 +27,5 @@ SkipInvalidPath=Skipped invalid import path: %s. InstallByGoGet=Installing package(%s) through 'go get'. NoVCSTool=No version control tool is available, pure download enabled! DownloadStatus=Downloading package: %s. -RemovePackage=Removing package: %s. \ No newline at end of file +RemovePackage=Removing package: %s. +NoKeyword=Cannot search without a keyword. \ No newline at end of file diff --git a/i18n/en-US/usage_search.txt b/i18n/en-US/usage_search.txt new file mode 100644 index 000000000..aadf6306a --- /dev/null +++ b/i18n/en-US/usage_search.txt @@ -0,0 +1,8 @@ +search packages||| +Search searchs packages by keyword. + +The search flags are: + + +The list flags accept a space-separated list of strings. To embed spaces +in an element in the list, surround it with either single or double quotes. diff --git a/i18n/zh-CN/prompt.txt b/i18n/zh-CN/prompt.txt index ab9509246..82b055f08 100644 --- a/i18n/zh-CN/prompt.txt +++ b/i18n/zh-CN/prompt.txt @@ -12,7 +12,7 @@ NotFoundError=合法的源代码托管平台,但正则匹配失败. ErrNoMatch=不被支持的源代码托管平台. PackageNotFound=无法找到包: %s. -MovedFile=成功将文件从 $GOPATH(%s) 移动至当前目录 (%s). +MovedFile=SUCCESS: 成功将文件从 $GOPATH(%s) 移动至当前目录 (%s). PureDownload=已激活纯净下载模式. DownloadOnly=已激活无安装模式. DownloadExDeps=已激活下载示例代码依赖. @@ -27,4 +27,5 @@ SkipInvalidPath=忽略无效的导入路径: %s. InstallByGoGet=正在通过 'go get' 安装包 (%s). NoVCSTool=未找到合适的版本控制工具,已激活纯净下载模式! DownloadStatus=正在下载包: %s. -RemovePackage=正在删除包: %s. \ No newline at end of file +RemovePackage=正在删除包: %s. +NoKeyword=没有关键字,无法搜索. \ No newline at end of file diff --git a/i18n/zh-CN/usage_search.txt b/i18n/zh-CN/usage_search.txt new file mode 100644 index 000000000..aadf6306a --- /dev/null +++ b/i18n/zh-CN/usage_search.txt @@ -0,0 +1,8 @@ +search packages||| +Search searchs packages by keyword. + +The search flags are: + + +The list flags accept a space-separated list of strings. To embed spaces +in an element in the list, surround it with either single or double quotes. diff --git a/install.go b/install.go index 15cbf0812..bebe9c1e8 100644 --- a/install.go +++ b/install.go @@ -74,7 +74,7 @@ func checkFlags(flags map[string]bool, enable []string, args []string, print fun // Check if it a valid flag. if v, ok := flags[f]; ok { flags[f] = !v - if v { + if !v { print(f) } else { fmt.Println("DISABLE: " + f) diff --git a/search.go b/search.go new file mode 100644 index 000000000..2199b0ace --- /dev/null +++ b/search.go @@ -0,0 +1,56 @@ +// Copyright (c) 2013 GPMGo Members. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package main + +import ( + "fmt" + "strings" + + "github.com/GPMGo/gpm/doc" +) + +var cmdSearch = &Command{ + UsageLine: "search [search flags] ", +} + +func init() { + cmdSearch.Run = runSearch +} + +// printSearchPrompt prints prompt information to users to +// let them know what's going on. +func printSearchPrompt(flag string) { + switch flag { + + } +} + +func runSearch(cmd *Command, args []string) { + // Check flags. + num := checkFlags(cmd.Flags, config.AutoEnable.Search, args, printSearchPrompt) + if num == -1 { + return + } + args = args[num:] + + // Check length of arguments. + if len(args) < 1 { + fmt.Printf(fmt.Sprintf("%s\n", promptMsg["NoKeyword"])) + return + } + + // Search from server, and list results. + results, _ := doc.HttpGetBytes(doc.HttpClient, "http://gowalker.org/search?raw=true&q="+args[0], nil) + pkgs := strings.Split(string(results), "|||") + for _, p := range pkgs { + i := strings.Index(p, "$") + if i > -1 { + fmt.Println("-> " + p[:i]) // Package import path. + if len(p) > (i + 1) { + fmt.Println(" " + p[i+1:]) // Synopsis。 + } + } + } +}