()
| 17 | } |
| 18 | |
| 19 | func TranslationHelper() (TranslationHelperFunc, func()) { |
| 20 | var translationKeyMap = map[string]string{} |
| 21 | v := viper.New() |
| 22 | |
| 23 | // Load from JSON file |
| 24 | v.SetConfigName("github-mcp-server-config") |
| 25 | v.SetConfigType("json") |
| 26 | v.AddConfigPath(".") |
| 27 | |
| 28 | if err := v.ReadInConfig(); err != nil { |
| 29 | // ignore error if file not found as it is not required |
| 30 | if _, ok := err.(viper.ConfigFileNotFoundError); !ok { |
| 31 | log.Printf("Could not read JSON config: %v", err) |
| 32 | } |
| 33 | } |
| 34 | |
| 35 | // create a function that takes both a key, and a default value and returns either the default value or an override value |
| 36 | return func(key string, defaultValue string) string { |
| 37 | key = strings.ToUpper(key) |
| 38 | if value, exists := translationKeyMap[key]; exists { |
| 39 | return value |
| 40 | } |
| 41 | // check if the env var exists |
| 42 | if value, exists := os.LookupEnv("GITHUB_MCP_" + key); exists { |
| 43 | // TODO I could not get Viper to play ball reading the env var |
| 44 | translationKeyMap[key] = value |
| 45 | return value |
| 46 | } |
| 47 | |
| 48 | v.SetDefault(key, defaultValue) |
| 49 | translationKeyMap[key] = v.GetString(key) |
| 50 | return translationKeyMap[key] |
| 51 | }, func() { |
| 52 | // dump the translationKeyMap to a json file |
| 53 | if err := DumpTranslationKeyMap(translationKeyMap); err != nil { |
| 54 | log.Fatalf("Could not dump translation key map: %v", err) |
| 55 | } |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | // DumpTranslationKeyMap writes the translation map to a json file called github-mcp-server-config.json |
| 60 | func DumpTranslationKeyMap(translationKeyMap map[string]string) error { |
no test coverage detected