Skip to content

Instantly share code, notes, and snippets.

@ripienaar
Created January 18, 2021 20:23
Show Gist options
  • Save ripienaar/23002c49940208c723ee90e39163352d to your computer and use it in GitHub Desktop.
Save ripienaar/23002c49940208c723ee90e39163352d to your computer and use it in GitHub Desktop.
$ cat list
n1.example.net
n2.example.net
$ ./test --nodes list <21:21:44
Discovering nodes .... 2
2 / 2 0s [====================================================================] 100%
n1.example.net
Timestamp: 1611001344
n2.example.net
Timestamp: 1611001344
Finished processing 2 / 2 hosts in 688ms
./test --help
usage: demo [<flags>]
Small demo app
Flags:
--help Show context-sensitive help (also try --help-long and --help-man).
--verbose Be verbose
-F, --wf=WF ... Match hosts with a certain fact
-C, --wc=WC ... Match hosts with a certain configuration management class
-A, --wa=WA ... Match hosts with a certain Choria agent
-I, --wi=WI ... Match hosts with a certain Choria identity
-W, --with=FILTER ... Combined classes and facts filter
-S, --select=EXPR Match hosts using a expr compound filter
-T, --target=TARGET Target a specific sub collective
--nodes=NODES List of nodes to interact with in JSON, YAML or TEXT formats
--dm=DM Sets a discovery method (mc, choria)
--discovery-timeout=SECONDS
Timeout for doing discovery
package main
import (
"context"
"os"
"gopkg.in/alecthomas/kingpin.v2"
"github.com/choria-io/go-choria/choria"
"github.com/choria-io/go-choria/client/discovery"
"github.com/choria-io/go-choria/client/rpcutilclient"
)
var (
opt *discovery.StandardOptions
verbose bool
)
func main() {
app := kingpin.New("demo", "Small demo app").Action(run)
app.Flag("verbose", "Be verbose").BoolVar(&verbose)
opt = &discovery.StandardOptions{}
opt.AddFilterFlags(app)
opt.AddFlatFileFlags(app)
opt.AddSelectionFlags(app)
kingpin.MustParse(app.Parse(os.Args[1:]))
}
func run(_ *kingpin.ParseContext) error {
fw, err := choria.New(choria.UserConfig())
if err != nil {
return err
}
opt.SetDefaultsByConfig(fw.Configuration())
rpcutil, err := rpcutilclient.New(rpcutilclient.Progress(), rpcutilclient.Discovery(&rpcutilclient.MetaNS{
Options: opt,
Agent: "rpcutil",
DisablePipedDiscovery: false,
}))
if err != nil {
return err
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
res, err := rpcutil.Ping().Do(ctx)
if err != nil {
return err
}
res.RenderResults(os.Stdout, rpcutilclient.TextFormat, rpcutilclient.DisplayDDL, false, false, true, fw.Logger("render"))
return nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment