| 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 6: Include Files |
| 16 |
// |
| 17 |
// This example demonstrates how gitconfig handles include directives. |
| 18 |
// Git config supports including other config files: |
| 19 |
// |
| 20 |
// [include] |
| 21 |
// path = /path/to/other/config |
| 22 |
// |
| 23 |
// This enables modular configuration and code reuse. |
| 24 |
func main() { |
| 25 |
// Create temporary directory |
| 26 |
tmpDir, err := os.MkdirTemp("", "gitconfig-example-") |
| 27 |
if err != nil { |
| 28 |
log.Fatal(err) |
| 29 |
} |
| 30 |
defer os.RemoveAll(tmpDir) |
| 31 |
|
| 32 |
fmt.Println("=== Example 6: Include Files ===\n") |
| 33 |
|
| 34 |
// Create included config files |
| 35 |
commonPath := filepath.Join(tmpDir, "config-common") |
| 36 |
projectPath := filepath.Join(tmpDir, "config-project") |
| 37 |
mainPath := filepath.Join(tmpDir, "config-main") |
| 38 |
|
| 39 |
// Common settings (included by all projects) |
| 40 |
err = os.WriteFile(commonPath, []byte(`[core] |
| 41 |
# Common settings used across all projects |
| 42 |
sshCommand = ssh -i ~/.ssh/id_ed25519 |
| 43 |
autocrlf = input |
| 44 |
[init] |
| 45 |
defaultBranch = main |
| 46 |
`), 0o644) |
| 47 |
if err != nil { |
| 48 |
log.Fatal(err) |
| 49 |
} |
| 50 |
|
| 51 |
// Project-specific settings (included by main config) |
| 52 |
err = os.WriteFile(projectPath, []byte(`[user] |
| 53 |
name = Project Team |
| 54 |
email = [email protected] |
| 55 |
[feature] |
| 56 |
enabled = true |
| 57 |
`), 0o644) |
| 58 |
if err != nil { |
| 59 |
log.Fatal(err) |
| 60 |
} |
| 61 |
|
| 62 |
// Main configuration that includes others |
| 63 |
err = os.WriteFile(mainPath, []byte(fmt.Sprintf(`# Main project config |
| 64 |
[include] |
| 65 |
path = %s |
| 66 |
path = %s |
| 67 |
[user] |
| 68 |
signingkey = ~/.ssh/id_ed25519.pub |
| 69 |
[commit] |
| 70 |
gpgsign = true |
| 71 |
`, commonPath, projectPath)), 0o644) |
| 72 |
if err != nil { |
| 73 |
log.Fatal(err) |
| 74 |
} |
| 75 |
|
| 76 |
// Load the main config (which includes others) |
| 77 |
cfg, err := gitconfig.LoadConfig(mainPath) |
| 78 |
if err != nil { |
| 79 |
log.Fatal(err) |
| 80 |
} |
| 81 |
|
| 82 |
fmt.Println("Configuration structure:") |
| 83 |
fmt.Printf(" Main config: %s\n", mainPath) |
| 84 |
fmt.Printf(" Includes: %s\n", commonPath) |
| 85 |
fmt.Printf(" %s\n", projectPath) |
| 86 |
|
| 87 |
fmt.Println("\nResolved configuration values:") |
| 88 |
|
| 89 |
// Values from common config (included) |
| 90 |
values := []string{ |
| 91 |
"core.sshCommand", |
| 92 |
"core.autocrlf", |
| 93 |
"init.defaultBranch", |
| 94 |
} |
| 95 |
|
| 96 |
for _, key := range values { |
| 97 |
if v, ok := cfg.Get(key); ok { |
| 98 |
fmt.Printf(" %s = %s (from common config)\n", key, v) |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
// Values from project config (included) |
| 103 |
values = []string{ |
| 104 |
"user.name", |
| 105 |
"user.email", |
| 106 |
"feature.enabled", |
| 107 |
} |
| 108 |
|
| 109 |
for _, key := range values { |
| 110 |
if v, ok := cfg.Get(key); ok { |
| 111 |
fmt.Printf(" %s = %s (from project config)\n", key, v) |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
// Values from main config itself |
| 116 |
values = []string{ |
| 117 |
"user.signingkey", |
| 118 |
"commit.gpgsign", |
| 119 |
} |
| 120 |
|
| 121 |
for _, key := range values { |
| 122 |
if v, ok := cfg.Get(key); ok { |
| 123 |
fmt.Printf(" %s = %s (from main config)\n", key, v) |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
fmt.Println("\n=== Include Path Resolution ===") |
| 128 |
fmt.Println("Include paths are relative to the config file location.") |
| 129 |
fmt.Println("Absolute paths are also supported.") |
| 130 |
fmt.Println("Included files can themselves include other files.") |
| 131 |
fmt.Println("Later includes override earlier values (last-write-wins).") |
| 132 |
|
| 133 |
fmt.Println("\n=== Real-world Example ===") |
| 134 |
fmt.Println("Typical Git config organization:") |
| 135 |
fmt.Println(" ~/.gitconfig # Global user config") |
| 136 |
fmt.Println(" ~/.gitconfig-work # Work-specific settings") |
| 137 |
fmt.Println(" ~/.gitconfig-personal # Personal project settings") |
| 138 |
fmt.Println("") |
| 139 |
fmt.Println("With entries in ~/.gitconfig:") |
| 140 |
fmt.Println(" [include]") |
| 141 |
fmt.Println(" path = ~/.gitconfig-work") |
| 142 |
fmt.Println(" path = ~/.gitconfig-personal") |
| 143 |
|
| 144 |
fmt.Println("\n=== Summary ===") |
| 145 |
fmt.Println("Include directives enable modular configuration.") |
| 146 |
fmt.Println("Common patterns in included files:") |
| 147 |
fmt.Println(" - Base settings (core options)") |
| 148 |
fmt.Println(" - User-specific settings") |
| 149 |
fmt.Println(" - Project-specific settings") |
| 150 |
fmt.Println(" - Environment-specific settings (dev/prod)") |
| 151 |
fmt.Println("") |
| 152 |
fmt.Println("Git processes includes in order they appear.") |
| 153 |
fmt.Println("Later values override earlier ones from different files.") |
| 154 |
} |