Skip to content

Commit

Permalink
Added tree
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Persson committed Sep 24, 2015
1 parent 028364b commit b74e18a
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 1 deletion.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# etcd-Export/Import/Validate
# etcd-Export/Import/Validate/Delete/Tree

Export/Import etcd directory as JSON/YAML/TOML and validate directory using JSON schema.

Expand Down
66 changes: 66 additions & 0 deletions src/github.com/mickep76/etcd-tree/etcd-tree.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package main

import (
"flag"
"fmt"
"log"
"os"
"strings"

etcd "github.com/coreos/go-etcd/etcd"

"github.com/mickep76/common"
)

// Tree
func Tree(root *etcd.Node, indent string) {
for _, n := range root.Nodes {
keys := strings.Split(n.Key, "/")
k := keys[len(keys)-1]
if n.Dir {
fmt.Printf("%s├── %s/\n", indent, k)
Tree(n, indent+"│ ")
} else {
fmt.Printf("%s├── %s\n", indent, k)
}
}
}

/*
├── postfix
│   ├── LICENSE
│   ├── TLS_LICENSE
│   ├── access
│   ├── aliases
*/

func main() {
// Options.
version := flag.Bool("version", false, "Version")
peers := flag.String("peers", common.GetEnv(), "Comma separated list of etcd nodes")
dir := flag.String("dir", "", "etcd directory")
flag.Parse()

// Print version.
if *version {
fmt.Printf("etcd-import %s\n", common.Version)
os.Exit(0)
}

// Validate input.
if *dir == "" {
log.Fatalf("You need to specify etcd dir.")
}

// Setup etcd client.
client := etcd.NewClient(strings.Split(*peers, ","))

// Export data.
res, err := client.Get(*dir, true, true)
if err != nil {
log.Fatal(err.Error())
}

fmt.Println(strings.TrimRight(*dir, "/") + "/")
Tree(res.Node, "")
}

0 comments on commit b74e18a

Please sign in to comment.