From 817870ead2b3e2824c9a82e0343318233d50e03b Mon Sep 17 00:00:00 2001 From: David Pinheiro Date: Sun, 14 Oct 2018 20:59:58 -0700 Subject: [PATCH] Add option to remove an alias --- cli/cli.go | 25 +++++++++++++++---------- cli/cli_test.go | 4 ++++ cmd/mole/main.go | 15 +++++++++++++++ docs/index.md | 3 +++ storage/storage.go | 19 +++++++++++++++++++ storage/storage_test.go | 31 +++++++++++++++++++++++++++++-- 6 files changed, 85 insertions(+), 12 deletions(-) diff --git a/cli/cli.go b/cli/cli.go index cb088b5..1a66053 100644 --- a/cli/cli.go +++ b/cli/cli.go @@ -13,16 +13,17 @@ type App struct { args []string flag *flag.FlagSet - Command string - Local HostInput - Remote HostInput - Server HostInput - Key string - Verbose bool - Help bool - Version bool - Alias string - Start string + Command string + Local HostInput + Remote HostInput + Server HostInput + Key string + Verbose bool + Help bool + Version bool + Alias string + Start string + AliasDelete bool } func New(args []string) *App { @@ -33,6 +34,7 @@ func (c *App) Parse() error { f := flag.NewFlagSet(usage(), flag.ExitOnError) f.StringVar(&c.Alias, "alias", "", "Create a tunnel alias") + f.BoolVar(&c.AliasDelete, "delete", false, "delete a tunnel alias (must be used with -alias)") f.StringVar(&c.Start, "start", "", "Start a tunnel using a given alias") f.Var(&c.Local, "local", "(optional) Set local endpoint address: []:") f.Var(&c.Remote, "remote", "set remote endpoing address: []:") @@ -54,6 +56,8 @@ func (c *App) Parse() error { c.Command = "help" } else if c.Version { c.Command = "version" + } else if c.Alias != "" && c.AliasDelete { + c.Command = "rm-alias" } else if c.Alias != "" { c.Command = "new-alias" } else if c.Start != "" { @@ -122,6 +126,7 @@ func usage() string { return `usage: mole [-v] [-local []:] -remote []: -server [@][:] [-key ] mole -alias [-v] [-local []:] -remote []: -server [@][:] [-key ] + mole -alias -delete mole -start mole -help mole -version diff --git a/cli/cli_test.go b/cli/cli_test.go index 4cdeeb2..d6ccab3 100644 --- a/cli/cli_test.go +++ b/cli/cli_test.go @@ -28,6 +28,10 @@ func TestHandleArgs(t *testing.T) { []string{"./mole", "-alias", "xyz", "-remote", ":443", "-server", "example1"}, "new-alias", }, + { + []string{"./mole", "-alias", "xyz", "-delete"}, + "rm-alias", + }, { []string{"./mole", "-start", "example1-alias"}, "start-from-alias", diff --git a/cmd/mole/main.go b/cmd/mole/main.go index 52034ff..110aa25 100644 --- a/cmd/mole/main.go +++ b/cmd/mole/main.go @@ -42,6 +42,12 @@ func main() { if err != nil { os.Exit(1) } + case "rm-alias": + err := rmAlias(*app) + if err != nil { + os.Exit(1) + } + } } @@ -103,6 +109,15 @@ func newAlias(app cli.App) error { return nil } +func rmAlias(app cli.App) error { + _, err := storage.Remove(app.Alias) + if err != nil { + return err + } + + return nil +} + func app2alias(app cli.App) *storage.Tunnel { return &storage.Tunnel{ Local: app.Local.String(), diff --git a/docs/index.md b/docs/index.md index 3695a52..f58e6d2 100644 --- a/docs/index.md +++ b/docs/index.md @@ -108,12 +108,15 @@ $ mole -help usage: mole [-v] [-local []:] -remote []: -server [@][:] [-key ] mole -alias [-v] [-local []:] -remote []: -server [@][:] [-key ] + mole -alias -delete mole -start mole -help mole -version -alias string Create a tunnel alias + -delete + delete a tunnel alias (must be used with -alias) -help list all options available -key string diff --git a/storage/storage.go b/storage/storage.go index abea546..eae33a4 100644 --- a/storage/storage.go +++ b/storage/storage.go @@ -59,6 +59,25 @@ func FindByName(name string) (*Tunnel, error) { return tun, nil } +func Remove(name string) (*Tunnel, error) { + store, err := loadStore() + if err != nil { + return nil, fmt.Errorf("error while loading mole configuration: %v", err) + } + + tun := store.Tunnels[name] + + if tun != nil { + delete(store.Tunnels, name) + _, err := createStore(store) + if err != nil { + return nil, err + } + } + + return tun, nil +} + func loadStore() (*Store, error) { var store *Store diff --git a/storage/storage_test.go b/storage/storage_test.go index 532d24d..f008474 100644 --- a/storage/storage_test.go +++ b/storage/storage_test.go @@ -10,11 +10,11 @@ import ( ) func TestSaveTunnel(t *testing.T) { - alias := "hpe-halon-443" + alias := "example-save-443" expected := &storage.Tunnel{ Local: "", Remote: ":443", - Server: "hpe-halon", + Server: "example", Verbose: true, } @@ -33,6 +33,33 @@ func TestSaveTunnel(t *testing.T) { } } +func TestRemoveTunnel(t *testing.T) { + alias := "example-rm-443" + expected := &storage.Tunnel{ + Local: "", + Remote: ":443", + Server: "example", + Verbose: true, + } + + storage.Save(alias, expected) + value, err := storage.Remove(alias) + if err != nil { + t.Errorf("Test failed while removing tunnel configuration: %v", err) + } + + if !reflect.DeepEqual(expected, value) { + t.Errorf("Test failed.\n\texpected: %s\n\tvalue : %s", expected, value) + } + + value, _ = storage.FindByName(alias) + + if value != nil { + t.Errorf("Test failed. Alias %s is not suppose to exist after deletion.", alias) + } + +} + func TestMain(m *testing.M) { dir, err := ioutil.TempDir("", "mole-testing") if err != nil {