|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "log" |
| 6 | + "net/http" |
| 7 | + "strings" |
| 8 | + |
| 9 | + "github.com/Piszmog/cloudconfigclient/v2" |
| 10 | +) |
| 11 | + |
| 12 | +func main() { |
| 13 | + // ensure you have the Config Server running locally... |
| 14 | + client, err := cloudconfigclient.New(cloudconfigclient.Basic(&http.Client{}, "username", "password", "http://localhost:8888")) |
| 15 | + if err != nil { |
| 16 | + log.Fatalln(err) |
| 17 | + } |
| 18 | + |
| 19 | + // load a config file |
| 20 | + configuration, err := client.GetConfiguration("test-app", "local") |
| 21 | + if err != nil { |
| 22 | + log.Fatalln(err) |
| 23 | + } |
| 24 | + // we can either unmarshal into a struct |
| 25 | + var configData configStruct |
| 26 | + if err = configuration.Unmarshal(&configData); err != nil { |
| 27 | + log.Fatalln("failed to find cloud property file") |
| 28 | + } |
| 29 | + |
| 30 | + // or manually access the values |
| 31 | + localProp, err := configuration.GetPropertySource("application-local.yml") |
| 32 | + if err != nil { |
| 33 | + log.Fatalln("failed to find local property file") |
| 34 | + } |
| 35 | + fmt.Printf("local property file: %+v\n", localProp) |
| 36 | + // handle all config properties |
| 37 | + configuration.HandlePropertySources(func(propertySource cloudconfigclient.PropertySource) { |
| 38 | + if strings.HasSuffix(propertySource.Name, "test-app.properties") { |
| 39 | + // TODO save off values |
| 40 | + } |
| 41 | + }) |
| 42 | + |
| 43 | + // load a specific file (e.g. json/txt) |
| 44 | + var f map[string]string |
| 45 | + // if 'fooDir' has been added to 'searchPaths' in SCS v3.x, then pass "" (blank) for directory |
| 46 | + if err = client.GetFile("fooDir", "bar.json", &f); err != nil { |
| 47 | + log.Fatalln(err) |
| 48 | + } |
| 49 | + fmt.Printf("file from default branch: %+v\n", f) |
| 50 | + |
| 51 | + // load a specific file (e.g. json/txt) |
| 52 | + var b map[string]string |
| 53 | + // if 'fooDir' has been added to 'searchPaths' in SCS v3.x, then pass "" (blank) for directory |
| 54 | + if err = client.GetFileFromBranch("develop", "fooDir", "bar.json", &b); err != nil { |
| 55 | + log.Fatalln(err) |
| 56 | + } |
| 57 | + fmt.Printf("file from specific branch: %+v\n", b) |
| 58 | +} |
| 59 | + |
| 60 | +type configStruct struct { |
| 61 | + Field1 string `json:"field1"` |
| 62 | +} |
0 commit comments