Skip to content

Commit

Permalink
added configuration via json config-file and logging
Browse files Browse the repository at this point in the history
  • Loading branch information
leroxyl committed Oct 18, 2019
1 parent 0176f0c commit a114fb4
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@

# IDEA
.idea/
config.json
29 changes: 29 additions & 0 deletions main/config.go
Original file line number Diff line number Diff line change
@@ -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")
}
20 changes: 16 additions & 4 deletions main/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit a114fb4

Please sign in to comment.