From a114fb4c971b958d78b45c8d4f5939dabb93d0bf Mon Sep 17 00:00:00 2001 From: Roxana Meixner Date: Fri, 18 Oct 2019 16:56:47 +0200 Subject: [PATCH] added configuration via json config-file and logging --- .gitignore | 1 + main/config.go | 29 +++++++++++++++++++++++++++++ main/main.go | 20 ++++++++++++++++---- 3 files changed, 46 insertions(+), 4 deletions(-) create mode 100644 main/config.go diff --git a/.gitignore b/.gitignore index 5fc4286..3261461 100644 --- a/.gitignore +++ b/.gitignore @@ -13,3 +13,4 @@ # IDEA .idea/ +config.json \ No newline at end of file diff --git a/main/config.go b/main/config.go new file mode 100644 index 0000000..6e22056 --- /dev/null +++ b/main/config.go @@ -0,0 +1,29 @@ +package main + +import ( + "encoding/json" + "io/ioutil" + "log" +) + +// configuration of the client +type Config struct { + Tenant string `json:"tenant"` + Password string `json:"password"` +} + +func (c *Config) Load(filename string) { + // read the configuration file + contextBytes, err := ioutil.ReadFile(filename) + if err != nil { + log.Fatalf("ERROR: unable to read configuration: %v", err) + } + + // parse json configuration + err = json.Unmarshal(contextBytes, c) + if err != nil { + log.Fatalf("ERROR: unable to parse configuration: %v", err) + } + + log.Printf("configuration found") +} diff --git a/main/main.go b/main/main.go index defe1b9..d8332fb 100644 --- a/main/main.go +++ b/main/main.go @@ -3,15 +3,27 @@ package main import ( "fmt" "github.com/ubirch/ubirch-go-c8y-client/c8y" + "log" ) +const ConfigFile = "config.json" + func main() { - tenant := "ubirch" - c8yPassword := "---" + log.Println("UBIRCH Golang Cumulocity client") + + // read configuration + conf := Config{} + conf.Load(ConfigFile) + + tenant := conf.Tenant + c8yPassword := conf.Password + + // bootstrap c8yAuth, err := c8y.C8yBootstrap(tenant, c8yPassword) if err != nil { - panic(fmt.Sprintf("unable to bootstrap device: %v", err)) + fmt.Printf(tenant + " : " + c8yPassword) + log.Fatalf("unable to bootstrap device: %v", err) } - fmt.Printf(c8yAuth) + fmt.Printf(tenant + " : " + c8yPassword + " : " + c8yAuth) }