Skip to content

Commit

Permalink
Feature: Add channel to receive flag
Browse files Browse the repository at this point in the history
Signed-off-by: JeffMboya <[email protected]>
  • Loading branch information
JeffMboya committed Jun 10, 2024
1 parent 8321521 commit a46b80c
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 14 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ endif

install:
for file in $(BUILD_DIR)/*; do \
cp $$file /usr/local/go/bin/magistrala-`basename $$file`; \
cp $$file $(GOBIN)/magistrala-`basename $$file`; \
done

mocks:
Expand Down
16 changes: 15 additions & 1 deletion cli/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@

package cli

import mgxsdk "github.com/absmach/magistrala/pkg/sdk/go"
import (
"fmt"

mgxsdk "github.com/absmach/magistrala/pkg/sdk/go"
)

// Keep SDK handle in global var.
var sdk mgxsdk.SDK
Expand All @@ -12,3 +16,13 @@ var sdk mgxsdk.SDK
func SetSDK(s mgxsdk.SDK) {
sdk = s
}

func AccessCurlFlagChan() {
curlFlagChan := sdk.GetCurlFlagChan()
// Now you can use curlFlagChan
go func() {
for curlCommand := range curlFlagChan {
fmt.Println(curlCommand)
}
}()
}
22 changes: 10 additions & 12 deletions pkg/sdk/go/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -1154,6 +1154,8 @@ type SDK interface {
// err := sdk.DeleteInvitation("userID", "domainID", "token")
// fmt.Println(err)
DeleteInvitation(userID, domainID, token string) (err error)

GetCurlFlagChan() chan string
}

type mgSDK struct {
Expand Down Expand Up @@ -1191,7 +1193,7 @@ type Config struct {
CurlFlagChan chan string
}

// / NewSDK returns new magistrala SDK instance.
// NewSDK returns new magistrala SDK instance.
func NewSDK(conf Config) SDK {
sdk := &mgSDK{
bootstrapURL: conf.BootstrapURL,
Expand All @@ -1213,15 +1215,7 @@ func NewSDK(conf Config) SDK {
},
},
curlFlag: conf.CurlFlag,
curlFlagChan: make(chan string, 1),
}

if sdk.curlFlag {
go func() {
for curlCommand := range sdk.curlFlagChan {
fmt.Println(curlCommand)
}
}()
curlFlagChan: make(chan string),
}

return sdk
Expand Down Expand Up @@ -1251,10 +1245,10 @@ func (sdk *mgSDK) processRequest(method, reqUrl, token string, data []byte, head
if sdk.curlFlag {
curlCommand, err := http2curl.GetCurlCommand(req)
if err != nil {
return nil, nil, nil
return nil, nil, errors.NewSDKError(err)
}
sdk.curlFlagChan <- curlCommand.String()
return make(http.Header), []byte("{}"), nil
return make(http.Header), []byte(curlCommand.String()), nil
}

resp, err := sdk.client.Do(req)
Expand All @@ -1276,6 +1270,10 @@ func (sdk *mgSDK) processRequest(method, reqUrl, token string, data []byte, head
return resp.Header, body, nil
}

// GetCurlFlagChan returns the curlFlagChan.
func (sdk *mgSDK) GetCurlFlagChan() chan string {
return sdk.curlFlagChan
}
func (sdk mgSDK) withQueryParams(baseURL, endpoint string, pm PageMetadata) (string, error) {
q, err := pm.query()
if err != nil {
Expand Down
20 changes: 20 additions & 0 deletions pkg/sdk/mocks/sdk.go

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

0 comments on commit a46b80c

Please sign in to comment.