Skip to content

Commit e460391

Browse files
authored
Merge pull request #4 from davrodpin/rm-alias
Add option to remove an alias
2 parents 880f0e2 + 817870e commit e460391

File tree

6 files changed

+85
-12
lines changed

6 files changed

+85
-12
lines changed

cli/cli.go

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,17 @@ type App struct {
1313
args []string
1414
flag *flag.FlagSet
1515

16-
Command string
17-
Local HostInput
18-
Remote HostInput
19-
Server HostInput
20-
Key string
21-
Verbose bool
22-
Help bool
23-
Version bool
24-
Alias string
25-
Start string
16+
Command string
17+
Local HostInput
18+
Remote HostInput
19+
Server HostInput
20+
Key string
21+
Verbose bool
22+
Help bool
23+
Version bool
24+
Alias string
25+
Start string
26+
AliasDelete bool
2627
}
2728

2829
func New(args []string) *App {
@@ -33,6 +34,7 @@ func (c *App) Parse() error {
3334
f := flag.NewFlagSet(usage(), flag.ExitOnError)
3435

3536
f.StringVar(&c.Alias, "alias", "", "Create a tunnel alias")
37+
f.BoolVar(&c.AliasDelete, "delete", false, "delete a tunnel alias (must be used with -alias)")
3638
f.StringVar(&c.Start, "start", "", "Start a tunnel using a given alias")
3739
f.Var(&c.Local, "local", "(optional) Set local endpoint address: [<host>]:<port>")
3840
f.Var(&c.Remote, "remote", "set remote endpoing address: [<host>]:<port>")
@@ -54,6 +56,8 @@ func (c *App) Parse() error {
5456
c.Command = "help"
5557
} else if c.Version {
5658
c.Command = "version"
59+
} else if c.Alias != "" && c.AliasDelete {
60+
c.Command = "rm-alias"
5761
} else if c.Alias != "" {
5862
c.Command = "new-alias"
5963
} else if c.Start != "" {
@@ -122,6 +126,7 @@ func usage() string {
122126
return `usage:
123127
mole [-v] [-local [<host>]:<port>] -remote [<host>]:<port> -server [<user>@]<host>[:<port>] [-key <key_path>]
124128
mole -alias <alias_name> [-v] [-local [<host>]:<port>] -remote [<host>]:<port> -server [<user>@]<host>[:<port>] [-key <key_path>]
129+
mole -alias <alias_name> -delete
125130
mole -start <alias_name>
126131
mole -help
127132
mole -version

cli/cli_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ func TestHandleArgs(t *testing.T) {
2828
[]string{"./mole", "-alias", "xyz", "-remote", ":443", "-server", "example1"},
2929
"new-alias",
3030
},
31+
{
32+
[]string{"./mole", "-alias", "xyz", "-delete"},
33+
"rm-alias",
34+
},
3135
{
3236
[]string{"./mole", "-start", "example1-alias"},
3337
"start-from-alias",

cmd/mole/main.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ func main() {
4242
if err != nil {
4343
os.Exit(1)
4444
}
45+
case "rm-alias":
46+
err := rmAlias(*app)
47+
if err != nil {
48+
os.Exit(1)
49+
}
50+
4551
}
4652
}
4753

@@ -103,6 +109,15 @@ func newAlias(app cli.App) error {
103109
return nil
104110
}
105111

112+
func rmAlias(app cli.App) error {
113+
_, err := storage.Remove(app.Alias)
114+
if err != nil {
115+
return err
116+
}
117+
118+
return nil
119+
}
120+
106121
func app2alias(app cli.App) *storage.Tunnel {
107122
return &storage.Tunnel{
108123
Local: app.Local.String(),

docs/index.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,12 +108,15 @@ $ mole -help
108108
usage:
109109
mole [-v] [-local [<host>]:<port>] -remote [<host>]:<port> -server [<user>@]<host>[:<port>] [-key <key_path>]
110110
mole -alias <alias_name> [-v] [-local [<host>]:<port>] -remote [<host>]:<port> -server [<user>@]<host>[:<port>] [-key <key_path>]
111+
mole -alias <alias_name> -delete
111112
mole -start <alias_name>
112113
mole -help
113114
mole -version
114115

115116
-alias string
116117
Create a tunnel alias
118+
-delete
119+
delete a tunnel alias (must be used with -alias)
117120
-help
118121
list all options available
119122
-key string

storage/storage.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,25 @@ func FindByName(name string) (*Tunnel, error) {
5959
return tun, nil
6060
}
6161

62+
func Remove(name string) (*Tunnel, error) {
63+
store, err := loadStore()
64+
if err != nil {
65+
return nil, fmt.Errorf("error while loading mole configuration: %v", err)
66+
}
67+
68+
tun := store.Tunnels[name]
69+
70+
if tun != nil {
71+
delete(store.Tunnels, name)
72+
_, err := createStore(store)
73+
if err != nil {
74+
return nil, err
75+
}
76+
}
77+
78+
return tun, nil
79+
}
80+
6281
func loadStore() (*Store, error) {
6382
var store *Store
6483

storage/storage_test.go

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ import (
1010
)
1111

1212
func TestSaveTunnel(t *testing.T) {
13-
alias := "hpe-halon-443"
13+
alias := "example-save-443"
1414
expected := &storage.Tunnel{
1515
Local: "",
1616
Remote: ":443",
17-
Server: "hpe-halon",
17+
Server: "example",
1818
Verbose: true,
1919
}
2020

@@ -33,6 +33,33 @@ func TestSaveTunnel(t *testing.T) {
3333
}
3434
}
3535

36+
func TestRemoveTunnel(t *testing.T) {
37+
alias := "example-rm-443"
38+
expected := &storage.Tunnel{
39+
Local: "",
40+
Remote: ":443",
41+
Server: "example",
42+
Verbose: true,
43+
}
44+
45+
storage.Save(alias, expected)
46+
value, err := storage.Remove(alias)
47+
if err != nil {
48+
t.Errorf("Test failed while removing tunnel configuration: %v", err)
49+
}
50+
51+
if !reflect.DeepEqual(expected, value) {
52+
t.Errorf("Test failed.\n\texpected: %s\n\tvalue : %s", expected, value)
53+
}
54+
55+
value, _ = storage.FindByName(alias)
56+
57+
if value != nil {
58+
t.Errorf("Test failed. Alias %s is not suppose to exist after deletion.", alias)
59+
}
60+
61+
}
62+
3663
func TestMain(m *testing.M) {
3764
dir, err := ioutil.TempDir("", "mole-testing")
3865
if err != nil {

0 commit comments

Comments
 (0)