From bf48ff957818701359d102c8f0c01f313ea6bd82 Mon Sep 17 00:00:00 2001 From: 1998-felix Date: Tue, 14 May 2024 18:37:27 +0300 Subject: [PATCH] feat: add verbose flag Signed-off-by: 1998-felix --- README.md | 7 +++++-- cmd/main.go | 16 +++++++++------- coap/client.go | 13 ++++++++----- 3 files changed, 22 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index be86c50..92c0c58 100644 --- a/README.md +++ b/README.md @@ -23,11 +23,13 @@ Flags: -H, --host string Host (default "localhost") -O, --options stringArray Options -p, --port string Port (default "5683") + -v, --verbose Verbose output -d, --data string Data(default "") - only available for put, post and delete commands -o, --observe bool Observe - only available for get command Use "coap-cli [command] --help" for more information about a command ``` + The options flag accepts a comma separated string comprising of the optionID defined by [RFC-7252](https://datatracker.ietf.org/doc/html/rfc7252) and a string or hex value. Hex values are used to set options that require numerical values e.g observe, maxAge ## Examples: @@ -47,6 +49,7 @@ coap-cli post channels/0bb5ba61-a66e-4972-bab6-26f19962678f/messages/subtopic -- ```bash coap-cli post channels/0bb5ba61-a66e-4972-bab6-26f19962678f/messages/subtopic --auth 1e1017e6-dee7-45b4-8a13-00e6afeb66eb -d "hello world" -H 0.0.0.0 -p 1234 ``` -```bash + +```bash coap-cli post channels/0bb5ba61-a66e-4972-bab6-26f19962678f/messages/subtopic -options 15,auth=1e1017e6-dee7-45b4-8a13-00e6afeb66eb -d "hello world" -H 0.0.0.0 -p 5683 -``` \ No newline at end of file +``` diff --git a/cmd/main.go b/cmd/main.go index cdaacc0..9e36114 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -31,6 +31,7 @@ var ( data string options []string keepAlive uint64 + verbose bool ) func main() { @@ -82,14 +83,15 @@ func main() { rootCmd.PersistentFlags().IntVarP(&contentFormat, "content-format", "c", 50, "Content format") rootCmd.PersistentFlags().StringArrayVarP(&options, "options", "O", []string{}, "Options") rootCmd.PersistentFlags().Uint64VarP(&keepAlive, "keep-alive", "K", 60, "Keep alive interval") + rootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "Verbose output") if err := rootCmd.Execute(); err != nil { log.Fatalf("Error executing command: %v", err) } } -func printMsg(m *pool.Message) { - if m != nil { +func printMsg(m *pool.Message, verbose bool) { + if m != nil && verbose { log.Printf("\nMESSAGE:\n%s", m.String()) } body, err := m.ReadBody() @@ -97,12 +99,12 @@ func printMsg(m *pool.Message) { log.Fatalf("failed to read body %v", err) } if len(body) > 0 { - log.Printf("\nMESSAGE BODY:\n%s", string(body)) + log.Printf("MESSAGE BODY:\n %s", string(body)) } } func makeRequest(code codes.Code, args []string) { - client, err := coap.New(host+":"+port, keepAlive) + client, err := coap.NewClient(host+":"+port, keepAlive) if err != nil { log.Fatalf("Error coap creating client: %v", err) } @@ -141,7 +143,7 @@ func makeRequest(code codes.Code, args []string) { case codes.GET: switch { case observe: - obs, err := client.Receive(args[0], opts...) + obs, err := client.Receive(args[0], verbose, opts...) if err != nil { log.Fatalf("Error observing resource: %v", err) } @@ -162,7 +164,7 @@ func makeRequest(code codes.Code, args []string) { if err != nil { log.Fatalf("Error sending message: %v", err) } - printMsg(res) + printMsg(res, verbose) } default: pld := strings.NewReader(data) @@ -170,7 +172,7 @@ func makeRequest(code codes.Code, args []string) { if err != nil { log.Fatalf("Error sending message: %v", err) } - printMsg(res) + printMsg(res, verbose) } } diff --git a/coap/client.go b/coap/client.go index b932738..a6e1bf7 100644 --- a/coap/client.go +++ b/coap/client.go @@ -23,6 +23,7 @@ import ( var ( maxRetries uint32 = 10 errInvalidMsgCode = errors.New("message can be GET, POST, PUT or DELETE") + errDialFailed = errors.New("Failed to dial the connection") ) // Client represents CoAP client. @@ -31,18 +32,18 @@ type Client struct { } // New returns new CoAP client connecting it to the server. -func New(addr string, keepAlive uint64) (Client, error) { +func NewClient(addr string, keepAlive uint64) (Client, error) { if keepAlive > 0 { c, err := udp.Dial(addr, options.WithKeepAlive(maxRetries, time.Duration(keepAlive)*time.Second, onInactive)) if err != nil { - log.Fatalf("Error dialing: %v", err) + return Client{}, errors.Join(errDialFailed, err) } return Client{conn: c}, nil } c, err := udp.Dial(addr) if err != nil { - log.Fatalf("Error dialing: %v", err) + return Client{}, errors.Join(errDialFailed, err) } return Client{conn: c}, nil } @@ -67,12 +68,14 @@ func (c Client) Send(path string, msgCode codes.Code, cf message.MediaType, payl } // Receive receives a message. -func (c Client) Receive(path string, opts ...message.Option) (mux.Observation, error) { +func (c Client) Receive(path string, verbose bool, opts ...message.Option) (mux.Observation, error) { ctx, cancel := context.WithTimeout(context.Background(), time.Minute) defer cancel() return c.conn.Observe(ctx, path, func(res *pool.Message) { - fmt.Printf("\nRECEIVED OBSERVE: %v\n", res) + if verbose { + fmt.Printf("RECEIVED OBSERVE: %v\n", res) + } body, err := res.ReadBody() if err != nil { fmt.Println("Error reading message body: ", err)