Skip to content
Snippets Groups Projects
Commit 3b936e20 authored by Shreyas Prabhu's avatar Shreyas Prabhu
Browse files

feat: add .env support and improve credential handling with better error messages

parent 829faa4c
No related branches found
No related tags found
No related merge requests found
...@@ -3,7 +3,7 @@ ...@@ -3,7 +3,7 @@
"location": "asia-south1", "location": "asia-south1",
"default_conn_type": "GITLAB", "default_conn_type": "GITLAB",
"auth": { "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": [ "connections": [
{ {
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
}, },
"gitlab_config": { "gitlab_config": {
"host_uri": "https://gitlab.niveussolutions.com", "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", "read_token_version": "projects/460440866465/secrets/prajwalgitlabreadapi/versions/1",
"auth_token_version": "projects/460440866465/secrets/prajwalgitlabapi/versions/1" "auth_token_version": "projects/460440866465/secrets/prajwalgitlabapi/versions/1"
} }
......
...@@ -13,6 +13,7 @@ require ( ...@@ -13,6 +13,7 @@ require (
github.com/google/uuid v1.6.0 // indirect github.com/google/uuid v1.6.0 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.3.6 // indirect github.com/googleapis/enterprise-certificate-proxy v0.3.6 // indirect
github.com/googleapis/gax-go/v2 v2.14.1 // 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/auto/sdk v1.1.0 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.60.0 // indirect go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.60.0 // indirect
go.opentelemetry.io/otel v1.35.0 // indirect go.opentelemetry.io/otel v1.35.0 // indirect
......
...@@ -19,6 +19,8 @@ github.com/googleapis/enterprise-certificate-proxy v0.3.6 h1:GW/XbdyBFQ8Qe+YAmFU ...@@ -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/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 h1:hb0FFeiPaQskmvakKu5EbCbpntQn48jyHuvrkurSS/Q=
github.com/googleapis/gax-go/v2 v2.14.1/go.mod h1:Hb/NubMaVM88SrNkvl8X/o8XWwDJEPqouaLeN2IUxoA= 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 h1:cH53jehLUN6UFLY71z+NDOiNJqDdPRaXzTel0sJySYA=
go.opentelemetry.io/auto/sdk v1.1.0/go.mod h1:3wSPjt5PWp2RhlCcmmOial7AvC4DQqZb7a7wCow3W8A= 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= go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.60.0 h1:sbiXRNDSWJOTobXh5HyQKjq6wUC5tNybqjIqDpAY4CU=
......
...@@ -9,6 +9,7 @@ import ( ...@@ -9,6 +9,7 @@ import (
"strings" "strings"
"time" "time"
"github.com/joho/godotenv"
"github.com/niveus/cloud-build-go/cloudbuild" "github.com/niveus/cloud-build-go/cloudbuild"
"github.com/niveus/cloud-build-go/config" "github.com/niveus/cloud-build-go/config"
cloudbuildv1 "google.golang.org/api/cloudbuild/v1" cloudbuildv1 "google.golang.org/api/cloudbuild/v1"
...@@ -42,6 +43,14 @@ func formatTimestamp(id string) string { ...@@ -42,6 +43,14 @@ func formatTimestamp(id string) string {
} }
func main() { 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 // Define command-line flags
configPath := flag.String("config", "config/config.json", "Path to config file") 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") 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() { ...@@ -107,6 +116,7 @@ func main() {
if serviceAccountKeyFile := os.Getenv("GOOGLE_APPLICATION_CREDENTIALS"); serviceAccountKeyFile != "" { if serviceAccountKeyFile := os.Getenv("GOOGLE_APPLICATION_CREDENTIALS"); serviceAccountKeyFile != "" {
client, err = cloudbuild.NewClientWithCredentialsFile(ctx, serviceAccountKeyFile) client, err = cloudbuild.NewClientWithCredentialsFile(ctx, serviceAccountKeyFile)
} else { } else {
fmt.Printf("GOOGLE_APPLICATION_CREDENTIALS environment variable is not set. Falling back to default authentication methods.\n")
// Fall back to default authentication methods // Fall back to default authentication methods
client, err = cloudbuild.NewClient(ctx) client, err = cloudbuild.NewClient(ctx)
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment