3 changed files with 171 additions and 1 deletions
@ -0,0 +1,166 @@ |
|||||
|
package main |
||||
|
|
||||
|
import ( |
||||
|
"context" |
||||
|
"flag" |
||||
|
"fmt" |
||||
|
"os" |
||||
|
"strings" |
||||
|
) |
||||
|
|
||||
|
func handleAliasCommand() { |
||||
|
if len(os.Args) < 3 { |
||||
|
fmt.Fprintf(os.Stderr, "%s alias: subcommand missing\n", os.Args[0]) |
||||
|
printAliasUsage() |
||||
|
os.Exit(1) |
||||
|
} |
||||
|
|
||||
|
subcmd := strings.ToLower(os.Args[2]) |
||||
|
switch subcmd { |
||||
|
case "list": |
||||
|
listcmd := flag.NewFlagSet("alias list", flag.ExitOnError) |
||||
|
listcmd.SetOutput(os.Stdout) |
||||
|
cfgpath := listcmd.String("config", "", "path to configuration") |
||||
|
aname := listcmd.String("name", "", "filter list to named alias") |
||||
|
listcmd.Parse(os.Args[3:]) |
||||
|
|
||||
|
cfg := LoadConfig(*cfgpath) |
||||
|
es := ClientFromConfig(cfg.Elastic) |
||||
|
|
||||
|
aliases, err := es.CatAliases().Do(context.Background()) |
||||
|
if err != nil { |
||||
|
fmt.Fprintf(os.Stderr, "elastic error: %s\n", err.Error()) |
||||
|
os.Exit(1) |
||||
|
} |
||||
|
|
||||
|
for _, alias := range aliases { |
||||
|
if *aname != "" { |
||||
|
if alias.Alias == *aname { |
||||
|
fmt.Printf("%s\n", alias.Index) |
||||
|
} |
||||
|
} else { |
||||
|
fmt.Printf("%s %s\n", alias.Alias, alias.Index) |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
case "create": |
||||
|
createcmd := flag.NewFlagSet("alias create", flag.ExitOnError) |
||||
|
createcmd.SetOutput(os.Stdout) |
||||
|
cfgpath := createcmd.String("config", "", "path to configuration") |
||||
|
aname := createcmd.String("name", "", "(required) name of alias") |
||||
|
iname := createcmd.String("index", "", "(required) name of index") |
||||
|
createcmd.Parse(os.Args[3:]) |
||||
|
|
||||
|
if *aname == "" { |
||||
|
fmt.Fprintf(os.Stderr, "name parameter is required for alias create\n") |
||||
|
createcmd.PrintDefaults() |
||||
|
os.Exit(1) |
||||
|
} |
||||
|
|
||||
|
if *iname == "" { |
||||
|
fmt.Fprintf(os.Stderr, "index parameter is required for alias create\n") |
||||
|
createcmd.PrintDefaults() |
||||
|
os.Exit(1) |
||||
|
} |
||||
|
|
||||
|
cfg := LoadConfig(*cfgpath) |
||||
|
es := ClientFromConfig(cfg.Elastic) |
||||
|
|
||||
|
_, err := es.Alias().Add(*iname, *aname).Do(context.Background()) |
||||
|
if err != nil { |
||||
|
fmt.Fprintf(os.Stderr, "elastic error: %s\n", err.Error()) |
||||
|
os.Exit(1) |
||||
|
} |
||||
|
|
||||
|
case "delete": |
||||
|
deletecmd := flag.NewFlagSet("alias delete", flag.ExitOnError) |
||||
|
deletecmd.SetOutput(os.Stdout) |
||||
|
cfgpath := deletecmd.String("config", "", "path to configuration") |
||||
|
aname := deletecmd.String("name", "", "(required) name of alias") |
||||
|
iname := deletecmd.String("index", "", "(required) name of index") |
||||
|
deletecmd.Parse(os.Args[3:]) |
||||
|
|
||||
|
if *aname == "" { |
||||
|
fmt.Fprintf(os.Stderr, "name parameter is required for alias delete\n") |
||||
|
deletecmd.PrintDefaults() |
||||
|
os.Exit(1) |
||||
|
} |
||||
|
|
||||
|
if *iname == "" { |
||||
|
fmt.Fprintf(os.Stderr, "index parameter is required for alias delete\n") |
||||
|
deletecmd.PrintDefaults() |
||||
|
os.Exit(1) |
||||
|
} |
||||
|
|
||||
|
cfg := LoadConfig(*cfgpath) |
||||
|
es := ClientFromConfig(cfg.Elastic) |
||||
|
|
||||
|
_, err := es.Alias().Remove(*iname, *aname).Do(context.Background()) |
||||
|
if err != nil { |
||||
|
fmt.Fprintf(os.Stderr, "elastic error: %s\n", err.Error()) |
||||
|
os.Exit(1) |
||||
|
} |
||||
|
|
||||
|
case "replace": |
||||
|
replacecmd := flag.NewFlagSet("alias replace", flag.ExitOnError) |
||||
|
replacecmd.SetOutput(os.Stdout) |
||||
|
cfgpath := replacecmd.String("config", "", "path to configuration") |
||||
|
aname := replacecmd.String("name", "", "(required) name of alias") |
||||
|
oiname := replacecmd.String("oldindex", "", |
||||
|
"(required) name of the old index") |
||||
|
niname := replacecmd.String("newindex", "", |
||||
|
"(required) name of the new index") |
||||
|
replacecmd.Parse(os.Args[3:]) |
||||
|
|
||||
|
if *aname == "" { |
||||
|
fmt.Fprintf(os.Stderr, "name parameter is required for alias replace\n") |
||||
|
replacecmd.PrintDefaults() |
||||
|
os.Exit(1) |
||||
|
} |
||||
|
if *oiname == "" { |
||||
|
fmt.Fprintf(os.Stderr, |
||||
|
"oldindex parameter is required for alias replace\n") |
||||
|
replacecmd.PrintDefaults() |
||||
|
os.Exit(1) |
||||
|
} |
||||
|
if *niname == "" { |
||||
|
fmt.Fprintf(os.Stderr, |
||||
|
"newindex parameter is required for alias replace\n") |
||||
|
replacecmd.PrintDefaults() |
||||
|
os.Exit(1) |
||||
|
} |
||||
|
|
||||
|
cfg := LoadConfig(*cfgpath) |
||||
|
es := ClientFromConfig(cfg.Elastic) |
||||
|
|
||||
|
_, err := es.Alias(). |
||||
|
Remove(*oiname, *aname). |
||||
|
Add(*niname, *aname). |
||||
|
Do(context.Background()) |
||||
|
if err != nil { |
||||
|
fmt.Fprintf(os.Stderr, "elastic error: %s\n", err.Error()) |
||||
|
os.Exit(1) |
||||
|
} |
||||
|
|
||||
|
case "--help": |
||||
|
printAliasUsage() |
||||
|
os.Exit(0) |
||||
|
|
||||
|
default: |
||||
|
fmt.Fprintf(os.Stderr, "%s: alias '%s' is not a recognized subcommand\n", |
||||
|
os.Args[0], os.Args[2]) |
||||
|
printAliasUsage() |
||||
|
os.Exit(1) |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
func printAliasUsage() { |
||||
|
fmt.Printf("Usage: %s alias <subcommand> ... \n", os.Args[0]) |
||||
|
fmt.Printf("See '%s alias <subcommand> --help' for information", os.Args[0]) |
||||
|
fmt.Printf(" on a specific command\n") |
||||
|
fmt.Printf("valid subcommands:\n") |
||||
|
fmt.Printf(" list list aliases\n") |
||||
|
fmt.Printf(" create create alias\n") |
||||
|
fmt.Printf(" delete delete alias\n") |
||||
|
fmt.Printf(" replace replace alias (single step add/remove)\n") |
||||
|
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue