Skip to content
This repository has been archived by the owner on May 24, 2024. It is now read-only.

Commit

Permalink
Config handling
Browse files Browse the repository at this point in the history
  • Loading branch information
dkrizic committed Apr 30, 2024
1 parent c0e3c0f commit c413bc5
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 3 deletions.
115 changes: 115 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package config

import (
"errors"
"flag"
"log"
"log/slog"
"os"
"strconv"
)

const (
keyGithubEnterprise = "github-enterprise"
keyGithubToken = "github-token"
keyAzureClientId = "azure-client-id"
keyAzureClientSecret = "azure-client-secret"
keyAzureTenantId = "azure-tenant-id"
keyAzureGroup = "azure-group"
keyDryRun = "dry-run"

keyGitHubEnterpriseEnvironment = "GITHUB_ENTERPRISE"
keyGitHubTokenEnvironment = "GITHUB-TOKEN"
keyAzureClientIdEnvironment = "AZURE_CLIENT_ID"
keyAzureClientSecretEnvironment = "AZURE_CLIENT_SECRET"
keyAzureTenantIdEnvironment = "AZURE_TENANT_ID"
keyAzureGroupEnvironment = "AZURE_GROUP"
keyDryRunEnvironment = "DRY_RUN"
)

type GitHub struct {
Enterprise string
Token string
}

type Azure struct {
ClientId string
ClientSecret string
TenantId string
Group string
}

type Config struct {
GitHub GitHub
Azure Azure
DryRun bool
}

func New() (*Config, error) {
c := Config{}
flag.StringVar(&c.GitHub.Token, keyGithubToken, lookupEnvOrString(keyGitHubTokenEnvironment, ""), "The GitHub Token to use for authentication.")
flag.StringVar(&c.GitHub.Enterprise, keyGithubEnterprise, lookupEnvOrString(keyGitHubEnterpriseEnvironment, ""), "The GitHub Enterprise to query for repositories.")
flag.StringVar(&c.Azure.ClientId, keyAzureClientId, lookupEnvOrString(keyAzureClientIdEnvironment, ""), "The Azure Client ID.")
flag.StringVar(&c.Azure.ClientSecret, keyAzureClientSecret, lookupEnvOrString(keyAzureClientSecretEnvironment, ""), "The Azure Client Secret.")
flag.StringVar(&c.Azure.TenantId, keyAzureTenantId, lookupEnvOrString(keyAzureTenantIdEnvironment, ""), "The Azure Tenant ID.")
flag.StringVar(&c.Azure.Group, keyAzureGroup, lookupEnvOrString(keyAzureGroupEnvironment, ""), "The Azure Group.")
flag.BoolVar(&c.DryRun, keyDryRun, lookupEnvOrBool(keyDryRunEnvironment, false), "Dry run mode.")

flag.Parse()

if c.GitHub.Token == "" {
slog.Error("GitHub Token is required")
return nil, errors.New("GitHub Token is required")
}
if c.GitHub.Enterprise == "" {
slog.Error("GitHub Enterprise is required")
return nil, errors.New("GitHub Enterprise is required")
}
if c.Azure.ClientId == "" {
slog.Error("Azure Client ID is required")
return nil, errors.New("Azure Client ID is required")
}
if c.Azure.ClientSecret == "" {
slog.Error("Azure Client Secret is required")
return nil, errors.New("Azure Client Secret is required")
}
if c.Azure.TenantId == "" {
slog.Error("Azure Tenant ID is required")
return nil, errors.New("Azure Tenant ID is required")
}
if c.Azure.Group == "" {
slog.Error("Azure Group is required")
return nil, errors.New("Azure Group is required")
}

return &c, nil
}

func lookupEnvOrString(key string, defaultVal string) string {
if val, ok := os.LookupEnv(key); ok {
return val
}
return defaultVal
}

func lookupEnvOrInt(key string, defaultVal int) int {
if val, ok := os.LookupEnv(key); ok {
v, err := strconv.Atoi(val)
if err != nil {
log.Fatalf("LookupEnvOrInt[%s]: %v", key, err)
}
return v
}
return defaultVal
}

func lookupEnvOrBool(key string, defaultVal bool) bool {
if val, ok := os.LookupEnv(key); ok {
v, err := strconv.ParseBool(val)
if err != nil {
log.Fatalf("LookupEnvOrBool[%s]: %v", key, err)
}
return v
}
return defaultVal
}
19 changes: 16 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,23 @@
package main

import (
"fmt"
"github.com/prodyna/delete-from-enterprise/meta"
"github.com/prodyna/delete-from-enterprise/config"
"log/slog"
"os"
)

func main() {
fmt.Printf("Hello, World! %s\n", meta.Version)
c, err := config.New()
if err != nil {
slog.Error("Unable to create config", "error", err)
os.Exit(1)
}
slog.Info("Configuration",
"githubEnterprise", c.GitHub.Enterprise,
"githubToken", "***",
"azureClientId", c.Azure.ClientId,
"azureClientSecret", "***",
"azureTenantId", c.Azure.TenantId,
"azureGroup", c.Azure.Group,
"dryRun", c.DryRun)
}

0 comments on commit c413bc5

Please sign in to comment.