Skip to content

Commit

Permalink
Fixes to env read
Browse files Browse the repository at this point in the history
  • Loading branch information
chilaraiSxt committed May 6, 2024
1 parent df9934a commit 0e9bfd2
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 58 deletions.
73 changes: 18 additions & 55 deletions helpers/readenvironment.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,45 +3,23 @@ package helpers
import (
"log"
"os"
"path/filepath"

"github.com/joho/godotenv"
)

// Read User Id from Environment
func ReadUserId() (value string, ok bool){
err := godotenv.Load(filepath.Join("./", ".env"))
if err != nil {
value = os.Getenv("USERID")
if value == "" {
log.Fatal("USERID not set in environment")
}
} else {
value, ok = os.LookupEnv("USERID")

if !ok {
log.Fatal("USERID not set in environment")
}
value, ok = os.LookupEnv("USERID")
if !ok {
log.Fatal("USERID not set in environment")
}

return value, true
}

// Read Join Code from Environment
func ReadJoinCode() (value string, ok bool){

err := godotenv.Load(filepath.Join("./", ".env"))
if err != nil {
value = os.Getenv("JOINCODE")
if value == "" {
log.Println("JOINCODE not set in environment")
}
} else {
value, ok = os.LookupEnv("JOINCODE")

if !ok {
log.Println("JOINCODE not set in environment")
}
value, ok = os.LookupEnv("JOINCODE")
if !ok {
log.Println("JOINCODE not set in environment")
}

return value, true
Expand All @@ -50,45 +28,30 @@ func ReadJoinCode() (value string, ok bool){
// Read API End Point Discovery from Environment
func ReadEndPointDiscovery() (value string, ok bool){

err := godotenv.Load(filepath.Join("./", ".env"))
if err != nil {
value = os.Getenv("BASEURL_DISCOVERY")
if value == "" {
log.Fatal("Discovery BASEURL not set in environment")
}
} else {
value, ok = os.LookupEnv("BASEURL_DISCOVERY")

if !ok {
log.Fatal("Discovery BASEURL not set in environment")
}
value, ok = os.LookupEnv("BASEURL_DISCOVERY")
if !ok {
log.Fatal("Discovery BASEURL not set in environment")
}

return value, true
}

// Read API End Point Others in General from Environment
func ReadEndPointGeneral() (value string, ok bool){

err := godotenv.Load(filepath.Join("./", ".env"))
if err != nil {
value = os.Getenv("BASEURL_GENERAL")
if value == "" {
log.Fatal("General BASEURL not set in environment")
}
} else {
value, ok = os.LookupEnv("BASEURL_GENERAL")

if !ok {
log.Fatal("General BASEURL not set in environment")
}
value, ok = os.LookupEnv("BASEURL_GENERAL")
if !ok {
log.Fatal("General BASEURL not set in environment")
}

return value, true
}

// Read Scheme from Environment
func ReadScheme() (value string, ok bool){
value = "ed25519"
value, ok = os.LookupEnv("SCHEME")
if !ok {
log.Fatal("SCHEME not set in environment")
}

return value, true
}
17 changes: 14 additions & 3 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,20 @@ var pubKey ed25519.PublicKey

// Test Authentication
func TestAuthentication(t *testing.T){
userId = os.Getenv("TEST_USER")
privKeyB64 = os.Getenv("TEST_USER_PRIVKEY")
pubKeyB64 = os.Getenv("TEST_USER_PUBKEY")
userId, ok := os.LookupEnv("TEST_TRIAL_USERID")
if !ok {
t.Error("TEST_TRIAL_USERID not set in env")
}

privKeyB64, ok = os.LookupEnv("TEST_TRIAL_PRIVKEY")
if !ok {
t.Error("TEST_TRIAL_PRIVKEY not set in env")
}

pubKeyB64, ok = os.LookupEnv("TEST_TRIAL_PUBKEY")
if !ok {
t.Error("TEST_TRIAL_PUBKEY not set in env")
}

_, _, privKeyBytes, pubKeyBytes, err := utils.Authenticate(userId, pubKeyB64, privKeyB64)

Expand Down

0 comments on commit 0e9bfd2

Please sign in to comment.