Code

Added support for looking up other object types besides hosts.
authorSebastian Harl <sh@tokkee.org>
Wed, 26 Nov 2014 21:18:20 +0000 (22:18 +0100)
committerSebastian Harl <sh@tokkee.org>
Wed, 26 Nov 2014 21:18:20 +0000 (22:18 +0100)
A query may now be prefixed with <type>: to chose a different type. The
default is still hosts.

server/query.go
templates/main.tmpl

index a8a9501603ab0667d1c66b6240301d88909f4174..e303d93069a1d094e11489cb8677cef2aa634edc 100644 (file)
@@ -64,25 +64,33 @@ func lookup(req request, s *Server) (*page, error) {
                return nil, errors.New("Empty query")
        }
 
+       typ := "hosts"
        var args string
        for i, tok := range tokens {
-               if i != 0 {
+               if len(args) > 0 {
                        args += " AND"
                }
 
                if fields := strings.SplitN(tok, ":", 2); len(fields) == 2 {
-                       args += fmt.Sprintf(" attribute[%s] = %s",
-                               proto.EscapeString(fields[0]), proto.EscapeString(fields[1]))
+                       if i == 0 && fields[1] == "" {
+                               typ = fields[0]
+                       } else {
+                               args += fmt.Sprintf(" attribute[%s] = %s",
+                                       proto.EscapeString(fields[0]), proto.EscapeString(fields[1]))
+                       }
                } else {
                        args += fmt.Sprintf(" name =~ %s", proto.EscapeString(tok))
                }
        }
 
-       res, err := s.query("LOOKUP hosts MATCHING" + args)
+       res, err := s.query("LOOKUP %s MATCHING"+args, identifier(typ))
        if err != nil {
                return nil, err
        }
-       return tmpl(s.results["hosts"], res)
+       if t, ok := s.results[typ]; ok {
+               return tmpl(t, res)
+       }
+       return nil, fmt.Errorf("Unsupported type %s", typ)
 }
 
 func fetch(req request, s *Server) (*page, error) {
index c48b2234c964d1808e391254d9a8bbf7cab7c6c8..254a22ab714cbeb97f0a13bae3197d09338a0d99 100644 (file)
@@ -26,7 +26,7 @@
 
                        <div class="searchbox">
                                <form action="/lookup" method="POST">
-                                       <input type="text" name="query" value="{{.Query}}" placeholder="Search hosts"
+                                       <input type="text" name="query" value="{{.Query}}" placeholder="Search objects"
                                                required /><button type="submit">GO</button>
                                </form>
                        </div>