From 029a3c83669d3e12f1fff520a22ac9df6a9bbcc2 Mon Sep 17 00:00:00 2001 From: JeffMboya Date: Fri, 17 May 2024 14:20:54 +0300 Subject: [PATCH 01/19] Fix: Typo Signed-off-by: JeffMboya --- cli/users.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cli/users.go b/cli/users.go index 3857f5f28a..e82c991317 100644 --- a/cli/users.go +++ b/cli/users.go @@ -15,7 +15,7 @@ var cmdUsers = []cobra.Command{ { Use: "create ", Short: "Create user", - Long: "Create user with provided name, username and password. Token in optional\n" + + Long: "Create user with provided name, username and password. Token is optional\n" + "For example:\n" + "\tmagistrala-cli users create user user@example.com 12345678 $USER_AUTH_TOKEN\n", Run: func(cmd *cobra.Command, args []string) { From 06414af11df3350a02256c36822e2b58e1fb1c96 Mon Sep 17 00:00:00 2001 From: JeffMboya Date: Sun, 19 May 2024 12:41:57 +0300 Subject: [PATCH 02/19] Feature: add --curl flag to CLI Signed-off-by: JeffMboya --- Makefile | 4 ++-- cli/utils.go | 2 ++ cmd/cli/main.go | 7 +++++++ 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index eadc28afa1..9b31865743 100644 --- a/Makefile +++ b/Makefile @@ -115,7 +115,7 @@ endif install: for file in $(BUILD_DIR)/*; do \ - cp $$file $(GOBIN)/magistrala-`basename $$file`; \ + cp $$file /usr/local/go/bin/magistrala-`basename $$file`; \ done mocks: @@ -260,4 +260,4 @@ run_addons: check_certs $(foreach SVC,$(RUN_ADDON_ARGS),$(if $(filter $(SVC),$(ADDON_SERVICES) $(EXTERNAL_SERVICES)),,$(error Invalid Service $(SVC)))) @for SVC in $(RUN_ADDON_ARGS); do \ MG_ADDONS_CERTS_PATH_PREFIX="../." docker compose -f docker/addons/$$SVC/docker-compose.yml -p $(DOCKER_PROJECT) --env-file ./docker/.env $(DOCKER_COMPOSE_COMMAND) $(args) & \ - done + done \ No newline at end of file diff --git a/cli/utils.go b/cli/utils.go index f18c28b088..2890efe4e2 100644 --- a/cli/utils.go +++ b/cli/utils.go @@ -36,6 +36,8 @@ var ( Contact string = "" // RawOutput raw output mode. RawOutput bool = false + // Curl convert to curl command. + Curl bool = false ) func logJSON(iList ...interface{}) { diff --git a/cmd/cli/main.go b/cmd/cli/main.go index 38764c817d..d462d8114f 100644 --- a/cmd/cli/main.go +++ b/cmd/cli/main.go @@ -172,6 +172,13 @@ func main() { cli.RawOutput, "Enables raw output mode for easier parsing of output", ) + rootCmd.PersistentFlags().BoolVarP( + &cli.Curl, + "curl", + "x", + cli.Curl, + "Convert HTTP request to cURL command", + ) // Client and Channels Flags rootCmd.PersistentFlags().Uint64VarP( From a7695468bdc44b7c98788bd42678e3b4595a4dcd Mon Sep 17 00:00:00 2001 From: JeffMboya Date: Wed, 29 May 2024 10:05:08 +0300 Subject: [PATCH 03/19] refactor: add http2curl library Signed-off-by: JeffMboya --- go.mod | 1 + go.sum | 2 ++ pkg/sdk/go/sdk.go | 9 +++++++++ 3 files changed, 12 insertions(+) diff --git a/go.mod b/go.mod index 6a4c522171..b9e1efbe75 100644 --- a/go.mod +++ b/go.mod @@ -188,4 +188,5 @@ require ( gopkg.in/ini.v1 v1.67.0 // indirect gopkg.in/yaml.v2 v2.4.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect + moul.io/http2curl v1.0.0 // indirect ) diff --git a/go.sum b/go.sum index 40ed353561..df28cc31d8 100644 --- a/go.sum +++ b/go.sum @@ -702,3 +702,5 @@ gotest.tools/v3 v3.3.0/go.mod h1:Mcr9QNxkg0uMvy/YElmo4SpXgJKWgQvYrT7Kw5RzJ1A= honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg= +moul.io/http2curl v1.0.0 h1:6XwpyZOYsgZJrU8exnG87ncVkU1FVCcTRpwzOkTDUi8= +moul.io/http2curl v1.0.0/go.mod h1:f6cULg+e4Md/oW1cYmwW4IWQOVl2lGbmCNGOHvzX2kE= diff --git a/pkg/sdk/go/sdk.go b/pkg/sdk/go/sdk.go index 1f407faf22..043f627c39 100644 --- a/pkg/sdk/go/sdk.go +++ b/pkg/sdk/go/sdk.go @@ -9,6 +9,7 @@ import ( "encoding/json" "fmt" "io" + "log" "net/http" "net/url" "strconv" @@ -16,6 +17,7 @@ import ( "time" "github.com/absmach/magistrala/pkg/errors" + "moul.io/http2curl" ) const ( @@ -1233,6 +1235,13 @@ func (sdk mgSDK) processRequest(method, reqUrl, token string, data []byte, heade req.Header.Set("Authorization", token) } + // Convert the http.Request to a curl command and print it + cmd, err := http2curl.GetCurlCommand(req) + if err != nil { + log.Fatal(err) + } + fmt.Println(cmd) + resp, err := sdk.client.Do(req) if err != nil { return make(http.Header), []byte{}, errors.NewSDKError(err) From 0e956391a359b4ca73caa0b42aa12eb4b98d321a Mon Sep 17 00:00:00 2001 From: JeffMboya Date: Thu, 6 Jun 2024 17:29:35 +0300 Subject: [PATCH 04/19] Feature: Add channel to store flags Signed-off-by: JeffMboya --- pkg/sdk/go/sdk.go | 1 + 1 file changed, 1 insertion(+) diff --git a/pkg/sdk/go/sdk.go b/pkg/sdk/go/sdk.go index 043f627c39..3f574d187c 100644 --- a/pkg/sdk/go/sdk.go +++ b/pkg/sdk/go/sdk.go @@ -1170,6 +1170,7 @@ type mgSDK struct { msgContentType ContentType client *http.Client + flagsChan chan bool } // Config contains sdk configuration parameters. From 7297c474cfd1ed208d4263496e6f96a1419ed195 Mon Sep 17 00:00:00 2001 From: JeffMboya Date: Fri, 7 Jun 2024 15:55:04 +0300 Subject: [PATCH 05/19] Feature: Add channel to store flags Signed-off-by: JeffMboya --- cli/utils.go | 2 -- cmd/cli/main.go | 4 ++-- pkg/sdk/go/sdk.go | 26 +++++++++++++++----------- 3 files changed, 17 insertions(+), 15 deletions(-) diff --git a/cli/utils.go b/cli/utils.go index 2890efe4e2..f18c28b088 100644 --- a/cli/utils.go +++ b/cli/utils.go @@ -36,8 +36,6 @@ var ( Contact string = "" // RawOutput raw output mode. RawOutput bool = false - // Curl convert to curl command. - Curl bool = false ) func logJSON(iList ...interface{}) { diff --git a/cmd/cli/main.go b/cmd/cli/main.go index d462d8114f..994cb4f6f8 100644 --- a/cmd/cli/main.go +++ b/cmd/cli/main.go @@ -173,10 +173,10 @@ func main() { "Enables raw output mode for easier parsing of output", ) rootCmd.PersistentFlags().BoolVarP( - &cli.Curl, + &sdkConf.CurlFlag, "curl", "x", - cli.Curl, + false, "Convert HTTP request to cURL command", ) diff --git a/pkg/sdk/go/sdk.go b/pkg/sdk/go/sdk.go index 3f574d187c..4348571b92 100644 --- a/pkg/sdk/go/sdk.go +++ b/pkg/sdk/go/sdk.go @@ -9,7 +9,6 @@ import ( "encoding/json" "fmt" "io" - "log" "net/http" "net/url" "strconv" @@ -1170,7 +1169,8 @@ type mgSDK struct { msgContentType ContentType client *http.Client - flagsChan chan bool + curlFlag bool + curlFlagChan chan string } // Config contains sdk configuration parameters. @@ -1187,9 +1187,11 @@ type Config struct { MsgContentType ContentType TLSVerification bool + CurlFlag bool + CurlFlagChan chan string } -// NewSDK returns new magistrala SDK instance. +// / NewSDK returns new magistrala SDK instance. func NewSDK(conf Config) SDK { return &mgSDK{ bootstrapURL: conf.BootstrapURL, @@ -1210,19 +1212,19 @@ func NewSDK(conf Config) SDK { }, }, }, + curlFlag: conf.CurlFlag, + curlFlagChan: make(chan string), } } // processRequest creates and send a new HTTP request, and checks for errors in the HTTP response. // It then returns the response headers, the response body, and the associated error(s) (if any). -func (sdk mgSDK) processRequest(method, reqUrl, token string, data []byte, headers map[string]string, expectedRespCodes ...int) (http.Header, []byte, errors.SDKError) { +func (sdk *mgSDK) processRequest(method, reqUrl, token string, data []byte, headers map[string]string, expectedRespCodes ...int) (http.Header, []byte, errors.SDKError) { req, err := http.NewRequest(method, reqUrl, bytes.NewReader(data)) if err != nil { return make(http.Header), []byte{}, errors.NewSDKError(err) } - // Sets a default value for the Content-Type. - // Overridden if Content-Type is passed in the headers arguments. req.Header.Add("Content-Type", string(CTJSON)) for key, value := range headers { @@ -1236,12 +1238,14 @@ func (sdk mgSDK) processRequest(method, reqUrl, token string, data []byte, heade req.Header.Set("Authorization", token) } - // Convert the http.Request to a curl command and print it - cmd, err := http2curl.GetCurlCommand(req) - if err != nil { - log.Fatal(err) + if sdk.curlFlag { + curlCommand, err := http2curl.GetCurlCommand(req) + if err != nil { + return nil, nil, nil + } + sdk.curlFlagChan <- curlCommand.String() + return nil, nil, nil } - fmt.Println(cmd) resp, err := sdk.client.Do(req) if err != nil { From c622b5223edb6c4e0c9b61e6a7098edfb1939fd6 Mon Sep 17 00:00:00 2001 From: JeffMboya Date: Sun, 9 Jun 2024 21:31:12 +0300 Subject: [PATCH 06/19] Feature: Add channel to receive flag Signed-off-by: JeffMboya --- pkg/sdk/go/sdk.go | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/pkg/sdk/go/sdk.go b/pkg/sdk/go/sdk.go index 4348571b92..5524a0de9b 100644 --- a/pkg/sdk/go/sdk.go +++ b/pkg/sdk/go/sdk.go @@ -1193,7 +1193,7 @@ type Config struct { // / NewSDK returns new magistrala SDK instance. func NewSDK(conf Config) SDK { - return &mgSDK{ + sdk := &mgSDK{ bootstrapURL: conf.BootstrapURL, certsURL: conf.CertsURL, httpAdapterURL: conf.HTTPAdapterURL, @@ -1213,8 +1213,18 @@ func NewSDK(conf Config) SDK { }, }, curlFlag: conf.CurlFlag, - curlFlagChan: make(chan string), + curlFlagChan: make(chan string, 1), } + + if sdk.curlFlag { + go func() { + for curlCommand := range sdk.curlFlagChan { + fmt.Println(curlCommand) + } + }() + } + + return sdk } // processRequest creates and send a new HTTP request, and checks for errors in the HTTP response. @@ -1244,7 +1254,7 @@ func (sdk *mgSDK) processRequest(method, reqUrl, token string, data []byte, head return nil, nil, nil } sdk.curlFlagChan <- curlCommand.String() - return nil, nil, nil + return make(http.Header), []byte("{}"), nil } resp, err := sdk.client.Do(req) From 3ae5aa8bb1be17d544196ca3c9bc3935733c6218 Mon Sep 17 00:00:00 2001 From: JeffMboya Date: Mon, 10 Jun 2024 08:50:36 +0300 Subject: [PATCH 07/19] Feature: Add channel to receive flag Signed-off-by: JeffMboya --- Makefile | 2 +- cli/sdk.go | 16 +++++++++++++++- pkg/sdk/go/sdk.go | 22 ++++++++++------------ pkg/sdk/mocks/sdk.go | 20 ++++++++++++++++++++ 4 files changed, 46 insertions(+), 14 deletions(-) diff --git a/Makefile b/Makefile index 9b31865743..af87488b59 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 9f7e273cb2..1b57d8e9e7 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 5524a0de9b..8b560871fe 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 9320502839..b8a5cccbf6 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) From 2bc5a3f65146518156674f135683ae914820b535 Mon Sep 17 00:00:00 2001 From: JeffMboya Date: Mon, 10 Jun 2024 08:53:52 +0300 Subject: [PATCH 08/19] Feature: Add flag to print cURL commands Signed-off-by: JeffMboya --- Makefile | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index af87488b59..b9ee7589fc 100644 --- a/Makefile +++ b/Makefile @@ -260,4 +260,5 @@ run_addons: check_certs $(foreach SVC,$(RUN_ADDON_ARGS),$(if $(filter $(SVC),$(ADDON_SERVICES) $(EXTERNAL_SERVICES)),,$(error Invalid Service $(SVC)))) @for SVC in $(RUN_ADDON_ARGS); do \ MG_ADDONS_CERTS_PATH_PREFIX="../." docker compose -f docker/addons/$$SVC/docker-compose.yml -p $(DOCKER_PROJECT) --env-file ./docker/.env $(DOCKER_COMPOSE_COMMAND) $(args) & \ - done \ No newline at end of file + done + \ No newline at end of file From 399b449e76f0555a1f5ba6cd06d9267d0faf04ef Mon Sep 17 00:00:00 2001 From: JeffMboya Date: Mon, 10 Jun 2024 08:54:47 +0300 Subject: [PATCH 09/19] Feature: Add flag to print cURL commands Signed-off-by: JeffMboya --- Makefile | 1 + 1 file changed, 1 insertion(+) diff --git a/Makefile b/Makefile index b9ee7589fc..06c6dddbf8 100644 --- a/Makefile +++ b/Makefile @@ -261,4 +261,5 @@ run_addons: check_certs @for SVC in $(RUN_ADDON_ARGS); do \ MG_ADDONS_CERTS_PATH_PREFIX="../." docker compose -f docker/addons/$$SVC/docker-compose.yml -p $(DOCKER_PROJECT) --env-file ./docker/.env $(DOCKER_COMPOSE_COMMAND) $(args) & \ done + \ No newline at end of file From abed0c11b0f1d06d280324a454ce96c633103cba Mon Sep 17 00:00:00 2001 From: JeffMboya Date: Mon, 10 Jun 2024 08:56:09 +0300 Subject: [PATCH 10/19] Feature: Add flag to print cURL commands Signed-off-by: JeffMboya --- Makefile | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/Makefile b/Makefile index 06c6dddbf8..af87488b59 100644 --- a/Makefile +++ b/Makefile @@ -260,6 +260,4 @@ run_addons: check_certs $(foreach SVC,$(RUN_ADDON_ARGS),$(if $(filter $(SVC),$(ADDON_SERVICES) $(EXTERNAL_SERVICES)),,$(error Invalid Service $(SVC)))) @for SVC in $(RUN_ADDON_ARGS); do \ MG_ADDONS_CERTS_PATH_PREFIX="../." docker compose -f docker/addons/$$SVC/docker-compose.yml -p $(DOCKER_PROJECT) --env-file ./docker/.env $(DOCKER_COMPOSE_COMMAND) $(args) & \ - done - - \ No newline at end of file + done \ No newline at end of file From 4d12258dfd83d69097aa7347532370970f0b33c3 Mon Sep 17 00:00:00 2001 From: JeffMboya Date: Mon, 10 Jun 2024 09:18:21 +0300 Subject: [PATCH 11/19] Feature: Add flag to print cURL commands Signed-off-by: JeffMboya --- Makefile | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index af87488b59..b9ee7589fc 100644 --- a/Makefile +++ b/Makefile @@ -260,4 +260,5 @@ run_addons: check_certs $(foreach SVC,$(RUN_ADDON_ARGS),$(if $(filter $(SVC),$(ADDON_SERVICES) $(EXTERNAL_SERVICES)),,$(error Invalid Service $(SVC)))) @for SVC in $(RUN_ADDON_ARGS); do \ MG_ADDONS_CERTS_PATH_PREFIX="../." docker compose -f docker/addons/$$SVC/docker-compose.yml -p $(DOCKER_PROJECT) --env-file ./docker/.env $(DOCKER_COMPOSE_COMMAND) $(args) & \ - done \ No newline at end of file + done + \ No newline at end of file From e9e4a677d8e8d8f11c76805527225771f845aeb2 Mon Sep 17 00:00:00 2001 From: JeffMboya Date: Mon, 10 Jun 2024 09:47:35 +0300 Subject: [PATCH 12/19] Feature: Add flag to print cURL commands Signed-off-by: JeffMboya --- Makefile | 1 + 1 file changed, 1 insertion(+) diff --git a/Makefile b/Makefile index b9ee7589fc..06c6dddbf8 100644 --- a/Makefile +++ b/Makefile @@ -261,4 +261,5 @@ run_addons: check_certs @for SVC in $(RUN_ADDON_ARGS); do \ MG_ADDONS_CERTS_PATH_PREFIX="../." docker compose -f docker/addons/$$SVC/docker-compose.yml -p $(DOCKER_PROJECT) --env-file ./docker/.env $(DOCKER_COMPOSE_COMMAND) $(args) & \ done + \ No newline at end of file From 0ddfaed68e16c3101c4bc083cf41aab76670870e Mon Sep 17 00:00:00 2001 From: JeffMboya Date: Mon, 10 Jun 2024 09:48:20 +0300 Subject: [PATCH 13/19] Feature: Add flag to print cURL commands Signed-off-by: JeffMboya --- Makefile | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Makefile b/Makefile index 06c6dddbf8..cd24fe886b 100644 --- a/Makefile +++ b/Makefile @@ -262,4 +262,10 @@ run_addons: check_certs MG_ADDONS_CERTS_PATH_PREFIX="../." docker compose -f docker/addons/$$SVC/docker-compose.yml -p $(DOCKER_PROJECT) --env-file ./docker/.env $(DOCKER_COMPOSE_COMMAND) $(args) & \ done + + + + + + \ No newline at end of file From bd622c4b81f92721a4ced1cdb46b4ad57b3a158d Mon Sep 17 00:00:00 2001 From: JeffMboya Date: Mon, 10 Jun 2024 09:49:11 +0300 Subject: [PATCH 14/19] Feature: Add flag to print cURL commands Signed-off-by: JeffMboya --- Makefile | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/Makefile b/Makefile index cd24fe886b..af87488b59 100644 --- a/Makefile +++ b/Makefile @@ -260,12 +260,4 @@ run_addons: check_certs $(foreach SVC,$(RUN_ADDON_ARGS),$(if $(filter $(SVC),$(ADDON_SERVICES) $(EXTERNAL_SERVICES)),,$(error Invalid Service $(SVC)))) @for SVC in $(RUN_ADDON_ARGS); do \ MG_ADDONS_CERTS_PATH_PREFIX="../." docker compose -f docker/addons/$$SVC/docker-compose.yml -p $(DOCKER_PROJECT) --env-file ./docker/.env $(DOCKER_COMPOSE_COMMAND) $(args) & \ - done - - - - - - - - \ No newline at end of file + done \ No newline at end of file From 38441ff62628ddf51c9e4cf3e9b09a10945b4b0f Mon Sep 17 00:00:00 2001 From: JeffMboya Date: Mon, 10 Jun 2024 09:53:17 +0300 Subject: [PATCH 15/19] Feature: Add flag to print cURL commands Signed-off-by: JeffMboya --- cli/sdk.go | 1 - pkg/sdk/go/sdk.go | 4 +++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/cli/sdk.go b/cli/sdk.go index 1b57d8e9e7..52cc5a2bf6 100644 --- a/cli/sdk.go +++ b/cli/sdk.go @@ -19,7 +19,6 @@ func SetSDK(s mgxsdk.SDK) { 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 8b560871fe..171d885864 100644 --- a/pkg/sdk/go/sdk.go +++ b/pkg/sdk/go/sdk.go @@ -1223,12 +1223,14 @@ func NewSDK(conf Config) SDK { // processRequest creates and send a new HTTP request, and checks for errors in the HTTP response. // It then returns the response headers, the response body, and the associated error(s) (if any). -func (sdk *mgSDK) processRequest(method, reqUrl, token string, data []byte, headers map[string]string, expectedRespCodes ...int) (http.Header, []byte, errors.SDKError) { +func (sdk mgSDK) processRequest(method, reqUrl, token string, data []byte, headers map[string]string, expectedRespCodes ...int) (http.Header, []byte, errors.SDKError) { req, err := http.NewRequest(method, reqUrl, bytes.NewReader(data)) if err != nil { return make(http.Header), []byte{}, errors.NewSDKError(err) } + // Sets a default value for the Content-Type. + // Overridden if Content-Type is passed in the headers arguments. req.Header.Add("Content-Type", string(CTJSON)) for key, value := range headers { From 42b5a95f597c1ea6ef7038ff7a45d378fa46f7d4 Mon Sep 17 00:00:00 2001 From: JeffMboya Date: Mon, 10 Jun 2024 09:55:10 +0300 Subject: [PATCH 16/19] Feature: Add flag to print cURL commands Signed-off-by: JeffMboya --- pkg/sdk/go/sdk.go | 1 + 1 file changed, 1 insertion(+) diff --git a/pkg/sdk/go/sdk.go b/pkg/sdk/go/sdk.go index 171d885864..7341b99372 100644 --- a/pkg/sdk/go/sdk.go +++ b/pkg/sdk/go/sdk.go @@ -1276,6 +1276,7 @@ func (sdk mgSDK) processRequest(method, reqUrl, token string, data []byte, heade 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 { From 390630c5f015af57d383601c84f8ac908972b6af Mon Sep 17 00:00:00 2001 From: JeffMboya Date: Mon, 10 Jun 2024 16:39:19 +0300 Subject: [PATCH 17/19] Feature: Add flag to print cURL commands Signed-off-by: JeffMboya --- cli/sdk.go | 11 ----------- go.mod | 3 ++- go.sum | 8 ++++++++ pkg/sdk/go/sdk.go | 18 ++++-------------- 4 files changed, 14 insertions(+), 26 deletions(-) diff --git a/cli/sdk.go b/cli/sdk.go index 52cc5a2bf6..683e7bf3e4 100644 --- a/cli/sdk.go +++ b/cli/sdk.go @@ -4,8 +4,6 @@ package cli import ( - "fmt" - mgxsdk "github.com/absmach/magistrala/pkg/sdk/go" ) @@ -16,12 +14,3 @@ var sdk mgxsdk.SDK func SetSDK(s mgxsdk.SDK) { sdk = s } - -func AccessCurlFlagChan() { - curlFlagChan := sdk.GetCurlFlagChan() - go func() { - for curlCommand := range curlFlagChan { - fmt.Println(curlCommand) - } - }() -} diff --git a/go.mod b/go.mod index b9e1efbe75..31a826b367 100644 --- a/go.mod +++ b/go.mod @@ -61,6 +61,7 @@ require ( google.golang.org/grpc v1.64.0 google.golang.org/protobuf v1.34.1 gopkg.in/gomail.v2 v2.0.0-20160411212932-81ebce5c23df + moul.io/http2curl v1.0.0 ) require ( @@ -157,6 +158,7 @@ require ( github.com/samber/lo v1.39.0 // indirect github.com/segmentio/asm v1.2.0 // indirect github.com/sirupsen/logrus v1.9.3 // indirect + github.com/smartystreets/goconvey v1.8.1 // indirect github.com/sourcegraph/conc v0.3.0 // indirect github.com/spf13/afero v1.11.0 // indirect github.com/spf13/cast v1.6.0 // indirect @@ -188,5 +190,4 @@ require ( gopkg.in/ini.v1 v1.67.0 // indirect gopkg.in/yaml.v2 v2.4.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect - moul.io/http2curl v1.0.0 // indirect ) diff --git a/go.sum b/go.sum index df28cc31d8..ba13203fc0 100644 --- a/go.sum +++ b/go.sum @@ -162,6 +162,8 @@ github.com/gookit/color v1.5.4 h1:FZmqs7XOyGgCAxmWyPslpiok1k05wmY3SJTytgvYFs0= github.com/gookit/color v1.5.4/go.mod h1:pZJOeOS8DM43rXbp4AZo1n9zCU2qjpcRko0b6/QJi9w= github.com/gopcua/opcua v0.1.6 h1:B9SVRKQGzcWcwP2QPYN93Uku32+3wL+v5cgzBxE6V5I= github.com/gopcua/opcua v0.1.6/go.mod h1:INwnDoRxmNWAt7+tzqxuGqQkSF2c1C69VAL0c2q6AcY= +github.com/gopherjs/gopherjs v1.17.2 h1:fQnZVsXk8uxXIStYb0N4bGk7jeyTalG/wsZjQ25dO0g= +github.com/gopherjs/gopherjs v1.17.2/go.mod h1:pRRIvn/QzFLrKfvEz3qUuEhtE/zLCWfreZ6J5gM2i+k= github.com/gorilla/websocket v1.5.1 h1:gmztn0JnHVt9JZquRuzLw3g4wouNVzKL15iLr/zn/QY= github.com/gorilla/websocket v1.5.1/go.mod h1:x3kM2JMyaluk02fnUJpQuwD2dCS5NDG2ZHL0uE0tcaY= github.com/grpc-ecosystem/go-grpc-middleware v1.4.0 h1:UH//fgunKIs4JdUbpDl1VZCDaL56wXCB/5+wF6uHfaI= @@ -265,6 +267,8 @@ github.com/jackc/puddle/v2 v2.2.1 h1:RhxXJtFG022u4ibrCSMSiu5aOq1i77R3OHKNJj77OAk github.com/jackc/puddle/v2 v2.2.1/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4= github.com/jmoiron/sqlx v1.4.0 h1:1PLqN7S1UYp5t4SrVVnt4nUVNemrDAtxlulVe+Qgm3o= github.com/jmoiron/sqlx v1.4.0/go.mod h1:ZrZ7UsYB/weZdl2Bxg6jCRO9c3YHl8r3ahlKmRT4JLY= +github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo= +github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU= github.com/juju/gnuflag v0.0.0-20171113085948-2ce1bb71843d/go.mod h1:2PavIy+JPciBPrBUjwbNvtwB6RQlve+hkpll6QSNmOE= github.com/jzelinskie/stringz v0.0.3 h1:0GhG3lVMYrYtIvRbxvQI6zqRTT1P1xyQlpa0FhfUXas= github.com/jzelinskie/stringz v0.0.3/go.mod h1:hHYbgxJuNLRw91CmpuFsYEOyQqpDVFg8pvEh23vy4P0= @@ -415,6 +419,10 @@ github.com/sirupsen/logrus v1.4.1/go.mod h1:ni0Sbl8bgC9z8RoU9G6nDWqqs/fq4eDPysMB github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE= github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ= github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ= +github.com/smarty/assertions v1.15.0 h1:cR//PqUBUiQRakZWqBiFFQ9wb8emQGDb0HeGdqGByCY= +github.com/smarty/assertions v1.15.0/go.mod h1:yABtdzeQs6l1brC900WlRNwj6ZR55d7B+E8C6HtKdec= +github.com/smartystreets/goconvey v1.8.1 h1:qGjIddxOk4grTu9JPOU31tVfq3cNdBlNa5sSznIX1xY= +github.com/smartystreets/goconvey v1.8.1/go.mod h1:+/u4qLyY6x1jReYOp7GOM2FSt8aP9CzCZL03bI28W60= github.com/sourcegraph/conc v0.3.0 h1:OQTbbt6P72L20UqAkXXuLOj79LfEanQ+YQFNpLA9ySo= github.com/sourcegraph/conc v0.3.0/go.mod h1:Sdozi7LEKbFPqYX2/J+iBAM6HpqSLTASQIKqDmF7Mt0= github.com/spf13/afero v1.11.0 h1:WJQKhtpdm3v2IzqG8VMqrr6Rf3UYpEF239Jy9wNepM8= diff --git a/pkg/sdk/go/sdk.go b/pkg/sdk/go/sdk.go index 7341b99372..ab7ef502a4 100644 --- a/pkg/sdk/go/sdk.go +++ b/pkg/sdk/go/sdk.go @@ -9,6 +9,7 @@ import ( "encoding/json" "fmt" "io" + "log" "net/http" "net/url" "strconv" @@ -1154,8 +1155,6 @@ 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 { @@ -1172,7 +1171,6 @@ type mgSDK struct { msgContentType ContentType client *http.Client curlFlag bool - curlFlagChan chan string } // Config contains sdk configuration parameters. @@ -1190,7 +1188,6 @@ type Config struct { MsgContentType ContentType TLSVerification bool CurlFlag bool - CurlFlagChan chan string } // NewSDK returns new magistrala SDK instance. @@ -1214,8 +1211,7 @@ func NewSDK(conf Config) SDK { }, }, }, - curlFlag: conf.CurlFlag, - curlFlagChan: make(chan string), + curlFlag: conf.CurlFlag, } return sdk @@ -1223,7 +1219,7 @@ func NewSDK(conf Config) SDK { // processRequest creates and send a new HTTP request, and checks for errors in the HTTP response. // It then returns the response headers, the response body, and the associated error(s) (if any). -func (sdk mgSDK) processRequest(method, reqUrl, token string, data []byte, headers map[string]string, expectedRespCodes ...int) (http.Header, []byte, errors.SDKError) { +func (sdk *mgSDK) processRequest(method, reqUrl, token string, data []byte, headers map[string]string, expectedRespCodes ...int) (http.Header, []byte, errors.SDKError) { req, err := http.NewRequest(method, reqUrl, bytes.NewReader(data)) if err != nil { return make(http.Header), []byte{}, errors.NewSDKError(err) @@ -1249,8 +1245,7 @@ func (sdk mgSDK) processRequest(method, reqUrl, token string, data []byte, heade if err != nil { return nil, nil, errors.NewSDKError(err) } - sdk.curlFlagChan <- curlCommand.String() - return make(http.Header), []byte(curlCommand.String()), nil + log.Println(curlCommand.String()) } resp, err := sdk.client.Do(req) @@ -1272,11 +1267,6 @@ func (sdk mgSDK) processRequest(method, reqUrl, token string, data []byte, heade 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 { From 3eeabde131048ef0a2c30c16d4e49fa13794f02f Mon Sep 17 00:00:00 2001 From: JeffMboya Date: Mon, 10 Jun 2024 17:00:19 +0300 Subject: [PATCH 18/19] Feature: Add flag to print cURL commands Signed-off-by: JeffMboya --- cli/sdk.go | 4 +--- pkg/sdk/go/sdk.go | 6 ++---- pkg/sdk/mocks/sdk.go | 20 -------------------- 3 files changed, 3 insertions(+), 27 deletions(-) diff --git a/cli/sdk.go b/cli/sdk.go index 683e7bf3e4..9f7e273cb2 100644 --- a/cli/sdk.go +++ b/cli/sdk.go @@ -3,9 +3,7 @@ package cli -import ( - mgxsdk "github.com/absmach/magistrala/pkg/sdk/go" -) +import mgxsdk "github.com/absmach/magistrala/pkg/sdk/go" // Keep SDK handle in global var. var sdk mgxsdk.SDK diff --git a/pkg/sdk/go/sdk.go b/pkg/sdk/go/sdk.go index ab7ef502a4..3d2b50dc66 100644 --- a/pkg/sdk/go/sdk.go +++ b/pkg/sdk/go/sdk.go @@ -1192,7 +1192,7 @@ type Config struct { // NewSDK returns new magistrala SDK instance. func NewSDK(conf Config) SDK { - sdk := &mgSDK{ + return &mgSDK{ bootstrapURL: conf.BootstrapURL, certsURL: conf.CertsURL, httpAdapterURL: conf.HTTPAdapterURL, @@ -1213,13 +1213,11 @@ func NewSDK(conf Config) SDK { }, curlFlag: conf.CurlFlag, } - - return sdk } // processRequest creates and send a new HTTP request, and checks for errors in the HTTP response. // It then returns the response headers, the response body, and the associated error(s) (if any). -func (sdk *mgSDK) processRequest(method, reqUrl, token string, data []byte, headers map[string]string, expectedRespCodes ...int) (http.Header, []byte, errors.SDKError) { +func (sdk mgSDK) processRequest(method, reqUrl, token string, data []byte, headers map[string]string, expectedRespCodes ...int) (http.Header, []byte, errors.SDKError) { req, err := http.NewRequest(method, reqUrl, bytes.NewReader(data)) if err != nil { return make(http.Header), []byte{}, errors.NewSDKError(err) diff --git a/pkg/sdk/mocks/sdk.go b/pkg/sdk/mocks/sdk.go index b8a5cccbf6..9320502839 100644 --- a/pkg/sdk/mocks/sdk.go +++ b/pkg/sdk/mocks/sdk.go @@ -1176,26 +1176,6 @@ 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) From 6b7fc0de3a17b501b2653d7296b61616d953be60 Mon Sep 17 00:00:00 2001 From: JeffMboya Date: Tue, 11 Jun 2024 04:49:57 +0300 Subject: [PATCH 19/19] Feature: Add flag to print cURL commands Signed-off-by: JeffMboya --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index af87488b59..eadc28afa1 100644 --- a/Makefile +++ b/Makefile @@ -260,4 +260,4 @@ run_addons: check_certs $(foreach SVC,$(RUN_ADDON_ARGS),$(if $(filter $(SVC),$(ADDON_SERVICES) $(EXTERNAL_SERVICES)),,$(error Invalid Service $(SVC)))) @for SVC in $(RUN_ADDON_ARGS); do \ MG_ADDONS_CERTS_PATH_PREFIX="../." docker compose -f docker/addons/$$SVC/docker-compose.yml -p $(DOCKER_PROJECT) --env-file ./docker/.env $(DOCKER_COMPOSE_COMMAND) $(args) & \ - done \ No newline at end of file + done