Skip to content

Commit

Permalink
Fix ignore
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Persson committed Nov 27, 2015
1 parent 58783f7 commit 1173009
Show file tree
Hide file tree
Showing 12 changed files with 594 additions and 47 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
.DS_Store
etcdtool
.build
bin
pkg
.build
*.rpm
45 changes: 0 additions & 45 deletions ebuild/dev-db/etcd-export/etcd-export-2.3.1.ebuild

This file was deleted.

40 changes: 40 additions & 0 deletions ebuild/dev-db/etcdtool/etcdtool-2.6.ebuild
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Copyright 1999-2015 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Id$
# By Jean-Michel Smith, first created 9/21/15

EAPI=5

inherit user git-r3

DESCRIPTION="Export/Import/Edit etcd directory as JSON/YAML/TOML and validate directory using JSON schema"
HOMEPAGE="https://github.com/mickep76/etcdtool.git"
SRC_URI=""

LICENSE="Apache-2.0"
SLOT="0"
KEYWORDS="amd64"
IUSE=""

DEPEND="dev-lang/go"

EGIT_REPO_URI="https://github.com/mickep76/etcdtool.git"
EGIT_COMMIT="${PV}"

GOPATH="${WORKDIR}/etcdtool-${PV}"

src_compile() {
ebegin "Building etcdtool ${PV}"
export GOPATH
export PATH=${GOPATH}/bin:${PATH}
cd ${GOPATH}
./build
cd
eend ${?}
}

src_install() {
ebegin "installing etcdtool ${PV}"
dobin ${GOPATH}/bin/etcdtool
eend ${?}
}
63 changes: 63 additions & 0 deletions src/github.com/mickep76/etcdtool/command/connect.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package command

import (
"log"
"net/http"
"strings"
"time"

"github.com/codegangsta/cli"
"github.com/coreos/etcd/client"
"github.com/coreos/etcd/pkg/transport"
"golang.org/x/net/context"
)

func contextWithCommandTimeout(c *cli.Context) (context.Context, context.CancelFunc) {
return context.WithTimeout(context.Background(), c.GlobalDuration("command-timeout"))
}

func newTransport(c *cli.Context) *http.Transport {
tls := transport.TLSInfo{
CAFile: c.GlobalString("ca"),
CertFile: c.GlobalString("cert"),
KeyFile: c.GlobalString("key"),
}

timeout := 30 * time.Second
tr, err := transport.NewTransport(tls, timeout)
if err != nil {
log.Fatal(err.Error())
}

return tr
}

func newClient(c *cli.Context) client.Client {
cfg := client.Config{
Transport: newTransport(c),
Endpoints: strings.Split(c.GlobalString("peers"), ","),
HeaderTimeoutPerRequest: c.GlobalDuration("timeout"),
}

/*
uFlag := c.GlobalString("username")
if uFlag != "" {
username, password, err := getUsernamePasswordFromFlag(uFlag)
if err != nil {
return nil, err
}
cfg.Username = username
cfg.Password = password
}
*/
cl, err := client.New(cfg)
if err != nil {
log.Fatal(err.Error())
}

return cl
}

func newKeyAPI(c *cli.Context) client.KeysAPI {
return client.NewKeysAPI(newClient(c))
}
95 changes: 95 additions & 0 deletions src/github.com/mickep76/etcdtool/command/edit_command.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package command

import (
"fmt"
"log"
"os"
"os/exec"
"strings"

"github.com/codegangsta/cli"
"github.com/coreos/etcd/client"
"github.com/mickep76/iodatafmt"
)

// NewImportCommand sets data from input.
func NewEditCommand() cli.Command {
return cli.Command{
Name: "edit",
Usage: "edit a directory",
Flags: []cli.Flag{
cli.BoolFlag{Name: "sort, s", Usage: "returns result in sorted order"},
cli.BoolFlag{Name: "yes, y", Usage: "Answer yes to any questions"},
cli.BoolFlag{Name: "replace, r", Usage: "Replace data"},
cli.StringFlag{Name: "format, f", Value: "JSON", EnvVar: "ETCDTOOL_FORMAT", Usage: "Data serialization format YAML, TOML or JSON"},
cli.StringFlag{Name: "editor, e", Value: "vim", Usage: "Editor", EnvVar: "EDITOR"},
cli.StringFlag{Name: "tmp-file, t", Value: ".etcdtool.swp", Usage: "Temporary file"},
},
Action: func(c *cli.Context) {
editCommandFunc(c, newKeyAPI(c))
},
}
}

