Skip to content

Commit

Permalink
refactor: refactor code
Browse files Browse the repository at this point in the history
Signed-off-by: 1998-felix <[email protected]>
  • Loading branch information
felixgateru committed May 6, 2024
1 parent 176e2a9 commit bccac03
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 68 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Flags:
-c, --content-format int Content format (default 50)
-h, --help help for coap-cli
-H, --host string Host (default "localhost")
-0, --options stringArray Options
-O, --options stringArray Options
-p, --port string Port (default "5683")
-d, --data string Data(default "") - only available for put, post and delete commands
-o, --observe bool Observe - only available for get command
Expand Down
134 changes: 67 additions & 67 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,72 @@ func printMsg(m *pool.Message) {
}
}

func makeRequest(code codes.Code, args []string) {
client, err := coap.New(host + ":" + port)
if err != nil {
log.Fatalf("Error coap creating client: %v", err)
}
var opts coapmsg.Options
if options != nil {
for _, optString := range options {
opt := strings.Split(optString, ",")
if len(opt) < 2 {
log.Fatal("Invalid option format")
}
optId, err := strconv.ParseUint(opt[0], 10, 16)
if err != nil {
log.Fatal("Error parsing option id")
}
opts = append(opts, coapmsg.Option{ID: coapmsg.OptionID(optId), Value: []byte(opt[1])})
}
}
if auth != "" {
opts = append(opts, coapmsg.Option{ID: coapmsg.URIQuery, Value: []byte("auth=" + auth)})
}
switch code {
case codes.GET:
switch {
case observe:
obs, err := client.Receive(args[0], opts...)
if err != nil {
log.Fatalf("Error observing resource: %v", err)
}
errs := make(chan error, 2)
go func() {
c := make(chan os.Signal)
signal.Notify(c, syscall.SIGINT)
errs <- fmt.Errorf("%s", <-c)
}()

err = <-errs
obs.Cancel(context.Background(), opts...)
log.Fatalf("Observation terminated: %v", err)
default:
res, err := client.Send(args[0], code, coapmsg.MediaType(contentFormat), nil, opts...)
if err != nil {
log.Fatalf("Error sending message: %v", err)
}
printMsg(res)
}
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)

}
}

func checkArgs(cmd *cobra.Command, args []string) bool {
if len(args) < 1 {
fmt.Fprintf(os.Stdout, color.YellowString("\nusage: %s\n\n"), cmd.Use)
return false
}
return true
}

func main() {
var rootCmd = &cobra.Command{
Use: "coap-cli <method> <URL> [options]",
Expand Down Expand Up @@ -99,75 +165,9 @@ func main() {
rootCmd.PersistentFlags().StringVarP(&port, "port", "p", "5683", "Port")
rootCmd.PersistentFlags().StringVarP(&auth, "auth", "a", "", "Auth")
rootCmd.PersistentFlags().IntVarP(&contentFormat, "content-format", "c", 50, "Content format")
rootCmd.PersistentFlags().StringArrayVarP(&options, "options", "0", []string{}, "Options")
rootCmd.PersistentFlags().StringArrayVarP(&options, "options", "O", []string{}, "Options")

if err := rootCmd.Execute(); err != nil {
log.Fatalf("Error executing command: %v", err)
}
}

func makeRequest(code codes.Code, args []string) {
client, err := coap.New(host + ":" + port)
if err != nil {
log.Fatalf("Error coap creating client: %v", err)
}
var opts coapmsg.Options
if options != nil {
for _, optString := range options {
opt := strings.Split(optString, ",")
if len(opt) < 2 {
log.Fatal("Invalid option format")
}
optId, err := strconv.ParseUint(opt[0], 10, 16)
if err != nil {
log.Fatal("Error parsing option id")
}
opts = append(opts, coapmsg.Option{ID: coapmsg.OptionID(optId), Value: []byte(opt[1])})
}
}
if auth != "" {
opts = append(opts, coapmsg.Option{ID: coapmsg.URIQuery, Value: []byte("auth=" + auth)})
}
switch {
case !observe:
switch code {
case codes.GET:
res, err := client.Send(args[0], code, coapmsg.MediaType(contentFormat), nil, opts...)
if err != nil {
log.Fatalf("Error sending message: %v", err)
}
printMsg(res)

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)
}
case observe:
obs, err := client.Receive(args[0], opts...)
if err != nil {
log.Fatalf("Error observing resource: %v", err)
}
errs := make(chan error, 2)
go func() {
c := make(chan os.Signal)
signal.Notify(c, syscall.SIGINT)
errs <- fmt.Errorf("%s", <-c)
}()

err = <-errs
obs.Cancel(context.Background())
log.Fatalf("Observation terminated: %v", err)
}
}

func checkArgs(cmd *cobra.Command, args []string) bool {
if len(args) < 1 {
fmt.Fprintf(os.Stdout, color.YellowString("\nusage: %s\n\n"), cmd.Use)
return false
}
return true
}

0 comments on commit bccac03

Please sign in to comment.