Created
November 19, 2018 15:55
-
-
Save assada/c5cd11e36273d3860a3217e4d586c19c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "fmt" | |
| "path/filepath" | |
| "github.com/hashicorp/consul/api" | |
| "log" | |
| "os" | |
| "io" | |
| "strings" | |
| "flag" | |
| ) | |
| func save(filepath, s string) error { | |
| fo, err := os.Create(filepath) | |
| if err != nil { | |
| return err | |
| } | |
| defer fo.Close() | |
| _, err = io.Copy(fo, strings.NewReader(s)) | |
| if err != nil { | |
| return err | |
| } | |
| log.Printf("Saved: %s", filepath) | |
| return nil | |
| } | |
| func getClient(host string, port int) *api.Client { | |
| config := api.DefaultConfig() | |
| config.Address = fmt.Sprintf("%s:%d", host, port) | |
| client, err := api.NewClient(config) | |
| if err != nil { | |
| log.Fatal(err.Error()) | |
| } | |
| return client | |
| } | |
| func main() { | |
| fmt.Print("SignNow Consul Keys Creator\n\n") | |
| from := flag.String("from", "nil", "Consul keys path") //"us-east-1/apps/dev-core-user-api/dev/keys/" | |
| to := flag.String("to", "./", "Local path for keys") | |
| host := flag.String("host", "consul", "Consul host") | |
| port := flag.Int("port", 8500, "Consul port") | |
| flag.Parse() | |
| if _, err := os.Stat(*to); os.IsNotExist(err) { | |
| log.Print("Destination folder does not exists. Creating...") | |
| os.MkdirAll(*to, os.ModePerm) | |
| } | |
| destination := strings.TrimSuffix(*to, "/"); | |
| kv := getClient(*host, *port).KV() | |
| keys, _, err := kv.List(*from, nil) | |
| if err != nil { | |
| log.Panic(err) | |
| } | |
| if (len(keys) <= 0) { | |
| log.Fatal("Keys consul path empty or does not exists") | |
| } | |
| for _, pair := range keys { | |
| parts := strings.Split(pair.Key, "/") | |
| filename := parts[len(parts)-1] | |
| if(filename != "") { | |
| if err := save(filepath.Join(destination, filename), string(pair.Value[:])); err != nil { | |
| log.Panic(err) | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment