Skip to content

Commit

Permalink
Made all -dir pos. arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Persson committed Oct 27, 2015
1 parent 042a756 commit 3d13df0
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 49 deletions.
2 changes: 1 addition & 1 deletion src/github.com/mickep76/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ func GetEnv() string {
}
}

return "127.0.0.1:4001,127.0.0.1:2379"
return "http://127.0.0.1:4001,http://127.0.0.1:2379"
}
2 changes: 1 addition & 1 deletion src/github.com/mickep76/common/version.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package common

// Version
const Version = "2.3"
const Version = "2.4"
29 changes: 17 additions & 12 deletions src/github.com/mickep76/etcd-edit/etcd-edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,16 @@ func main() {
noValidate := flag.Bool("no-validate", false, "Skip validation using JSON schema")
schema := flag.String("schema", "", "etcd key for JSON schema")
editor := flag.String("editor", getEditor(), "Editor")
dir := flag.String("dir", "/", "etcd directory")
format := flag.String("format", "JSON", "Data serialization format YAML, TOML or JSON")
tmpFile := flag.String("tmp-file", ".etcd-edit.swp", "Temporary file")
flag.Parse()

var dir string
if len(flag.Args()) < 1 {
log.Fatal("You need to specify dir.")
}
dir = flag.Args()[0]

// Print version.
if *version {
fmt.Printf("etcd-edit %s\n", common.Version)
Expand Down Expand Up @@ -85,7 +90,7 @@ func main() {
case reflect.Map:
var vm map[string]interface{}
vm = v.(map[string]interface{})
match, err := regexp.MatchString(vm["regexp"].(string), *dir)
match, err := regexp.MatchString(vm["regexp"].(string), dir)
if err != nil {
panic(err)
}
Expand All @@ -98,7 +103,7 @@ func main() {
}

if *schema == "" {
log.Fatalf("Couldn't determine schema and template to use for directory (use -no-validate to skip this): %s", *dir)
log.Fatalf("Couldn't determine schema and template to use for directory (use -no-validate to skip this): %s", dir)
}
}

Expand All @@ -120,7 +125,7 @@ func main() {

} else {
kapi := etcd.NewKeysAPI(client)
res, err := kapi.Get(context.Background(), *dir, &etcd.GetOptions{Recursive: true})
res, err := kapi.Get(context.Background(), dir, &etcd.GetOptions{Recursive: true})
if err != nil {
log.Fatal(err.Error())
}
Expand Down Expand Up @@ -181,7 +186,7 @@ EDIT:

if !result.Valid() {
for _, e := range result.Errors() {
fmt.Printf("%s: %s\n", strings.Replace(e.Context().String("/"), "(root)", *dir, 1), e.Description())
fmt.Printf("%s: %s\n", strings.Replace(e.Context().String("/"), "(root)", dir, 1), e.Description())
}

fmt.Printf("Do you want to correct the changes? [yes|no]")
Expand All @@ -199,7 +204,7 @@ EDIT:
// Delete dir.
if !*noDelete {
if !*force {
fmt.Printf("Do you want to remove existing data in path: %s? [yes|no]", strings.TrimRight(*dir, "/"))
fmt.Printf("Do you want to remove existing data in path: %s? [yes|no]", strings.TrimRight(dir, "/"))
var query string
fmt.Scanln(&query)
if query != "yes" {
Expand All @@ -208,19 +213,19 @@ EDIT:
}

kapi := etcd.NewKeysAPI(client)
if _, err = kapi.Delete(context.Background(), strings.TrimRight(*dir, "/"), &etcd.DeleteOptions{Recursive: true}); err != nil {
if _, err = kapi.Delete(context.Background(), strings.TrimRight(dir, "/"), &etcd.DeleteOptions{Recursive: true}); err != nil {
// Don't exit since -new dir. won't exist
log.Println(err.Error())
} else {
log.Printf("Removed path: %s", strings.TrimRight(*dir, "/"))
log.Printf("Removed path: %s", strings.TrimRight(dir, "/"))
}

// Create dir.
if _, err := kapi.Set(context.TODO(), *dir, "", &etcd.SetOptions{Dir: true}); err != nil {
if _, err := kapi.Set(context.TODO(), dir, "", &etcd.SetOptions{Dir: true}); err != nil {
log.Fatalf(err.Error())
}
} else {
fmt.Printf("Do you want to overwrite existing data in path: %s? [yes|no]", strings.TrimRight(*dir, "/"))
fmt.Printf("Do you want to overwrite existing data in path: %s? [yes|no]", strings.TrimRight(dir, "/"))
var query string
fmt.Scanln(&query)
if query != "yes" {
Expand All @@ -229,8 +234,8 @@ EDIT:
}

// Import data.
log.Printf("Import data to: %s", strings.TrimRight(*dir, "/"))
if err = etcdmap.Create(&client, strings.TrimRight(*dir, "/"), reflect.ValueOf(imp)); err != nil {
log.Printf("Import data to: %s", strings.TrimRight(dir, "/"))
if err = etcdmap.Create(&client, strings.TrimRight(dir, "/"), reflect.ValueOf(imp)); err != nil {
log.Fatal(err.Error())
}
}
10 changes: 8 additions & 2 deletions src/github.com/mickep76/etcd-export/etcd-export.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,17 @@ 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")
// dir := flag.String("dir", "/", "etcd directory")
format := flag.String("format", "JSON", "Data serialization format YAML, TOML or JSON")
output := flag.String("output", "", "Output file")
flag.Parse()

var dir string
if len(flag.Args()) < 1 {
log.Fatal("You need to specify dir.")
}
dir = flag.Args()[0]

// Print version.
if *version {
fmt.Printf("etcd-export %s\n", common.Version)
Expand All @@ -51,7 +57,7 @@ func main() {

// Export data.
kapi := etcd.NewKeysAPI(client)
res, err3 := kapi.Get(context.Background(), *dir, &etcd.GetOptions{Recursive: true})
res, err3 := kapi.Get(context.Background(), dir, &etcd.GetOptions{Recursive: true})
if err3 != nil {
log.Fatal(err3.Error())
}
Expand Down
30 changes: 15 additions & 15 deletions src/github.com/mickep76/etcd-import/etcd-import.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,24 +30,24 @@ func main() {
peers := flag.String("peers", common.GetEnv(), "Comma separated list of etcd nodes")
force := flag.Bool("force", false, "Force delete without asking")
delete := flag.Bool("delete", false, "Delete entry before import")
dir := flag.String("dir", "", "etcd directory")
format := flag.String("format", "JSON", "Data serialization format YAML, TOML or JSON")
input := flag.String("input", "", "Input file")
noValidate := flag.Bool("no-validate", false, "Skip validation using JSON schema")
schema := flag.String("schema", "", "etcd key for JSON schema")
flag.Parse()

var dir string
if len(flag.Args()) < 1 {
log.Fatal("You need to specify dir.")
}
dir = flag.Args()[0]

// 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.")
}

// Get data format.
f, err := iodatafmt.Format(*format)
if err != nil {
Expand Down Expand Up @@ -80,7 +80,7 @@ func main() {
case reflect.Map:
var vm map[string]interface{}
vm = v.(map[string]interface{})
match, err := regexp.MatchString(vm["regexp"].(string), *dir)
match, err := regexp.MatchString(vm["regexp"].(string), dir)
if err != nil {
panic(err)
}
Expand All @@ -92,7 +92,7 @@ func main() {
}

if *schema == "" {
log.Fatalf("Couldn't determine schema to use for directory (use -no-validate to skip this): %s", *dir)
log.Fatalf("Couldn't determine schema to use for directory (use -no-validate to skip this): %s", dir)
}
}

Expand Down Expand Up @@ -134,7 +134,7 @@ func main() {

if !result.Valid() {
for _, e := range result.Errors() {
fmt.Printf("%s: %s\n", strings.Replace(e.Context().String("/"), "(root)", *dir, 1), e.Description())
fmt.Printf("%s: %s\n", strings.Replace(e.Context().String("/"), "(root)", dir, 1), e.Description())
}
os.Exit(1)
}
Expand All @@ -143,7 +143,7 @@ func main() {
// Delete dir.
if *delete {
if !*force {
fmt.Printf("Remove path: %s? [yes|no]", strings.TrimRight(*dir, "/"))
fmt.Printf("Remove path: %s? [yes|no]", strings.TrimRight(dir, "/"))
var query string
fmt.Scanln(&query)
if query != "yes" {
Expand All @@ -152,19 +152,19 @@ func main() {
}

kapi := etcd.NewKeysAPI(client)
if _, err = kapi.Delete(context.Background(), strings.TrimRight(*dir, "/"), &etcd.DeleteOptions{Recursive: true}); err != nil {
if _, err = kapi.Delete(context.Background(), strings.TrimRight(dir, "/"), &etcd.DeleteOptions{Recursive: true}); err != nil {
log.Fatalf(err.Error())
}
log.Printf("Removed path: %s", strings.TrimRight(*dir, "/"))
log.Printf("Removed path: %s", strings.TrimRight(dir, "/"))
}

// Create dir.
kapi := etcd.NewKeysAPI(client)
if _, err := kapi.Set(context.TODO(), *dir, "", &etcd.SetOptions{Dir: true}); err != nil {
if _, err := kapi.Set(context.TODO(), dir, "", &etcd.SetOptions{Dir: true}); err != nil {
log.Printf(err.Error())

// Should prob. check that we're actually dealing with an existing key and not something else...
fmt.Printf("Do you want to overwrite existing data in path: %s? [yes|no]", strings.TrimRight(*dir, "/"))
fmt.Printf("Do you want to overwrite existing data in path: %s? [yes|no]", strings.TrimRight(dir, "/"))
var query string
fmt.Scanln(&query)
if query != "yes" {
Expand All @@ -173,7 +173,7 @@ func main() {
}

// Import data.
if err = etcdmap.Create(&client, strings.TrimRight(*dir, "/"), reflect.ValueOf(m)); err != nil {
if err = etcdmap.Create(&client, strings.TrimRight(dir, "/"), reflect.ValueOf(m)); err != nil {
log.Fatal(err.Error())
}
}
17 changes: 9 additions & 8 deletions src/github.com/mickep76/etcd-tree/etcd-tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,20 +48,21 @@ 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()

var dir string
if len(flag.Args()) < 1 {
dir = "/"
} else {
dir = flag.Args()[0]
}

// 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.")
}

// Connect to etcd.
cfg := etcd.Config{
Endpoints: strings.Split(*peers, ","),
Expand All @@ -76,14 +77,14 @@ func main() {

// Export data.
kapi := etcd.NewKeysAPI(client)
res, err := kapi.Get(context.Background(), *dir, &etcd.GetOptions{Recursive: true, Sort: true})
res, err := kapi.Get(context.Background(), dir, &etcd.GetOptions{Recursive: true, Sort: true})
if err != nil {
log.Fatal(err.Error())
}

numDirs = 0
numKeys = 0
fmt.Println(strings.TrimRight(*dir, "/") + "/")
fmt.Println(strings.TrimRight(dir, "/") + "/")
Tree(res.Node, "")
fmt.Printf("\n%d directories, %d keys\n", numDirs, numKeys)
}
20 changes: 10 additions & 10 deletions src/github.com/mickep76/etcd-validate/etcd-validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,21 +22,21 @@ 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")
schema := flag.String("schema", "", "etcd key for JSON schema")
flag.Parse()

var dir string
if len(flag.Args()) < 1 {
log.Fatal("You need to specify dir.")
}
dir = flag.Args()[0]

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

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

// Connect to etcd.
cfg := etcd.Config{
Endpoints: strings.Split(*peers, ","),
Expand All @@ -63,7 +63,7 @@ func main() {
case reflect.Map:
var vm map[string]interface{}
vm = v.(map[string]interface{})
match, err := regexp.MatchString(vm["regexp"].(string), *dir)
match, err := regexp.MatchString(vm["regexp"].(string), dir)
if err != nil {
panic(err)
}
Expand All @@ -75,7 +75,7 @@ func main() {
}

if *schema == "" {
log.Fatalf("Couldn't determine schema to use for directory: %s", *dir)
log.Fatalf("Couldn't determine schema to use for directory: %s", dir)
}
}

Expand All @@ -89,7 +89,7 @@ func main() {
schemaLoader := jsonschema.NewStringLoader(res.Node.Value)

// Get etcd dir.
res2, err2 := kapi.Get(context.Background(), *dir, &etcd.GetOptions{Recursive: true})
res2, err2 := kapi.Get(context.Background(), dir, &etcd.GetOptions{Recursive: true})
if err2 != nil {
log.Fatal(err2.Error())
}
Expand All @@ -104,7 +104,7 @@ func main() {

if !result.Valid() {
for _, e := range result.Errors() {
fmt.Printf("%s: %s\n", strings.Replace(e.Context().String("/"), "(root)", *dir, 1), e.Description())
fmt.Printf("%s: %s\n", strings.Replace(e.Context().String("/"), "(root)", dir, 1), e.Description())
}
}
}

0 comments on commit 3d13df0

Please sign in to comment.