Debian Codegraph @

examples/04-custom-paths.go

raw
1 //go:build examples
2 // +build examples
3
4 package main
5
6 import (
7 "fmt"
8 "log"
9 "os"
10 "path/filepath"
11
12 "github.com/gopasspw/gitconfig"
13 )
14
15 // Example 4: Custom Paths
16 //
17 // This example demonstrates how to work with configuration files
18 // at custom locations instead of using the default Git paths.
19 // This is useful when:
20 // - Working with non-standard directory structures
21 // - Testing with temporary configs
22 // - Integrating with systems that use similar config formats
23 func main() {
24 // Create temporary directory for this example
25 tmpDir, err := os.MkdirTemp("", "gitconfig-example-")
26 if err != nil {
27 log.Fatal(err)
28 }
29 defer os.RemoveAll(tmpDir)
30
31 fmt.Println("=== Example 4: Custom Paths ===\n")
32
33 // Example 1: Load config from custom location
34 customPath1 := filepath.Join(tmpDir, "my-config")
35 err = os.WriteFile(customPath1, []byte(`[app]
36 name = MyApp
37 version = 1.0
38 [features]
39 logging = true
40 debug = false
41 `), 0o644)
42 if err != nil {
43 log.Fatal(err)
44 }
45
46 fmt.Println("Loading config from custom path:")
47 fmt.Printf(" Path: %s\n", customPath1)
48
49 cfg1, err := gitconfig.LoadConfig(customPath1)
50 if err != nil {
51 log.Fatal(err)
52 }
53
54 fmt.Println(" Values:")
55 if name, ok := cfg1.Get("app.name"); ok {
56 fmt.Printf(" app.name = %s\n", name)
57 }
58 if version, ok := cfg1.Get("app.version"); ok {
59 fmt.Printf(" app.version = %s\n", version)
60 }
61
62 // Example 2: Multiple custom config files
63 fmt.Println("\nWorking with multiple custom configs:")
64
65 configA := filepath.Join(tmpDir, "config-a")
66 configB := filepath.Join(tmpDir, "config-b")
67
68 err = os.WriteFile(configA, []byte(`[database]
69 host = localhost
70 port = 5432
71 `), 0o644)
72 if err != nil {
73 log.Fatal(err)
74 }
75
76 err = os.WriteFile(configB, []byte(`[database]
77 user = admin
78 password = secret
79 [cache]
80 enabled = true
81 `), 0o644)
82 if err != nil {
83 log.Fatal(err)
84 }
85
86 cfgA, err := gitconfig.LoadConfig(configA)
87 if err != nil {
88 log.Fatal(err)
89 }
90
91 cfgB, err := gitconfig.LoadConfig(configB)
92 if err != nil {
93 log.Fatal(err)
94 }
95
96 fmt.Println(" Config A (database.host):")
97 if host, ok := cfgA.Get("database.host"); ok {
98 fmt.Printf(" %s\n", host)
99 }
100
101 fmt.Println(" Config B (database.user):")
102 if user, ok := cfgB.Get("database.user"); ok {
103 fmt.Printf(" %s\n", user)
104 }
105
106 fmt.Println(" Config B (cache.enabled):")
107 if enabled, ok := cfgB.Get("cache.enabled"); ok {
108 fmt.Printf(" %s\n", enabled)
109 }
110
111 // Example 3: Creating and modifying custom config
112 fmt.Println("\nCreating and modifying custom config:")
113
114 customPath2 := filepath.Join(tmpDir, "new-config")
115 fmt.Printf(" Creating new config at: %s\n", customPath2)
116
117 // Create empty config file
118 if err := os.WriteFile(customPath2, []byte(""), 0o644); err != nil {
119 log.Fatal(err)
120 }
121
122 // Load empty config
123 cfg3, err := gitconfig.LoadConfig(customPath2)
124 if err != nil {
125 log.Fatal(err)
126 }
127
128 // Add values
129 if err := cfg3.Set("app.name", "NewApp"); err != nil {
130 log.Fatal(err)
131 }
132 if err := cfg3.Set("app.version", "2.0"); err != nil {
133 log.Fatal(err)
134 }
135 if err := cfg3.Set("app.environment", "production"); err != nil {
136 log.Fatal(err)
137 }
138
139 fmt.Println(" Config written successfully!")
140
141 // Verify by loading it back
142 cfg3Reloaded, err := gitconfig.LoadConfig(customPath2)
143 if err != nil {
144 log.Fatal(err)
145 }
146
147 fmt.Println(" Verifying persisted values:")
148 if name, ok := cfg3Reloaded.Get("app.name"); ok {
149 fmt.Printf(" app.name = %s\n", name)
150 }
151 if ver, ok := cfg3Reloaded.Get("app.version"); ok {
152 fmt.Printf(" app.version = %s\n", ver)
153 }
154 if env, ok := cfg3Reloaded.Get("app.environment"); ok {
155 fmt.Printf(" app.environment = %s\n", env)
156 }
157
158 fmt.Println("\n=== Summary ===")
159 fmt.Println("LoadConfig() accepts any file path as an argument.")
160 fmt.Println("This allows using gitconfig for non-Git applications.")
161 fmt.Println("Useful for config files with git-config format.")
162 }