From 3b936e20ec81f43535feb4198e1d6c920cb3f085 Mon Sep 17 00:00:00 2001
From: Shreyas Prabhu <shreyasprabhu26@gmail.com>
Date: Fri, 23 May 2025 14:40:57 +0530
Subject: [PATCH] feat: add .env support and improve credential handling with
 better error messages

---
 config/config.json |  3 ++-
 go.mod             |  1 +
 go.sum             |  2 ++
 main.go            | 12 +++++++++++-
 4 files changed, 16 insertions(+), 2 deletions(-)

diff --git a/config/config.json b/config/config.json
index 30b8aee..2c6608a 100644
--- a/config/config.json
+++ b/config/config.json
@@ -3,7 +3,7 @@
   "location": "asia-south1",
   "default_conn_type": "GITLAB",
   "auth": {
-    "credentials_file": "/home/prajwal/Projects/cloud build deployment/cloud-run-go/tech-rnd-project-2e698053b09e.json"
+    "credentials_file": "/home/niveus/Documents/projects/cloud-build-go/tech-rnd-project-2e698053b09e.json"
   },
   "connections": [
     {
@@ -15,6 +15,7 @@
       },
       "gitlab_config": {
         "host_uri": "https://gitlab.niveussolutions.com",
+        "webhook_secret_version": "projects/460440866465/secrets/poc-webhooksecreat/versions/1",
         "read_token_version": "projects/460440866465/secrets/prajwalgitlabreadapi/versions/1",
         "auth_token_version": "projects/460440866465/secrets/prajwalgitlabapi/versions/1"
       }
diff --git a/go.mod b/go.mod
index d2eeffc..af41530 100644
--- a/go.mod
+++ b/go.mod
@@ -13,6 +13,7 @@ require (
 	github.com/google/uuid v1.6.0 // indirect
 	github.com/googleapis/enterprise-certificate-proxy v0.3.6 // indirect
 	github.com/googleapis/gax-go/v2 v2.14.1 // indirect
+	github.com/joho/godotenv v1.5.1 // indirect
 	go.opentelemetry.io/auto/sdk v1.1.0 // indirect
 	go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.60.0 // indirect
 	go.opentelemetry.io/otel v1.35.0 // indirect
diff --git a/go.sum b/go.sum
index 2f7b4cd..5906cef 100644
--- a/go.sum
+++ b/go.sum
@@ -19,6 +19,8 @@ github.com/googleapis/enterprise-certificate-proxy v0.3.6 h1:GW/XbdyBFQ8Qe+YAmFU
 github.com/googleapis/enterprise-certificate-proxy v0.3.6/go.mod h1:MkHOF77EYAE7qfSuSS9PU6g4Nt4e11cnsDUowfwewLA=
 github.com/googleapis/gax-go/v2 v2.14.1 h1:hb0FFeiPaQskmvakKu5EbCbpntQn48jyHuvrkurSS/Q=
 github.com/googleapis/gax-go/v2 v2.14.1/go.mod h1:Hb/NubMaVM88SrNkvl8X/o8XWwDJEPqouaLeN2IUxoA=
+github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
+github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
 go.opentelemetry.io/auto/sdk v1.1.0 h1:cH53jehLUN6UFLY71z+NDOiNJqDdPRaXzTel0sJySYA=
 go.opentelemetry.io/auto/sdk v1.1.0/go.mod h1:3wSPjt5PWp2RhlCcmmOial7AvC4DQqZb7a7wCow3W8A=
 go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.60.0 h1:sbiXRNDSWJOTobXh5HyQKjq6wUC5tNybqjIqDpAY4CU=
diff --git a/main.go b/main.go
index 2503ede..eec15fd 100644
--- a/main.go
+++ b/main.go
@@ -9,6 +9,7 @@ import (
 	"strings"
 	"time"
 
+	"github.com/joho/godotenv"
 	"github.com/niveus/cloud-build-go/cloudbuild"
 	"github.com/niveus/cloud-build-go/config"
 	cloudbuildv1 "google.golang.org/api/cloudbuild/v1"
@@ -42,6 +43,14 @@ func formatTimestamp(id string) string {
 }
 
 func main() {
+	// Load environment variables from .env file
+	if err := godotenv.Load(); err != nil {
+		log.Printf("Warning: Error loading .env file: %v", err)
+		log.Println("Using environment variables from system instead")
+	} else {
+		log.Println("Successfully loaded environment variables from .env file")
+	}
+
 	// Define command-line flags
 	configPath := flag.String("config", "config/config.json", "Path to config file")
 	action := flag.String("action", "list-connections", "Action to perform: list-connections, create-gitlab, delete-connection, list-linkable-repos, inspect, list-repositories, list-repositories-for-triggers, create-trigger, list-triggers")
@@ -107,6 +116,7 @@ func main() {
 	if serviceAccountKeyFile := os.Getenv("GOOGLE_APPLICATION_CREDENTIALS"); serviceAccountKeyFile != "" {
 		client, err = cloudbuild.NewClientWithCredentialsFile(ctx, serviceAccountKeyFile)
 	} else {
+		fmt.Printf("GOOGLE_APPLICATION_CREDENTIALS environment variable is not set. Falling back to default authentication methods.\n")
 		// Fall back to default authentication methods
 		client, err = cloudbuild.NewClient(ctx)
 	}
@@ -426,7 +436,7 @@ func main() {
 		repoNameForTrigger := *repositoryName
 		branchPatternValue := *branchPattern
 		buildConfigPathValue := *buildConfigPath
-		repoTypeValue := connTypeValue 
+		repoTypeValue := connTypeValue
 		cloudbuild_repo_name := *cloudbuild_repo_name
 
 		if *triggerConfigPath != "" {
-- 
GitLab