Skip to content

Commit

Permalink
chore: adding configuration file env vars
Browse files Browse the repository at this point in the history
  • Loading branch information
spchortis committed Sep 26, 2023
1 parent 01837ed commit 02e5bb5
Showing 1 changed file with 58 additions and 1 deletion.
59 changes: 58 additions & 1 deletion src/services/k8s/knative.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ import (
"path"
"text/template"
"time"

"strings"
"bufio"
"os"

"github.com/charmbracelet/log"
"github.com/nearform/initium-cli/src/services/docker"
"github.com/nearform/initium-cli/src/services/project"
Expand Down Expand Up @@ -105,6 +108,60 @@ func Apply(namespace string, config *rest.Config, project *project.Project, dock
return fmt.Errorf("cannot create namespace %s, failed with %v", serviceManifest.ObjectMeta.Namespace, err)
}

if _, err := os.Stat(".env"); err != nil {
if os.IsNotExist(err) {
log.Info("No environment variables file (.env) to Load!")
} else {
log.Fatalf("Error checking .env file: %v", err)
}
} else {
log.Info("Environment variables file (.env) found! Loading..")
file, err := os.Open(".env")
if err != nil {
log.Fatalf("Error opening .env file: %v", err)
}
defer file.Close()

scanner := bufio.NewScanner(file)
envVariables := make(map[string]string)

checkFormat := func(line string) bool {
parts := strings.SplitN(line, "=", 2)
return len(parts) == 2
}

for scanner.Scan() {
line := scanner.Text()
if checkFormat(line) {
parts := strings.SplitN(line, "=", 2)
key := strings.TrimSpace(parts[0])
value := strings.TrimSpace(parts[1])
envVariables[key] = value
} else {
log.Warnf("Environment variables file (.env) line won't be processed due to invalid format: %s. Accepted: KEY=value", line)
}
}

if err := scanner.Err(); err != nil {
log.Errorf("Error reading environment variables file (.env): %v", err)
}

if len(envVariables) > 0 {
var envVarList []corev1.EnvVar
for key, value := range envVariables {
envVar := corev1.EnvVar{
Name: key,
Value: value,
}
envVarList = append(envVarList, envVar)
}
serviceManifest.Spec.Template.Spec.Containers[0].Env = append(serviceManifest.Spec.Template.Spec.Containers[0].Env, envVarList...)
log.Info("Environment variables file (.env) content is now loaded!")
} else {
log.Warnf("Environment file (.env) is empty, Nothing to load!")
}
}

service, err := servingClient.Services(serviceManifest.ObjectMeta.Namespace).Get(ctx, serviceManifest.ObjectMeta.Name, metav1.GetOptions{})
var deployedService *servingv1.Service
if err != nil {
Expand Down

0 comments on commit 02e5bb5

Please sign in to comment.