From 34ff113c14804c73bbc8cabebbb85a81e0fe6b68 Mon Sep 17 00:00:00 2001 From: Sebastian Harl Date: Wed, 26 Nov 2014 22:18:20 +0100 Subject: [PATCH] Added support for looking up other object types besides hosts. A query may now be prefixed with : to chose a different type. The default is still hosts. --- server/query.go | 18 +++++++++++++----- templates/main.tmpl | 2 +- 2 files changed, 14 insertions(+), 6 deletions(-) diff --git a/server/query.go b/server/query.go index a8a9501..e303d93 100644 --- a/server/query.go +++ b/server/query.go @@ -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) { diff --git a/templates/main.tmpl b/templates/main.tmpl index c48b223..254a22a 100644 --- a/templates/main.tmpl +++ b/templates/main.tmpl @@ -26,7 +26,7 @@ -- 2.30.2