Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
ai3
tools
tabacco
Commits
434f164b
Commit
434f164b
authored
Jun 19, 2019
by
ale
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add a 'query' command to query the metadb
parent
92df8776
Pipeline
#3486
passed with stage
in 46 seconds
Changes
1
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
89 additions
and
0 deletions
+89
-0
cmd/tabacco/query.go
cmd/tabacco/query.go
+89
-0
No files found.
cmd/tabacco/query.go
0 → 100644
View file @
434f164b
package
main
import
(
"context"
"encoding/json"
"errors"
"flag"
"log"
"os"
"time"
"github.com/google/subcommands"
"git.autistici.org/ai3/tools/tabacco"
mdbc
"git.autistici.org/ai3/tools/tabacco/metadb/client"
)
var
rpcTimeout
=
120
*
time
.
Second
type
queryCommand
struct
{
configPath
string
host
string
numVersions
int
}
func
(
c
*
queryCommand
)
Name
()
string
{
return
"query"
}
func
(
c
*
queryCommand
)
Synopsis
()
string
{
return
"query the backup metadata database"
}
func
(
c
*
queryCommand
)
Usage
()
string
{
return
`query [<flags>] <atom_pattern>
Query the backup metadata database.
`
}
func
(
c
*
queryCommand
)
SetFlags
(
f
*
flag
.
FlagSet
)
{
f
.
StringVar
(
&
c
.
configPath
,
"config"
,
"/etc/tabacco/agent.yml"
,
"configuration `file`"
)
f
.
StringVar
(
&
c
.
host
,
"host"
,
""
,
"filter by host"
)
f
.
IntVar
(
&
c
.
numVersions
,
"num-versions"
,
1
,
"return the most recent `N` versions"
)
}
func
(
c
*
queryCommand
)
buildRequest
(
f
*
flag
.
FlagSet
)
(
*
tabacco
.
FindRequest
,
error
)
{
if
f
.
NArg
()
!=
1
{
return
nil
,
errors
.
New
(
"error: wrong number of arguments"
)
}
return
&
tabacco
.
FindRequest
{
Pattern
:
f
.
Arg
(
0
),
Host
:
c
.
host
,
NumVersions
:
c
.
numVersions
,
},
nil
}
func
(
c
*
queryCommand
)
Execute
(
ctx
context
.
Context
,
f
*
flag
.
FlagSet
,
args
...
interface
{})
subcommands
.
ExitStatus
{
req
,
err
:=
c
.
buildRequest
(
f
)
if
err
!=
nil
{
log
.
Printf
(
"error in request: %v"
,
err
)
return
subcommands
.
ExitUsageError
}
// Parse configuration and connect to the metadata store.
config
,
err
:=
tabacco
.
ReadConfig
(
c
.
configPath
)
if
err
!=
nil
{
log
.
Printf
(
"error reading config: %v"
,
err
)
return
subcommands
.
ExitFailure
}
store
,
err
:=
mdbc
.
New
(
config
.
MetadataStoreBackend
)
if
err
!=
nil
{
log
.
Printf
(
"error in metadata client config: %v"
,
err
)
return
subcommands
.
ExitFailure
}
// Make the RPC.
rctx
,
cancel
:=
context
.
WithTimeout
(
ctx
,
rpcTimeout
)
defer
cancel
()
result
,
err
:=
store
.
FindAtoms
(
rctx
,
req
)
if
err
!=
nil
{
log
.
Printf
(
"FindAtoms() error: %v"
,
err
)
return
subcommands
.
ExitFailure
}
data
,
_
:=
json
.
MarshalIndent
(
result
,
""
,
" "
)
os
.
Stdout
.
Write
(
data
)
return
subcommands
.
ExitSuccess
}
func
init
()
{
subcommands
.
Register
(
&
queryCommand
{},
""
)
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment