Skip to content

Commit

Permalink
print reformat
Browse files Browse the repository at this point in the history
  • Loading branch information
kubemq committed Jan 27, 2021
1 parent f2db079 commit 80e1f2f
Show file tree
Hide file tree
Showing 20 changed files with 415 additions and 83 deletions.
73 changes: 47 additions & 26 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion cmd/commands/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ var commandsShort = `Execute Kubemq 'commands' RPC commands`
func NewCmdCommands(ctx context.Context, cfg *config.Config) *cobra.Command {
cmd := &cobra.Command{
Use: "commands",
Aliases: []string{"cmd"},
Aliases: []string{"cmd", "command"},
Short: commandsShort,
Long: commandsLong,
Example: commandsExamples,
Expand Down
74 changes: 74 additions & 0 deletions cmd/commands/helper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package commands

import (
b64 "encoding/base64"
"fmt"
jsoniter "github.com/json-iterator/go"
kubemq "github.com/kubemq-io/kubemq-go"
"strconv"
)

var json = jsoniter.ConfigCompatibleWithStandardLibrary

type object struct {
Id string `json:"id"`
Channel string `json:"channel,omitempty"`
ClientId string `json:"client_id,omitempty"`
Metadata string `json:"metadata,omitempty"`
Tags map[string]string `json:"tags,omitempty"`
Body string `json:"body,omitempty"`
Executed string `json:"executed,omitempty"`
ExecutedAt string `json:"executed_at,omitempty"`
Error string `json:"error,omitempty"`
payload []byte
}

func newObjectWithCommandReceive(cmd *kubemq.CommandReceive) *object {
obj := &object{
Id: cmd.Id,
Channel: cmd.Channel,
ClientId: cmd.ClientId,
Metadata: cmd.Metadata,
Tags: cmd.Tags,
Body: "",
Executed: "",
ExecutedAt: "",
Error: "",
payload: cmd.Body,
}

sDec, err := b64.StdEncoding.DecodeString(string(cmd.Body))
if err != nil {
obj.Body = string(cmd.Body)
} else {
obj.Body = string(sDec)
}
return obj
}
func newObjectWithCommandResponse(response *kubemq.CommandResponse) *object {
obj := &object{
Id: response.CommandId,
ClientId: response.ResponseClientId,
Tags: response.Tags,
Executed: strconv.FormatBool(response.Executed),
ExecutedAt: response.ExecutedAt.Format("2006-01-02 15:04:05.999"),
Error: response.Error,
}
if !response.Executed {
obj.ExecutedAt = ""
}
return obj
}

func (o *object) String() string {
data, _ := json.MarshalIndent(o, "", " ")
return string(data)
}

func printCommandReceive(command *kubemq.CommandReceive) {
fmt.Println(newObjectWithCommandReceive(command))
}

func printCommandResponse(response *kubemq.CommandResponse) {
fmt.Println(newObjectWithCommandResponse(response))
}
6 changes: 1 addition & 5 deletions cmd/commands/receive.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import (
"github.com/kubemq-io/kubemqctl/pkg/kubemq"
"github.com/kubemq-io/kubemqctl/pkg/utils"
"github.com/spf13/cobra"
"os"
"text/tabwriter"
"time"
)

Expand Down Expand Up @@ -80,7 +78,6 @@ func (o *CommandsReceiveOptions) Run(ctx context.Context) error {
defer func() {
client.Close()
}()
w := tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ', tabwriter.TabIndent)

errChan := make(chan error, 1)
commandsChan, err := client.SubscribeToCommands(ctx, o.channel, o.group, errChan)
Expand All @@ -98,8 +95,7 @@ func (o *CommandsReceiveOptions) Run(ctx context.Context) error {
utils.Println("server disconnected")
return nil
}
fmt.Fprintf(w, "[channel: %s]\t[id: %s]\t[metadata: %s]\t[body: %s]\n", command.Channel, command.Id, command.Metadata, command.Body)
w.Flush()
printCommandReceive(command)
if o.autoResponse {
err = client.R().SetRequestId(command.Id).SetExecutedAt(time.Now()).SetResponseTo(command.ResponseTo).Send(ctx)
if err != nil {
Expand Down
3 changes: 1 addition & 2 deletions cmd/commands/send.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ func (o *CommandsSendOptions) Run(ctx context.Context) error {
if err != nil {
return fmt.Errorf("sending commands message, %s", err.Error())
}
utils.Printlnf("[channel: %s] [client id: %s] -> {id: %s, executed: %t, executed at: %s, error: %s}", msg.Channel, msg.ClientId, msg.Id, res.Executed, res.ExecutedAt.Format("2006-01-02 15:04:05"), res.Error)

printCommandResponse(res)
return nil
}
2 changes: 1 addition & 1 deletion cmd/events/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ var eventsShort = `Execute Kubemq 'events' Pub/Sub commands`
func NewCmdEvents(ctx context.Context, cfg *config.Config) *cobra.Command {
cmd := &cobra.Command{
Use: "events",
Aliases: []string{"e"},
Aliases: []string{"e", "ev"},
Short: eventsShort,
Long: eventsLong,
Example: eventsExamples,
Expand Down
49 changes: 49 additions & 0 deletions cmd/events/helper.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package events

import (
b64 "encoding/base64"
"fmt"
jsoniter "github.com/json-iterator/go"
kubemq "github.com/kubemq-io/kubemq-go"
)

var json = jsoniter.ConfigCompatibleWithStandardLibrary

type object struct {
Id string `json:"id"`
Channel string `json:"channel,omitempty"`
ClientId string `json:"client_id,omitempty"`
Metadata string `json:"metadata,omitempty"`
Tags map[string]string `json:"tags,omitempty"`
Body string `json:"body,omitempty"`
payload []byte
}

func newObjectWithEvent(event *kubemq.Event) *object {
obj := &object{
Id: event.Id,
Channel: event.Channel,
ClientId: event.ClientId,
Metadata: event.Metadata,
Tags: event.Tags,
Body: "",
payload: event.Body,
}

sDec, err := b64.StdEncoding.DecodeString(string(event.Body))
if err != nil {
obj.Body = string(event.Body)
} else {
obj.Body = string(sDec)
}
return obj
}

func (o *object) String() string {
data, _ := json.MarshalIndent(o, "", " ")
return string(data)
}

func printEvent(event *kubemq.Event) {
fmt.Println(newObjectWithEvent(event))
}
6 changes: 1 addition & 5 deletions cmd/events/receive.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import (
"github.com/kubemq-io/kubemqctl/pkg/kubemq"
"github.com/kubemq-io/kubemqctl/pkg/utils"
"github.com/spf13/cobra"
"os"
"text/tabwriter"
)

type EventsReceiveOptions struct {
Expand Down Expand Up @@ -77,7 +75,6 @@ func (o *EventsReceiveOptions) Run(ctx context.Context) error {
defer func() {
client.Close()
}()
w := tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ', tabwriter.TabIndent)

errChan := make(chan error, 1)
eventsChan, err := client.SubscribeToEvents(ctx, o.channel, o.group, errChan)
Expand All @@ -93,8 +90,7 @@ func (o *EventsReceiveOptions) Run(ctx context.Context) error {
utils.Println("server disconnected")
return nil
}
fmt.Fprintf(w, "[channel: %s]\t[id: %s]\t[metadata: %s]\t[body: %s]\n", ev.Channel, ev.Id, ev.Metadata, ev.Body)
w.Flush()
printEvent(ev)
case err := <-errChan:
return fmt.Errorf("server disconnected with error: %s", err.Error())
case <-ctx.Done():
Expand Down
2 changes: 1 addition & 1 deletion cmd/events/send.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func (o *EventsSendOptions) Run(ctx context.Context) error {
if err != nil {
return fmt.Errorf("sending 'events' message, %s", err.Error())
}
utils.Printlnf("[message: %d] [channel: %s] [client id: %s] -> {id: %s, metadata: %s, body: %s}", i, msg.Channel, msg.ClientId, msg.Id, msg.Metadata, msg.Body)
printEvent(msg)
}
}

Expand Down
Loading

0 comments on commit 80e1f2f

Please sign in to comment.