From 9a45f8367fdcca32b70215faba642a8e3020dce0 Mon Sep 17 00:00:00 2001 From: Jason Jerome Date: Mon, 9 Sep 2024 22:41:17 -0400 Subject: [PATCH] feat: add client-id subcommand to yggctl This patch adds a 'client-id' subcommand to yggctl which retrieves and prints to stdout the active client ID of a yggd instance. This attempts to resolve issue #243 Signed-off-by: Jason Jerome --- cmd/yggctl/actions.go | 26 ++++++++++++++++++++++++++ cmd/yggctl/main.go | 10 ++++++++++ cmd/yggd/client.go | 4 ++++ data/dbus/yggd.conf.in | 4 ++++ dbus/com.redhat.Yggdrasil1.xml | 7 +++++++ 5 files changed, 51 insertions(+) diff --git a/cmd/yggctl/actions.go b/cmd/yggctl/actions.go index ab95ae6f..01cfbaff 100644 --- a/cmd/yggctl/actions.go +++ b/cmd/yggctl/actions.go @@ -173,6 +173,32 @@ func messageJournalAction(ctx *cli.Context) error { return nil } +// clientIDAction connects to the Yggdrasil1 dbus interface +// and attempts to retrieve the active yggd client id +// and display the client id to stdout. +func clientIDAction(ctx *cli.Context) error { + conn, err := connectBus() + if err != nil { + return cli.Exit(fmt.Errorf("cannot connect to bus: %w", err), 1) + } + + obj := conn.Object("com.redhat.Yggdrasil1", "/com/redhat/Yggdrasil1") + propertyName := "com.redhat.Yggdrasil1.ClientID" + result, err := obj.GetProperty(propertyName) + if err != nil { + return fmt.Errorf( + "cannot get property 'com.redhat.Yggdrasil1.ClientID': %v", err) + } + clientID, ok := result.Value().(map[string]string) + if !ok { + return cli.Exit(fmt.Errorf("cannot convert %T to map[string]string", result.Value()), 1) + } + + fmt.Println(clientID) + + return nil +} + func workersAction(c *cli.Context) error { conn, err := connectBus() if err != nil { diff --git a/cmd/yggctl/main.go b/cmd/yggctl/main.go index fb582b54..ee0eaff4 100644 --- a/cmd/yggctl/main.go +++ b/cmd/yggctl/main.go @@ -30,6 +30,16 @@ func main() { } app.Commands = []*cli.Command{ + { + + Name: "client-id", + Usage: "Retrieve the current client id", + UsageText: "yggctl client-id", + Description: `The client-id command retrieves the current client id of the yggdrasil instance`, + Aliases: []string{}, + Flags: []cli.Flag{}, + Action: clientIDAction, + }, { Name: "generate", Usage: `Generate messages for publishing to client "in" topics`, diff --git a/cmd/yggd/client.go b/cmd/yggd/client.go index eac46ba2..38ad9f97 100644 --- a/cmd/yggd/client.go +++ b/cmd/yggd/client.go @@ -201,6 +201,10 @@ func (c *Client) ListWorkers() (map[string]map[string]string, *dbus.Error) { return c.dispatcher.FlattenDispatchers(), nil } +func (c *Client) GetClientID() (string, *dbus.Error) { + return config.DefaultConfig.ClientID, nil +} + // MessageJournal implements the com.redhat.Yggdrasil1.MessageJournal method. func (c *Client) MessageJournal( messageID string, diff --git a/data/dbus/yggd.conf.in b/data/dbus/yggd.conf.in index 26b2d46c..af2071a3 100644 --- a/data/dbus/yggd.conf.in +++ b/data/dbus/yggd.conf.in @@ -13,6 +13,10 @@ + + + diff --git a/dbus/com.redhat.Yggdrasil1.xml b/dbus/com.redhat.Yggdrasil1.xml index 85517001..6baa419d 100644 --- a/dbus/com.redhat.Yggdrasil1.xml +++ b/dbus/com.redhat.Yggdrasil1.xml @@ -8,6 +8,13 @@ programmatic interaction with the yggdrasil system service. --> + + +