github.com/Xenoex/gopm@v0.6.5/cmd/search.go (about) 1 // Copyright 2013 gopm authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"): you may 4 // not use this file except in compliance with the License. You may obtain 5 // a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 12 // License for the specific language governing permissions and limitations 13 // under the License. 14 15 package cmd 16 17 // import ( 18 // "encoding/json" 19 // "fmt" 20 // "io/ioutil" 21 // "net/http" 22 23 // "github.com/Unknwon/com" 24 // ) 25 26 // var CmdSearch = &Command{ 27 // UsageLine: "search [keyword]", 28 // Short: "search for package", 29 // Long: ` 30 // search packages 31 32 // The search flags are: 33 34 // -e 35 // search extactly, you should input an exactly package name as keyword 36 // `, 37 // } 38 39 // func init() { 40 // CmdSearch.Run = runSearch 41 // CmdSearch.Flags = map[string]bool{ 42 // "-e": false, 43 // } 44 // } 45 46 // func printSearchPrompt(flag string) { 47 // switch flag { 48 // case "-e": 49 // com.ColorLog("[INFO] You enabled exactly search.\n") 50 // } 51 // } 52 53 // // search packages 54 // func runSearch(cmd *Command, args []string) { 55 56 // // Check length of arguments. 57 // if len(args) < 1 { 58 // com.ColorLog("[ERROR] Please input package's keyword.\n") 59 // return 60 // } 61 62 // var host, port string 63 // host = "localhost" 64 // port = "8991" 65 66 // if cmd.Flags["-e"] { 67 // search(host, port, args[0], true) 68 // } else { 69 // search(host, port, args[0], false) 70 // } 71 // } 72 73 // type searchRes struct { 74 // Pkg string 75 // Desc string 76 // } 77 78 // /* 79 // request local or remote search service to find packages according to keyword inputed 80 // */ 81 // func search(host, port, keyword string, isExactly bool) { 82 // url := fmt.Sprintf("http://%v:%v/search?%v", host, port, keyword) 83 // if isExactly { 84 // url = fmt.Sprintf("http://%v:%v/searche?%v", host, port, keyword) 85 // } 86 // resp, err := http.Get(url) 87 // if err != nil { 88 // com.ColorLog(err.Error()) 89 // return 90 // } 91 // defer resp.Body.Close() 92 93 // if resp.StatusCode == 200 { 94 // contents, err := ioutil.ReadAll(resp.Body) 95 // if err != nil { 96 // com.ColorLog(err.Error()) 97 // return 98 // } 99 100 // pkgs := make([]searchRes, 0) 101 // err = json.Unmarshal(contents, &pkgs) 102 // if err != nil { 103 // com.ColorLog(err.Error()) 104 // return 105 // } 106 // for i, pkg := range pkgs { 107 // fmt.Println(i+1, pkg.Pkg, "\t", pkg.Desc) 108 // } 109 // } else { 110 // com.ColorLog(resp.Status) 111 // } 112 // }