Repository navigation
Expand file tree
/
Copy pathlist.go
More file actions
108 lines (97 loc) · 2.43 KB
/
Copy pathlist.go
File metadata and controls
108 lines (97 loc) · 2.43 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
8000
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
97
98
99
100
101
102
103
104
105
106
107
108
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 {
url := "https://" + r.Hostname
rows = append(rows, []string{ui.Link(url, url), "->", r.Service})
}
ui.Table(os.Stdout, []string{"URL", "", "TARGET"}, rows)
printClickHint()
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"
}
url := "https://" + r.Hostname
rows = append(rows, []string{
r.Name,
ui.Link(url, url),
"->",
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())
printClickHint()
return nil
}
// printClickHint tells the user how to follow the URLs just printed, when the
// terminal makes that non-obvious.
func printClickHint() {
if hint := ui.ClickHint(); hint != "" {
ui.Info("%s", hint)
}
}
// 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
}