github.com/slspeek/camlistore_namedsearch@v0.0.0-20140519202248-ed6f70f7721a/cmd/camtool/searchnames.go (about) 1 /* 2 Copyright 2014 The Camlistore Authors 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package main 18 19 import ( 20 "encoding/json" 21 "flag" 22 "fmt" 23 "os" 24 25 "camlistore.org/pkg/cmdmain" 26 "camlistore.org/pkg/search" 27 ) 28 29 type searchNamesCmd struct{} 30 31 func init() { 32 cmdmain.RegisterCommand("searchnames", func(flags *flag.FlagSet) cmdmain.CommandRunner { 33 return new(searchNamesCmd) 34 }) 35 } 36 37 func (c *searchNamesCmd) Describe() string { 38 return "Gets and sets search aliases" 39 } 40 41 func (c *searchNamesCmd) Usage() { 42 fmt.Fprintln(os.Stderr, "camtool searchnames <name> [substitute]") 43 } 44 45 func (c *searchNamesCmd) RunCommand(args []string) error { 46 switch n := len(args); n { 47 case 1: 48 return c.getNamed(args[0]) 49 case 2: 50 return c.setNamed(args[0], args[1]) 51 default: 52 return cmdmain.UsageError("No more than two arguments allowed") 53 } 54 return nil 55 } 56 57 func (c *searchNamesCmd) getNamed(named string) error { 58 cc := newClient("") 59 gnr, err := cc.GetNamedSearch(&search.GetNamedRequest{Named: named}) 60 if err != nil { 61 return err 62 } 63 64 out, err := json.MarshalIndent(gnr, " ", "") 65 if err != nil { 66 return err 67 } 68 fmt.Fprintln(cmdmain.Stdout, string(out)) 69 return nil 70 } 71 72 func (c *searchNamesCmd) setNamed(named, substitute string) error { 73 cc := newClient("") 74 snr, err := cc.SetNamedSearch(&search.SetNamedRequest{Named: named, Substitute: substitute}) 75 if err != nil { 76 return err 77 } 78 out, err := json.MarshalIndent(snr, " ", "") 79 if err != nil { 80 return err 81 } 82 fmt.Fprintln(cmdmain.Stdout, string(out)) 83 return nil 84 }