Skip to content
File

Blob: cmd/client/lightweight.go

go63 lines
1package client
2 
3import (
4 "context"
5 "fmt"
6 "io"
7 "os"
8 "strings"
9 
10 clientruntime "go.miragespace.co/specter/internal/client"
11 
12 "github.com/urfave/cli/v3"
13)
14 
15func lightweightFlags() []cli.Flag {
16 return []cli.Flag{
17 &cli.StringFlag{
18 Name: "apex",
19 Required: true,
20 Usage: "TLS authority, bootstrap endpoint, and generated URL port; the server reports the URL hostname",
21 },
22 }
23}
24 
25func loadTunnelToken(path, env string, envSet bool) (string, error) {
26 if (path != "") == envSet {
27 return "", fmt.Errorf("specify exactly one of --token-file or SPECTER_TUNNEL_TOKEN")
28 }
29 data := []byte(env)
30 if path != "" {
31 f, err := os.Open(path)
32 if err != nil {
33 return "", err
34 }
35 defer f.Close()
36 data, err = io.ReadAll(io.LimitReader(f, 4097))
37 if err != nil {
38 return "", err
39 }
40 }
41 if len(data) > 4096 {
42 return "", fmt.Errorf("tunnel token input exceeds 4 KiB")
43 }
44 token := strings.TrimSpace(string(data))
45 if token == "" {
46 return "", fmt.Errorf("tunnel token is empty")
47 }
48 return token, nil
49}
50 
51func runLightweight(ctx context.Context, cmd *cli.Command, token string) error {
52 if cmd.Args().Len() != 1 {
53 return fmt.Errorf("expected exactly one target URL")
54 }
55 return clientruntime.RunLightweight(ctx, clientruntime.LightweightOptions{
56 Options: runtimeOptions(cmd),
57 Apex: cmd.String("apex"),
58 Target: cmd.Args().First(),
59 Token: token,
60 Output: cmd.Root().Writer,
61 })
62}