Repository navigation
Expand file tree
/
Copy pathlist.go
More file actions
96 lines (86 loc) · 2.13 KB
/
Copy pathlist.go
File metadata and controls
96 lines (86 loc) · 2.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package cmd
import (
"context"
"os"
"strings"
"github.com/spf13/cobra"
"github.com/Devehab/linko/cloudflare"
"github.com/Devehab/linko/config"
"github.com/Devehab/linko/internal/ui"
)
func newListCmd() *cobra.Command {
var remote bool
cmd := &cobra.Command{
Use: "list",
Aliases: []string{"ls"},
Short: "Show the hostnames you have published",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
ctx, cancel := signalContext(cmd.Context())
defer cancel()
return runList(ctx, remote)
},
}
cmd.Flags().BoolVar(&remote, "remote", false, "read the live routes from Cloudflare instead of the local config")
return cmd
}
func runList(ctx context.Context, remote bool) error {
cfg, client, err := loadClient()
if err != nil {
return err
}
if remote {
tunnelCfg, err := client.GetTunnelConfig(ctx, cfg.TunnelID)
if err != nil {
return err
}
rules := cloudflare.HostnameRules(tunnelCfg.Ingress)
if len(rules) == 0 {
ui.Info("No routes configured on tunnel %s.", cfg.TunnelName)
return nil
}
rows := make([][]string, 0, len(rules))
for _, r := range rules {
rows = append(rows, []string{"https://" + r.Hostname, "->", r.Service})
}
ui.Table(os.Stdout, []string{"URL", "", "TARGET"}, rows)
return nil
}
routes := cfg.SortedRoutes()
if len(routes) == 0 {
ui.Info("Nothing published yet. Try: linko 3000")
return nil
}
rows := make([][]string, 0, len(routes))
for _, r := range routes {
kind := "persistent"
if r.Ephemeral {
kind = "temporary"
}
rows = append(rows, []string{
r.Name,
"https://" + r.Hostname,
"->",
r.Service,
kind,
})
}
ui.Table(os.Stdout, []string{"NAME", "URL", "", "TARGET", "KIND"}, rows)
ui.Blank()
ui.Info("%d route(s) · tunnel %s · %s", len(routes), cfg.TunnelName, config.Path())
return nil
}
// routeNames is used for shell completion of `linko remove`.
func routeNames(prefix string) []string {
cfg, err := config.Load()
if err != nil {
return nil
}
out := []string{}
for _, r := range cfg.Routes {
if prefix == "" || strings.HasPrefix(r.Name, prefix) {
out = append(out, r.Name)
}
}
return out
}