-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path02-write-persist.go
More file actions
105 lines (88 loc) · 2.59 KB
/
02-write-persist.go
File metadata and controls
105 lines (88 loc) · 2.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
//go:build examples
// +build examples
package main
import (
"fmt"
"log"
"os"
"path/filepath"
"github.com/gopasspw/gitconfig"
)
// Example 2: Write and Persist
//
// This example demonstrates how to modify configuration values and persist
// those changes back to the config file.
// It shows:
// - Setting configuration values
// - Persisting changes to disk
// - Verifying changes
func main() {
// Create a temporary git config file for this example
tmpDir, err := os.MkdirTemp("", "gitconfig-example-")
if err != nil {
log.Fatal(err)
}
defer os.RemoveAll(tmpDir)
configPath := filepath.Join(tmpDir, ".git", "config")
if err := os.MkdirAll(filepath.Dir(configPath), 0o755); err != nil {
log.Fatal(err)
}
// Write initial config
initialConfig := `[user]
name = John Doe
[core]
editor = vim
`
err = os.WriteFile(configPath, []byte(initialConfig), 0o644)
if err != nil {
log.Fatal(err)
}
fmt.Println("=== Example 2: Write and Persist ===\n")
// Load the config file
cfg, err := gitconfig.LoadConfig(configPath)
if err != nil {
log.Fatal(err)
}
fmt.Println("Initial config:")
printValues(cfg, []string{"user.name", "user.email", "core.editor", "core.pager"})
// Modify values
fmt.Println("\nModifying configuration...")
if err := cfg.Set("user.email", "john.doe@example.com"); err != nil {
log.Fatal(err)
}
if err := cfg.Set("core.pager", "less -R"); err != nil {
log.Fatal(err)
}
if err := cfg.Set("core.autocrlf", "false"); err != nil {
log.Fatal(err)
}
fmt.Println("After modifications (in memory):")
printValues(cfg, []string{"user.name", "user.email", "core.editor", "core.pager", "core.autocrlf"})
// Set automatically persists to disk when the config has a path
fmt.Println("\nChanges persisted to disk.")
// Reload from disk to verify
fmt.Println("\nReloading config from disk...")
cfg2, err := gitconfig.LoadConfig(configPath)
if err != nil {
log.Fatal(err)
}
fmt.Println("Config after reload (verifying persistence):")
printValues(cfg2, []string{"user.name", "user.email", "core.editor", "core.pager", "core.autocrlf"})
// Print the actual file contents
fmt.Println("\nActual file contents:")
content, err := os.ReadFile(configPath)
if err != nil {
log.Fatal(err)
}
fmt.Println(string(content))
fmt.Println("=== Summary ===")
fmt.Println("Configuration values can be modified and persisted using Set().")
fmt.Println("The library preserves formatting of the original file.")
}
func printValues(cfg *gitconfig.Config, keys []string) {
for _, key := range keys {
if value, ok := cfg.Get(key); ok {
fmt.Printf(" %s = %s\n", key, value)
}
}
}