Skip to content

Commit

Permalink
Merge pull request #4 from davrodpin/rm-alias
Browse files Browse the repository at this point in the history
Add option to remove an alias
  • Loading branch information
davrodpin authored Oct 15, 2018
2 parents 880f0e2 + 817870e commit e460391
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 12 deletions.
25 changes: 15 additions & 10 deletions cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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: [<host>]:<port>")
f.Var(&c.Remote, "remote", "set remote endpoing address: [<host>]:<port>")
Expand All @@ -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 != "" {
Expand Down Expand Up @@ -122,6 +126,7 @@ func usage() string {
return `usage:
mole [-v] [-local [<host>]:<port>] -remote [<host>]:<port> -server [<user>@]<host>[:<port>] [-key <key_path>]
mole -alias <alias_name> [-v] [-local [<host>]:<port>] -remote [<host>]:<port> -server [<user>@]<host>[:<port>] [-key <key_path>]
mole -alias <alias_name> -delete
mole -start <alias_name>
mole -help
mole -version
Expand Down
4 changes: 4 additions & 0 deletions cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
15 changes: 15 additions & 0 deletions cmd/mole/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ func main() {
if err != nil {
os.Exit(1)
}
case "rm-alias":
err := rmAlias(*app)
if err != nil {
os.Exit(1)
}

}
}

Expand Down Expand Up @@ -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(),
Expand Down
3 changes: 3 additions & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,15 @@ $ mole -help
usage:
mole [-v] [-local [<host>]:<port>] -remote [<host>]:<port> -server [<user>@]<host>[:<port>] [-key <key_path>]
mole -alias <alias_name> [-v] [-local [<host>]:<port>] -remote [<host>]:<port> -server [<user>@]<host>[:<port>] [-key <key_path>]
mole -alias <alias_name> -delete
mole -start <alias_name>
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
Expand Down
19 changes: 19 additions & 0 deletions storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
31 changes: 29 additions & 2 deletions storage/storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}

Expand All @@ -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 {
Expand Down

0 comments on commit e460391

Please sign in to comment.