Skip to content

Commit

Permalink
NOISSUE - Add Domain URL to CLI and SDK configuration (#2118)
Browse files Browse the repository at this point in the history
Signed-off-by: Arvindh <[email protected]>
  • Loading branch information
arvindh123 authored Mar 20, 2024
1 parent ec7b4b4 commit f733147
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 41 deletions.
79 changes: 62 additions & 17 deletions cli/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,28 @@ import (
"github.com/spf13/cobra"
)

const (
defURL string = "http://localhost"
defUsersURL string = defURL + ":9002"
defThingsURL string = defURL + ":9000"
defReaderURL string = defURL + ":9011"
defBootstrapURL string = defURL + ":9013"
defDomainsURL string = defURL + ":8189"
defCertsURL string = defURL + ":9019"
defInvitationsURL string = defURL + ":9020"
defHTTPURL string = defURL + ":9016/http"
defTLSVerification bool = false
defOffset string = "0"
defLimit string = "10"
defTopic string = ""
defRawOutput string = "false"
)

type remotes struct {
ThingsURL string `toml:"things_url"`
UsersURL string `toml:"users_url"`
ReaderURL string `toml:"reader_url"`
DomainsURL string `toml:"domains_url"`
HTTPAdapterURL string `toml:"http_adapter_url"`
BootstrapURL string `toml:"bootstrap_url"`
CertsURL string `toml:"certs_url"`
Expand Down Expand Up @@ -85,14 +103,21 @@ func ParseConfig(sdkConf mgxsdk.Config) (mgxsdk.Config, error) {
case os.IsNotExist(err):
defaultConfig := config{
Remotes: remotes{
ThingsURL: "http://localhost:9000",
UsersURL: "http://localhost:9002",
ReaderURL: "http://localhost",
HTTPAdapterURL: "http://localhost/http:9016",
BootstrapURL: "http://localhost",
CertsURL: "http://localhost:9019",
TLSVerification: false,
ThingsURL: defThingsURL,
UsersURL: defUsersURL,
ReaderURL: defReaderURL,
DomainsURL: defDomainsURL,
HTTPAdapterURL: defHTTPURL,
BootstrapURL: defBootstrapURL,
CertsURL: defCertsURL,
TLSVerification: defTLSVerification,
},
Filter: filter{
Offset: defOffset,
Limit: defLimit,
Topic: defTopic,
},
RawOutput: defRawOutput,
}
buf, err := toml.Marshal(defaultConfig)
if err != nil {
Expand All @@ -110,23 +135,23 @@ func ParseConfig(sdkConf mgxsdk.Config) (mgxsdk.Config, error) {
return sdkConf, err
}

if config.Filter.Offset != "" {
if config.Filter.Offset != "" && Offset == 0 {
offset, err := strconv.ParseUint(config.Filter.Offset, 10, 64)
if err != nil {
return sdkConf, err
}
Offset = offset
}

if config.Filter.Limit != "" {
if config.Filter.Limit != "" && Limit == 0 {
limit, err := strconv.ParseUint(config.Filter.Limit, 10, 64)
if err != nil {
return sdkConf, err
}
Limit = limit
}

if config.Filter.Topic != "" {
if config.Filter.Topic != "" && Topic == "" {
Topic = config.Filter.Topic
}

Expand All @@ -135,15 +160,35 @@ func ParseConfig(sdkConf mgxsdk.Config) (mgxsdk.Config, error) {
if err != nil {
return sdkConf, err
}
RawOutput = rawOutput
// check for config file value or flag input value is true
RawOutput = rawOutput || RawOutput
}

if sdkConf.ThingsURL == "" && config.Remotes.ThingsURL != "" {
sdkConf.ThingsURL = config.Remotes.ThingsURL
}

if sdkConf.UsersURL == "" && config.Remotes.UsersURL != "" {
sdkConf.UsersURL = config.Remotes.UsersURL
}

if sdkConf.ReaderURL == "" && config.Remotes.ReaderURL != "" {
sdkConf.ReaderURL = config.Remotes.ReaderURL
}

if sdkConf.HTTPAdapterURL == "" && config.Remotes.HTTPAdapterURL != "" {
sdkConf.HTTPAdapterURL = config.Remotes.HTTPAdapterURL
}

if sdkConf.BootstrapURL == "" && config.Remotes.BootstrapURL != "" {
sdkConf.BootstrapURL = config.Remotes.BootstrapURL
}

if sdkConf.CertsURL == "" && config.Remotes.CertsURL != "" {
sdkConf.CertsURL = config.Remotes.CertsURL
}

sdkConf.ThingsURL = config.Remotes.ThingsURL
sdkConf.UsersURL = config.Remotes.UsersURL
sdkConf.ReaderURL = config.Remotes.ReaderURL
sdkConf.HTTPAdapterURL = config.Remotes.HTTPAdapterURL
sdkConf.BootstrapURL = config.Remotes.BootstrapURL
sdkConf.CertsURL = config.Remotes.CertsURL
sdkConf.TLSVerification = config.Remotes.TLSVerification || sdkConf.TLSVerification

return sdkConf, nil
}
Expand Down
22 changes: 3 additions & 19 deletions cmd/cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
package main

import (
"fmt"
"log"

"github.com/absmach/magistrala/cli"
Expand All @@ -14,29 +13,14 @@ import (
)

const (
defURL string = "http://localhost"
defUsersURL string = defURL + ":9002"
defThingsURL string = defURL + ":9000"
defBootstrapURL string = defURL + ":9013"
defDomainsURL string = defURL + ":8189"
defCertsURL string = defURL + ":9019"
defInvitationsURL string = defURL + ":9020"
defURL string = "http://localhost"
)

func main() {
msgContentType := string(sdk.CTJSONSenML)
sdkConf := sdk.Config{
ThingsURL: defThingsURL,
UsersURL: defUsersURL,
ReaderURL: defURL,
HTTPAdapterURL: fmt.Sprintf("%s/http", defURL),
BootstrapURL: defBootstrapURL,
CertsURL: defCertsURL,
DomainsURL: defDomainsURL,
InvitationsURL: defInvitationsURL,
MsgContentType: sdk.ContentType(msgContentType),
TLSVerification: false,
HostURL: defURL,
MsgContentType: sdk.ContentType(msgContentType),
HostURL: defURL,
}

// Root
Expand Down
11 changes: 6 additions & 5 deletions config.toml
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
# Copyright (c) Abstract Machines
# SPDX-License-Identifier: Apache-2.0

raw_output = ""
raw_output = "false"
user_token = ""

[filter]
limit = ""
offset = ""
limit = "10"
offset = "0"
topic = ""

[remotes]
bootstrap_url = "http://localhost:9013"
certs_url = "http://localhost:9019"
http_adapter_url = "http://localhost/http"
reader_url = "http://localhost:9009"
domains_url = "http://localhost:8189"
http_adapter_url = "http://localhost:9016/http"
reader_url = "http://localhost:9011"
things_url = "http://localhost:9000"
tls_verification = false
users_url = "http://localhost:9002"

0 comments on commit f733147

Please sign in to comment.