Skip to content

Commit

Permalink
feat: add verbose flag
Browse files Browse the repository at this point in the history
Signed-off-by: 1998-felix <[email protected]>
  • Loading branch information
felixgateru committed May 14, 2024
1 parent b928ae2 commit bf48ff9
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 14 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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
```
```
16 changes: 9 additions & 7 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ var (
data string
options []string
keepAlive uint64
verbose bool
)

func main() {
Expand Down Expand Up @@ -82,27 +83,28 @@ 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()
if err != nil {
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)
}
Expand Down Expand Up @@ -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)
}
Expand All @@ -162,15 +164,15 @@ 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)
res, err := client.Send(args[0], code, coapmsg.MediaType(contentFormat), pld, opts...)
if err != nil {
log.Fatalf("Error sending message: %v", err)
}
printMsg(res)
printMsg(res, verbose)
}
}

Expand Down
13 changes: 8 additions & 5 deletions coap/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
}
Expand All @@ -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)
Expand Down

0 comments on commit bf48ff9

Please sign in to comment.