Skip to content

Commit 901d194

Browse files
committed
Simplify config internals, and move out env load to main
1 parent de6e2af commit 901d194

17 files changed

Lines changed: 252 additions & 520 deletions

File tree

usage-examples/go/atlas-sdk-go/examples/billing/historical/main.go

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,30 +14,31 @@ import (
1414
"atlas-sdk-go/internal/billing"
1515
"atlas-sdk-go/internal/config"
1616
"atlas-sdk-go/internal/data/export"
17-
"atlas-sdk-go/internal/errors"
1817
"atlas-sdk-go/internal/fileutils"
1918

19+
"github.com/joho/godotenv"
2020
"go.mongodb.org/atlas-sdk/v20250219001/admin"
2121
)
2222

2323
func main() {
24-
configPath := "" // Use default config path for environment
25-
explicitEnv := "" // Use default environment
26-
secrets, cfg, err := config.LoadAll(configPath, explicitEnv)
24+
_ = godotenv.Load() // or godotenv.Load(".env.development")
25+
26+
ctx := context.Background()
27+
envName := config.Environment("test") // Cast string to config.Environment
28+
configPath := "configs/config.test.json" // Optional explicit config file path; if empty, uses environment-based path
29+
secrets, cfg, err := config.LoadAll(envName, configPath)
2730
if err != nil {
28-
errors.ExitWithError("Failed to load configuration", err)
31+
log.Fatalf("Failed to load configuration %v", err)
2932
}
3033

31-
client, err := auth.NewClient(cfg, secrets)
34+
client, err := auth.NewClient(ctx, &cfg, &secrets) // Pass pointers
3235
if err != nil {
33-
errors.ExitWithError("Failed to initialize authentication client", err)
36+
log.Fatalf("Failed to initialize authentication client: %v", err)
3437
}
3538

36-
ctx := context.Background()
3739
p := &admin.ListInvoicesApiParams{
3840
OrgId: cfg.OrgID,
3941
}
40-
4142
fmt.Printf("Fetching historical invoices for organization: %s\n", p.OrgId)
4243

4344
// Fetch invoices from the previous six months with the provided options
@@ -46,7 +47,7 @@ func main() {
4647
billing.WithIncludeCount(true),
4748
billing.WithDateRange(time.Now().AddDate(0, -6, 0), time.Now()))
4849
if err != nil {
49-
errors.ExitWithError("Failed to retrieve invoices", err)
50+
log.Fatalf("Failed to retrieve invoices: %v", err)
5051
}
5152

5253
if invoices.GetTotalCount() > 0 {
@@ -60,8 +61,14 @@ func main() {
6061
outDir := "invoices"
6162
prefix := fmt.Sprintf("historical_%s", p.OrgId)
6263

63-
exportInvoicesToJSON(invoices, outDir, prefix)
64-
exportInvoicesToCSV(invoices, outDir, prefix)
64+
err = exportInvoicesToJSON(invoices, outDir, prefix)
65+
if err != nil {
66+
log.Fatalf("Failed to export invoices to JSON: %v", err)
67+
}
68+
err = exportInvoicesToCSV(invoices, outDir, prefix)
69+
if err != nil {
70+
log.Fatalf("Failed to export invoices to CSV: %v", err)
71+
}
6572
// :remove-start:
6673
// Clean up (internal-only function)
6774
if err = fileutils.SafeDelete(outDir); err != nil {
@@ -71,21 +78,22 @@ func main() {
7178
// :remove-end:
7279
}
7380

74-
func exportInvoicesToJSON(invoices *admin.PaginatedApiInvoiceMetadata, outDir, prefix string) {
81+
func exportInvoicesToJSON(invoices *admin.PaginatedApiInvoiceMetadata, outDir, prefix string) error {
7582
jsonPath, err := fileutils.GenerateOutputPath(outDir, prefix, "json")
7683
if err != nil {
77-
errors.ExitWithError("Failed to generate JSON output path", err)
84+
return fmt.Errorf("failed to generate JSON output path: %v", err)
7885
}
7986
if err := export.ToJSON(invoices.GetResults(), jsonPath); err != nil {
80-
errors.ExitWithError("Failed to write JSON file", err)
87+
return fmt.Errorf("failed to write JSON file: %v", err)
8188
}
8289
fmt.Printf("Exported invoice data to %s\n", jsonPath)
90+
return nil
8391
}
8492

85-
func exportInvoicesToCSV(invoices *admin.PaginatedApiInvoiceMetadata, outDir, prefix string) {
93+
func exportInvoicesToCSV(invoices *admin.PaginatedApiInvoiceMetadata, outDir, prefix string) error {
8694
csvPath, err := fileutils.GenerateOutputPath(outDir, prefix, "csv")
8795
if err != nil {
88-
errors.ExitWithError("Failed to generate CSV output path", err)
96+
return fmt.Errorf("failed to generate CSV output path: %v", err)
8997
}
9098

9199
// Set the headers and mapped rows for the CSV export
@@ -99,10 +107,11 @@ func exportInvoicesToCSV(invoices *admin.PaginatedApiInvoiceMetadata, outDir, pr
99107
}
100108
})
101109
if err != nil {
102-
errors.ExitWithError("Failed to write CSV file", err)
110+
return fmt.Errorf("failed to write CSV file: %v", err)
103111
}
104112

105113
fmt.Printf("Exported invoice data to %s\n", csvPath)
114+
return nil
106115
}
107116

108117
// :snippet-end: [historical-billing]

usage-examples/go/atlas-sdk-go/examples/billing/line_items/main.go

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,31 +7,34 @@ package main
77
import (
88
"context"
99
"fmt"
10-
"go.mongodb.org/atlas-sdk/v20250219001/admin"
1110
"log"
1211

12+
"github.com/joho/godotenv"
13+
"go.mongodb.org/atlas-sdk/v20250219001/admin"
14+
1315
"atlas-sdk-go/internal/auth"
1416
"atlas-sdk-go/internal/billing"
1517
"atlas-sdk-go/internal/config"
1618
"atlas-sdk-go/internal/data/export"
17-
"atlas-sdk-go/internal/errors"
1819
"atlas-sdk-go/internal/fileutils"
1920
)
2021

2122
func main() {
22-
configPath := "" // Use default config path for environment
23-
explicitEnv := "" // Use default environment
24-
secrets, cfg, err := config.LoadAll(configPath, explicitEnv)
23+
_ = godotenv.Load() // or godotenv.Load(".env.development")
24+
25+
ctx := context.Background()
26+
envName := config.Environment("test") // Cast string to config.Environment
27+
configPath := "configs/config.test.json" // Optional explicit config file path; if empty, uses environment-based path
28+
secrets, cfg, err := config.LoadAll(envName, configPath)
2529
if err != nil {
26-
errors.ExitWithError("Failed to load configuration", err)
30+
log.Fatalf("Failed to load configuration %v", err)
2731
}
2832

29-
client, err := auth.NewClient(cfg, secrets)
33+
client, err := auth.NewClient(ctx, &cfg, &secrets) // Pass pointers
3034
if err != nil {
31-
errors.ExitWithError("Failed to initialize authentication client", err)
35+
log.Fatalf("Failed to initialize authentication client: %v", err)
3236
}
3337

34-
ctx := context.Background()
3538
p := &admin.ListInvoicesApiParams{
3639
OrgId: cfg.OrgID,
3740
}
@@ -40,7 +43,7 @@ func main() {
4043

4144
details, err := billing.CollectLineItemBillingData(ctx, client.InvoicesApi, client.OrganizationsApi, p.OrgId, nil)
4245
if err != nil {
43-
errors.ExitWithError(fmt.Sprintf("Failed to retrieve pending invoices for %s", p.OrgId), err)
46+
log.Fatalf("Failed to retrieve pending invoices for %s: %v", p.OrgId, err)
4447
}
4548

4649
if len(details) == 0 {
@@ -53,33 +56,40 @@ func main() {
5356
outDir := "invoices"
5457
prefix := fmt.Sprintf("pending_%s", p.OrgId)
5558

56-
exportInvoicesToJSON(details, outDir, prefix)
57-
exportInvoicesToCSV(details, outDir, prefix)
59+
err = exportInvoicesToJSON(details, outDir, prefix)
60+
if err != nil {
61+
log.Fatalf("Failed to export invoices to JSON: %v", err)
62+
}
63+
err = exportInvoicesToCSV(details, outDir, prefix)
64+
if err != nil {
65+
log.Fatalf("Failed to export invoices to CSV: %v", err)
66+
}
5867
// :remove-start:
5968
// Clean up (internal-only function)
60-
if err := fileutils.SafeDelete(outDir); err != nil {
69+
if err = fileutils.SafeDelete(outDir); err != nil {
6170
log.Printf("Cleanup error: %v", err)
6271
}
6372
fmt.Println("Deleted generated files from", outDir)
6473
// :remove-end:
6574
}
6675

67-
func exportInvoicesToJSON(details []billing.Detail, outDir, prefix string) {
76+
func exportInvoicesToJSON(details []billing.Detail, outDir, prefix string) error {
6877
jsonPath, err := fileutils.GenerateOutputPath(outDir, prefix, "json")
6978
if err != nil {
70-
errors.ExitWithError("Failed to generate JSON output path", err)
79+
return fmt.Errorf("failed to generate JSON output path: %v", err)
7180
}
7281

7382
if err := export.ToJSON(details, jsonPath); err != nil {
74-
errors.ExitWithError("Failed to write JSON file", err)
83+
return fmt.Errorf("failed to write JSON file: %v", err)
7584
}
7685
fmt.Printf("Exported billing data to %s\n", jsonPath)
86+
return nil
7787
}
7888

79-
func exportInvoicesToCSV(details []billing.Detail, outDir, prefix string) {
89+
func exportInvoicesToCSV(details []billing.Detail, outDir, prefix string) error {
8090
csvPath, err := fileutils.GenerateOutputPath(outDir, prefix, "csv")
8191
if err != nil {
82-
errors.ExitWithError("Failed to generate CSV output path", err)
92+
return fmt.Errorf("failed to generate CSV output path: %v", err)
8393
}
8494

8595
// Set the headers and mapped rows for the CSV export
@@ -101,9 +111,10 @@ func exportInvoicesToCSV(details []billing.Detail, outDir, prefix string) {
101111
}
102112
})
103113
if err != nil {
104-
errors.ExitWithError("Failed to write CSV file", err)
114+
return fmt.Errorf("failed to write CSV file: %v", err)
105115
}
106116
fmt.Printf("Exported billing data to %s\n", csvPath)
117+
return nil
107118
}
108119

109120
// :snippet-end: [line-items]

usage-examples/go/atlas-sdk-go/examples/billing/linked_orgs/main.go

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,32 @@ package main
77
import (
88
"context"
99
"fmt"
10+
"log"
1011

1112
"atlas-sdk-go/internal/auth"
1213
"atlas-sdk-go/internal/billing"
1314
"atlas-sdk-go/internal/config"
14-
"atlas-sdk-go/internal/errors"
1515

16+
"github.com/joho/godotenv"
1617
"go.mongodb.org/atlas-sdk/v20250219001/admin"
1718
)
1819

1920
func main() {
20-
configPath := "" // Use default config path for environment
21-
explicitEnv := "" // Use default environment
22-
secrets, cfg, err := config.LoadAll(configPath, explicitEnv)
21+
_ = godotenv.Load() // or godotenv.Load(".env.development")
22+
23+
ctx := context.Background()
24+
envName := config.Environment("test") // Cast string to config.Environment
25+
configPath := "configs/config.test.json" // Optional explicit config file path; if empty, uses environment-based path
26+
secrets, cfg, err := config.LoadAll(envName, configPath)
2327
if err != nil {
24-
errors.ExitWithError("Failed to load configuration", err)
28+
log.Fatalf("Failed to load configuration %v", err)
2529
}
2630

27-
client, err := auth.NewClient(cfg, secrets)
31+
client, err := auth.NewClient(ctx, &cfg, &secrets) // Pass pointers
2832
if err != nil {
29-
errors.ExitWithError("Failed to initialize authentication client", err)
33+
log.Fatalf("Failed to initialize authentication client: %v", err)
3034
}
3135

32-
ctx := context.Background()
3336
p := &admin.ListInvoicesApiParams{
3437
OrgId: cfg.OrgID,
3538
}
@@ -38,7 +41,7 @@ func main() {
3841

3942
invoices, err := billing.GetCrossOrgBilling(ctx, client.InvoicesApi, p)
4043
if err != nil {
41-
errors.ExitWithError(fmt.Sprintf("Failed to retrieve cross-organization billing data for %s", p.OrgId), err)
44+
log.Fatalf("Failed to retrieve cross-organization billing data for %s: %v", p.OrgId, err)
4245
}
4346

4447
displayLinkedOrganizations(invoices, p.OrgId)

usage-examples/go/atlas-sdk-go/examples/monitoring/logs/main.go

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,28 +11,29 @@ import (
1111

1212
"atlas-sdk-go/internal/auth"
1313
"atlas-sdk-go/internal/config"
14-
"atlas-sdk-go/internal/errors"
1514
"atlas-sdk-go/internal/fileutils"
1615
"atlas-sdk-go/internal/logs"
1716

17+
"github.com/joho/godotenv"
1818
"go.mongodb.org/atlas-sdk/v20250219001/admin"
1919
)
2020

2121
func main() {
22-
configPath := "" // Use default config path for environment
23-
explicitEnv := "" // Use default environment
24-
secrets, cfg, err := config.LoadAll(configPath, explicitEnv)
22+
_ = godotenv.Load() // or godotenv.Load(".env.development")
23+
24+
ctx := context.Background()
25+
envName := config.Environment("test") // Cast string to config.Environment
26+
configPath := "configs/config.test.json" // Optional explicit config file path; if empty, uses environment-based path
27+
secrets, cfg, err := config.LoadAll(envName, configPath)
2528
if err != nil {
26-
errors.ExitWithError("Failed to load configuration", err)
29+
log.Fatalf("Failed to load configuration %v", err)
2730
}
2831

29-
client, err := auth.NewClient(cfg, secrets)
32+
client, err := auth.NewClient(ctx, &cfg, &secrets) // Pass pointers
3033
if err != nil {
31-
errors.ExitWithError("Failed to initialize authentication client", err)
34+
log.Fatalf("Failed to initialize authentication client: %v", err)
3235
}
3336

34-
ctx := context.Background()
35-
3637
// Fetch logs with the provided parameters
3738
p := &admin.GetHostLogsApiParams{
3839
GroupId: cfg.ProjectID,
@@ -43,7 +44,7 @@ func main() {
4344
cfg.ProjectID, cfg.HostName, p.LogName)
4445
rc, err := logs.FetchHostLogs(ctx, client.MonitoringAndLogsApi, p)
4546
if err != nil {
46-
errors.ExitWithError("Failed to fetch logs", err)
47+
log.Fatalf("Failed to fetch logs: %v", err)
4748
}
4849
defer fileutils.SafeClose(rc)
4950

@@ -53,22 +54,22 @@ func main() {
5354
prefix := fmt.Sprintf("%s_%s", p.HostName, p.LogName)
5455
gzPath, err := fileutils.GenerateOutputPath(outDir, prefix, "gz")
5556
if err != nil {
56-
errors.ExitWithError("Failed to generate GZ output path", err)
57+
log.Fatalf("Failed to generate GZ output path: %v", err)
5758
}
5859
txtPath, err := fileutils.GenerateOutputPath(outDir, prefix, "txt")
5960
if err != nil {
60-
errors.ExitWithError("Failed to generate TXT output path", err)
61+
log.Fatalf("Failed to generate TXT output path: %v", err)
6162
}
6263

6364
// Save compressed logs
6465
if err := fileutils.WriteToFile(rc, gzPath); err != nil {
65-
errors.ExitWithError("Failed to save compressed logs", err)
66+
log.Fatalf("Failed to save compressed logs: %v", err)
6667
}
6768
fmt.Println("Saved compressed log to", gzPath)
6869

6970
// Decompress logs
7071
if err := fileutils.DecompressGzip(gzPath, txtPath); err != nil {
71-
errors.ExitWithError("Failed to decompress logs", err)
72+
log.Fatalf("Failed to decompress logs: %v", err)
7273
}
7374
fmt.Println("Uncompressed log to", txtPath)
7475
// :remove-start:

0 commit comments

Comments
 (0)