func editFile(editor string, file string) error {
_, err := exec.LookPath(editor)
if err != nil {
log.Fatalf("Editor doesn't exist: %s", editor)
}

cmd := exec.Command(editor, file)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
if err := cmd.Run(); err != nil {
log.Fatal(err.Error())
}
return nil
}

// editCommandFunc edit data as either JSON, YAML or TOML.
func editCommandFunc(c *cli.Context, ki client.KeysAPI) {
var key string
if len(c.Args()) == 0 {
log.Fatal("You need to specify directory")
} else {
key = strings.TrimRight(c.Args()[0], "/") + "/"
}

sort := c.Bool("sort")

// Get data format.
f, err := iodatafmt.Format(c.String("format"))
if err != nil {
log.Fatal(err.Error())
}

// Export to file.
exportFunc(key, sort, c.String("tmp-file"), f, c, ki)

// Get modified time stamp.
before, err := os.Stat(c.String("tmp-file"))
if err != nil {
log.Fatal(err.Error())
}

// Edit file.
editFile(c.String("editor"), c.String("tmp-file"))

// Check modified time stamp.
after, err := os.Stat(c.String("tmp-file"))
if err != nil {
log.Fatal(err.Error())
}

// Import from file if it has changed.
if before.ModTime() != after.ModTime() {
importFunc(key, c.String("tmp-file"), f, c.Bool("replace"), c.Bool("yes"), c, ki)
} else {
fmt.Printf("File wasn't modified, skipping import\n")
}

// Unlink file.
if err := os.Remove(c.String("tmp-file")); err != nil {
log.Fatal(err.Error())
}
}
63 changes: 63 additions & 0 deletions src/github.com/mickep76/etcdtool/command/export_command.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package command

import (
"log"
"strings"

"github.com/codegangsta/cli"
"github.com/coreos/etcd/client"
"github.com/mickep76/etcdmap"
"github.com/mickep76/iodatafmt"
)

// NewExportCommand returns data from export.
func NewExportCommand() cli.Command {
return cli.Command{
Name: "export",
Usage: "export a directory",
Flags: []cli.Flag{
cli.BoolFlag{Name: "sort, s", Usage: "returns result in sorted order"},
cli.StringFlag{Name: "format, f", EnvVar: "ETCDTOOL_FORMAT", Value: "JSON", Usage: "Data serialization format YAML, TOML or JSON"},
cli.StringFlag{Name: "output, o", Value: "", Usage: "Output file"},
},
Action: func(c *cli.Context) {
exportCommandFunc(c, newKeyAPI(c))
},
}
}

// exportCommandFunc exports data as either JSON, YAML or TOML.
func exportCommandFunc(c *cli.Context, ki client.KeysAPI) {
key := "/"
if len(c.Args()) != 0 {
key = strings.TrimRight(c.Args()[0], "/") + "/"
}

sort := c.Bool("sort")

// Get data format.
f, err := iodatafmt.Format(c.String("format"))
if err != nil {
log.Fatal(err.Error())
}

exportFunc(key, sort, c.String("output"), f, c, ki)
}

// exportCommandFunc exports data as either JSON, YAML or TOML.
func exportFunc(key string, sort bool, file string, f iodatafmt.DataFmt, c *cli.Context, ki client.KeysAPI) {
ctx, cancel := contextWithCommandTimeout(c)
resp, err := ki.Get(ctx, key, &client.GetOptions{Sort: sort, Recursive: true})
cancel()
if err != nil {
log.Fatal(err.Error())
}

// Export and write output.
m := etcdmap.Map(resp.Node)
if file != "" {
iodatafmt.Write(file, m, f)
} else {
iodatafmt.Print(m, f)
}
}
Loading

0 comments on commit 1173009

Please sign in to comment.