Repository navigation
Expand file tree
/
Copy pathroot.go
More file actions
144 lines (128 loc) · 3.32 KB
/
Copy pathroot.go
File metadata and controls
144 lines (128 loc) · 3.32 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// Package cmd wires up linko's command line interface.
package cmd
import (
"context"
"errors"
"fmt"
"os"
"os/signal"
"syscall"
"github.com/spf13/cobra"
"github.com/Devehab/linko/cloudflare"
"github.com/Devehab/linko/config"
"github.com/Devehab/linko/internal/target"
"github.com/Devehab/linko/internal/ui"
)
// Version is set from main at build time.
var Version = "dev"
var noColor bool
// NewRootCmd builds the command tree.
func NewRootCmd() *cobra.Command {
root := &cobra.Command{
Use: "linko",
Short: "Expose a local port on a public HTTPS URL via Cloudflare Tunnel",
Long: `linko turns a service running on your machine into a public HTTPS URL
using your own Cloudflare account and domain.
linko init one-time setup
linko 3000 publish localhost:3000 on a random subdomain
linko 3000 -n crm publish it on crm.<your base domain>`,
SilenceUsage: true,
SilenceErrors: true,
Version: Version,
PersistentPreRun: func(cmd *cobra.Command, args []string) {
if noColor {
ui.SetColor(false)
}
},
}
root.PersistentFlags().BoolVar(&noColor, "no-color", false, "disable coloured output")
root.SetVersionTemplate("linko {{.Version}}\n")
root.AddCommand(
newInitCmd(),
newStartCmd(),
newListCmd(),
newRemoveCmd(),
newStatusCmd(),
newDoctorCmd(),
newDocsCmd(),
newStopCmd(),
newPsCmd(),
newServiceCmd(),
)
return root
}
// Execute runs linko and converts errors into a clean exit.
func Execute(version string) {
Version = version
cloudflare.UserAgent = "linko/" + version
root := NewRootCmd()
root.SetArgs(NormalizeArgs(os.Args[1:], commandNames(root)))
if err := root.Execute(); err != nil {
ui.Blank()
fmt.Fprintf(os.Stderr, "%s %v\n", ui.Red("Error:"), err)
if errors.Is(err, config.ErrNotInitialized) {
fmt.Fprintf(os.Stderr, "\nRun %s to get started.\n", ui.Bold("linko init"))
}
os.Exit(1)
}
}
func commandNames(root *cobra.Command) map[string]bool {
names := map[string]bool{
"help": true,
"completion"
6657
: true,
}
for _, c := range root.Commands() {
names[c.Name()] = true
for _, a := range c.Aliases {
names[a] = true
}
}
return names
}
// NormalizeArgs rewrites `linko 3000 …` into `linko start 3000 …` so a port can
// be used as the default command.
func NormalizeArgs(args []string, known map[string]bool) []string {
if len(args) == 0 {
return args
}
first := args[0]
if known[first] {
return args
}
if len(first) > 0 && first[0] == '-' {
return args
}
if target.Looks(first) {
return append([]string{"start"}, args...)
}
return args
}
// signalContext cancels when the user hits Ctrl+C.
func signalContext(parent context.Context) (context.Context, context.CancelFunc) {
ctx, cancel := context.WithCancel(parent)
ch := make(chan os.Signal, 1)
signal.Notify(ch, os.Interrupt, syscall.SIGTERM)
go func() {
select {
case <-ch:
cancel()
case <-ctx.Done():
}
signal.Stop(ch)
}()
return ctx, cancel
}
// loadClient loads the config and returns a ready Cloudflare client.
func loadClient() (*config.Config, *cloudflare.Client, error) {
cfg, err := config.Load()
if err != nil {
return nil, nil, err
}
if err := cfg.Validate(); err != nil {
return nil, nil, err
}
client := cloudflare.New(cfg.APIToken)
client.AccountID = cfg.AccountID
client.ZoneID = cfg.ZoneID
return cfg, client, nil
}