Skip to content

Commit

Permalink
refactor: move message-journal action outside of main
Browse files Browse the repository at this point in the history
Signed-off-by: Jason Jerome <jajerome@redhat.com>
  • Loading branch information
DuckBoss committed May 9, 2023
1 parent eac1a98 commit 97fd6e2
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 68 deletions.
69 changes: 69 additions & 0 deletions cmd/yggctl/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,75 @@ func generateControlMessageAction(c *cli.Context) error {
return nil
}

func messageJournalAction(ctx *cli.Context) error {
var conn *dbus.Conn
var err error

if os.Getenv("DBUS_SESSION_BUS_ADDRESS") != "" {
conn, err = dbus.ConnectSessionBus()
} else {
conn, err = dbus.ConnectSystemBus()
}
if err != nil {
return cli.Exit(fmt.Errorf("cannot connect to bus: %w", err), 1)
}

var journalEntries []map[string]string
obj := conn.Object("com.redhat.Yggdrasil1", "/com/redhat/Yggdrasil1")
if err := obj.Call("com.redhat.Yggdrasil1.MessageJournal", dbus.Flags(0)).Store(&journalEntries); err != nil {
return cli.Exit(fmt.Errorf("cannot list messages: %v", err), 1)
}

// Set the worker message truncate length from the user-provided arguments
// Default to 10 if not provided or invalid value provided.
truncateLength := ctx.Int("truncate-message")
if truncateLength <= 0 {
truncateLength = 10
}
// Get the user provided 'worker'/'message-id' arguments to filter journal entries if provided.
selectedWorker := ctx.String("worker")
selectedMessageID := ctx.String("message-id")

// Filter the journal entries by the user provided arguments.
var filteredJournalEntries []string
for idx, entry := range journalEntries {
// If only a specific worker is selected, skip all journal entries from other workers.
if len(selectedWorker) != 0 {
if entry["worker_name"] != selectedWorker {
continue
}
}
// If only a specific message id is selected, skip all journal entries with other message ids.
if len(selectedMessageID) != 0 {
if entry["message_id"] != selectedMessageID {
continue
}
}
// Truncate the worker messages by the truncate length specified.
messageMaxSize := len(entry["worker_message"])
workerMessage := entry["worker_message"]
if messageMaxSize >= truncateLength {
messageMaxSize = truncateLength
workerMessage = fmt.Sprintf("%+v...", entry["worker_message"][:messageMaxSize])
}

filteredJournalEntries = append(filteredJournalEntries, fmt.Sprintf("%d\t%s\t%s\t%s\t%s\t%v\t%s\n", idx, entry["message_id"], entry["sent"], entry["worker_name"], entry["response_to"], entry["worker_event"], workerMessage))
}
if len(filteredJournalEntries) == 0 {
fmt.Println("No journal entries found.")
return nil
}

writer := tabwriter.NewWriter(os.Stdout, 4, 4, 2, ' ', 0)
fmt.Fprint(writer, "MESSAGE #\tMESSAGE ID\tSENT\tWORKER NAME\tRESPONSE TO\tWORKER EVENT\tWORKER MESSAGE\n")
for _, filteredEntry := range filteredJournalEntries {
fmt.Fprint(writer, filteredEntry)
}
writer.Flush()

return nil
}

func workersAction(c *cli.Context) error {
var conn *dbus.Conn
var err error
Expand Down
69 changes: 1 addition & 68 deletions cmd/yggctl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,74 +156,7 @@ func main() {
Required: false,
},
},
Action: func(ctx *cli.Context) error {
var conn *dbus.Conn
var err error

if os.Getenv("DBUS_SESSION_BUS_ADDRESS") != "" {
conn, err = dbus.ConnectSessionBus()
} else {
conn, err = dbus.ConnectSystemBus()
}
if err != nil {
return cli.Exit(fmt.Errorf("cannot connect to bus: %w", err), 1)
}

var journalEntries []map[string]string
obj := conn.Object("com.redhat.Yggdrasil1", "/com/redhat/Yggdrasil1")
if err := obj.Call("com.redhat.Yggdrasil1.MessageJournal", dbus.Flags(0)).Store(&journalEntries); err != nil {
return cli.Exit(fmt.Errorf("cannot list messages: %v", err), 1)
}

// Set the worker message truncate length from the user-provided arguments
// Default to 10 if not provided or invalid value provided.
truncateLength := ctx.Int("truncate-message")
if truncateLength <= 0 {
truncateLength = 10
}
// Get the user provided 'worker'/'message-id' arguments to filter journal entries if provided.
selectedWorker := ctx.String("worker")
selectedMessageID := ctx.String("message-id")

// Filter the journal entries by the user provided arguments.
var filteredJournalEntries []string
for idx, entry := range journalEntries {
// If only a specific worker is selected, skip all journal entries from other workers.
if len(selectedWorker) != 0 {
if entry["worker_name"] != selectedWorker {
continue
}
}
// If only a specific message id is selected, skip all journal entries with other message ids.
if len(selectedMessageID) != 0 {
if entry["message_id"] != selectedMessageID {
continue
}
}
// Truncate the worker messages by the truncate length specified.
messageMaxSize := len(entry["worker_message"])
workerMessage := entry["worker_message"]
if messageMaxSize >= truncateLength {
messageMaxSize = truncateLength
workerMessage = fmt.Sprintf("%+v...", entry["worker_message"][:messageMaxSize])
}

filteredJournalEntries = append(filteredJournalEntries, fmt.Sprintf("%d\t%s\t%s\t%s\t%s\t%v\t%s\n", idx, entry["message_id"], entry["sent"], entry["worker_name"], entry["response_to"], entry["worker_event"], workerMessage))
}
if len(filteredJournalEntries) == 0 {
fmt.Println("No journal entries found.")
return nil
}

writer := tabwriter.NewWriter(os.Stdout, 4, 4, 2, ' ', 0)
fmt.Fprint(writer, "MESSAGE #\tMESSAGE ID\tSENT\tWORKER NAME\tRESPONSE TO\tWORKER EVENT\tWORKER MESSAGE\n")
for _, filteredEntry := range filteredJournalEntries {
fmt.Fprint(writer, filteredEntry)
}
writer.Flush()

return nil
},
Action: messageJournalAction,
},
{
Name: "listen",
Expand Down

0 comments on commit 97fd6e2

Please sign in to comment.