diff --git a/Makefile b/Makefile index 2b0b70e9289..33162386c77 100644 --- a/Makefile +++ b/Makefile @@ -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: diff --git a/cli/sdk.go b/cli/sdk.go index 9f7e273cb26..1b57d8e9e7d 100644 --- a/cli/sdk.go +++ b/cli/sdk.go @@ -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 @@ -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) + } + }() +} diff --git a/pkg/sdk/go/sdk.go b/pkg/sdk/go/sdk.go index 27f2a21e057..b12050892ef 100644 --- a/pkg/sdk/go/sdk.go +++ b/pkg/sdk/go/sdk.go @@ -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 { @@ -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, @@ -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 @@ -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) @@ -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 { diff --git a/pkg/sdk/mocks/sdk.go b/pkg/sdk/mocks/sdk.go index 63146f10f47..6b549de0b17 100644 --- a/pkg/sdk/mocks/sdk.go +++ b/pkg/sdk/mocks/sdk.go @@ -1176,6 +1176,26 @@ func (_m *SDK) EnableUser(id string, token string) (sdk.User, errors.SDKError) { return r0, r1 } +// GetCurlFlagChan provides a mock function with given fields: +func (_m *SDK) GetCurlFlagChan() chan string { + ret := _m.Called() + + if len(ret) == 0 { + panic("no return value specified for GetCurlFlagChan") + } + + var r0 chan string + if rf, ok := ret.Get(0).(func() chan string); ok { + r0 = rf() + } else { + if ret.Get(0) != nil { + r0 = ret.Get(0).(chan string) + } + } + + return r0 +} + // Group provides a mock function with given fields: id, token func (_m *SDK) Group(id string, token string) (sdk.Group, errors.SDKError) { ret := _m.Called(id, token)