Skip to content

Commit

Permalink
updated the logger
Browse files Browse the repository at this point in the history
Signed-off-by: nikhil2611 <nikhilgupta2102@gmail.com>
  • Loading branch information
nikhil2611 committed Jun 20, 2023
1 parent e217eab commit 5e73148
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 20 deletions.
14 changes: 5 additions & 9 deletions licensing/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package licensing

import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
Expand All @@ -27,14 +26,13 @@ func invokeGetAPI(opts map[string]string, URL string) {
params.Add("licenseId", opts["licenseId"])
params.Add("entitlementId", opts["entitlementId"])

// fmt.Println("params are :", params)
key, check := os.LookupEnv("CHEF_LICENSE_SERVER")
if check {
URL = key
}
res, err := http.Get(URL + "/" + CLIENT + "?" + params.Encode())
if err != nil {
fmt.Print(err.Error())
log.Fatal(err.Error())
}
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)
Expand All @@ -44,17 +42,15 @@ func invokeGetAPI(opts map[string]string, URL string) {

var response Response
if err := json.Unmarshal(body, &response); err != nil { // Parse []byte to go struct pointer
fmt.Println("Can not unmarshal JSON")
fmt.Println("err is", err)
log.Println("Can not unmarshal JSON")
log.Fatal(err)
}
if response.StatusCode != 200 {
fmt.Println("Error:", response.Message)
os.Exit(1)
log.Fatal(response.Message)
}
// fmt.Println("response is---", PrettyPrint(response))
if response.Data == false {
fmt.Println("Error:", response.Message)
os.Exit(1)
log.Fatal("Error:", response.Message)
}
}

Expand Down
15 changes: 4 additions & 11 deletions licensing/licensing.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package licensing

import (
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"strings"
Expand All @@ -27,29 +27,25 @@ func CheckSoftwareEntitlement(softwareEntitlementID string, URL string) {
licenseFilePath := filepath.Join(home, ".chef/licenses.yaml")
info, _ := os.Stat(licenseFilePath)
if info != nil {
fmt.Println("inside license fileeee")
licenseKey = licenseFileFetch(licenseFilePath)
client(licenseKey, softwareEntitlementID, URL)
return
}
key, check := os.LookupEnv("CHEF_LICENSE_KEY")
if check {
fmt.Println("inside ENV var")
licenseKey = append(licenseKey, key)
client(licenseKey, softwareEntitlementID, URL)
return
}
args := os.Args
for k, v := range args {
if v == "--chef-license-key" {
fmt.Println("inside arggg1")
if len(args) > k+1 {
licenseKey = append(licenseKey, args[k+1])
client(licenseKey, softwareEntitlementID, URL)
return
}
} else if strings.HasPrefix(v, "--chef-license-key=") {
fmt.Println("inside arggggg2")
split := strings.Split(v, "=")
licenseKey = append(licenseKey, split[len(split)-1])
client(licenseKey, softwareEntitlementID, URL)
Expand All @@ -62,14 +58,12 @@ func CheckSoftwareEntitlement(softwareEntitlementID string, URL string) {
func licenseFileFetch(licenseFilePath string) []string {
data, err := ioutil.ReadFile(licenseFilePath)
if err != nil {
fmt.Println(err)
return nil
log.Fatal(err)
}
var li Key
err = yaml.Unmarshal(data, &li)
if err != nil {
fmt.Println(err)
return nil
log.Fatal(err)
}
licenseKey := []string{}

Expand All @@ -83,8 +77,7 @@ func licenseFileFetch(licenseFilePath string) []string {

func client(licenseKey []string, softwareEntitlementID string, URL string) {
if len(licenseKey) == 0 {
fmt.Println("You dont have license key, Please generate by running chef license command")
os.Exit(0)
log.Fatal("You dont have license key, Please generate by running chef license command")
} else {
var opts = make(map[string]string)
opts["licenseId"] = strings.Join(licenseKey, ",")
Expand Down

0 comments on commit 5e73148

Please sign in to comment.