forked from subtrace/subtrace
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsubtrace.go
More file actions
68 lines (58 loc) · 1.66 KB
/
subtrace.go
File metadata and controls
68 lines (58 loc) · 1.66 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
// Copyright (c) Subtrace, Inc.
// SPDX-License-Identifier: BSD-3-Clause
package main
import (
"context"
"errors"
"flag"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/peterbourgon/ff/v3/ffcli"
"subtrace.dev/cmd/proxy"
"subtrace.dev/cmd/run"
"subtrace.dev/cmd/version"
"subtrace.dev/cmd/worker"
)
func main() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
c := new(ffcli.Command)
c.Name = filepath.Base(os.Args[0])
c.ShortUsage = "subtrace <command>"
c.Subcommands = append(c.Subcommands, run.NewCommand())
c.Subcommands = append(c.Subcommands, proxy.NewCommand())
c.Subcommands = append(c.Subcommands, worker.NewCommand())
c.Subcommands = append(c.Subcommands, version.NewCommand())
c.FlagSet = flag.NewFlagSet("subtrace", flag.ContinueOnError)
c.FlagSet.SetOutput(os.Stdout)
c.Exec = func(ctx context.Context, args []string) error {
text := c.UsageFunc(c)
if len(args) == 0 {
text += run.ExtraHelp()
}
fmt.Fprintf(os.Stdout, "%s\n", text)
// Using os.Args and not args[0] because `subtrace -- curl` calls Exec with
// args={"curl"}, not args={"--", "curl"}. The real error is the lack of
// the run subcommand (explicit is better than implicit).
if len(os.Args) >= 2 {
return fmt.Errorf("unknown command %q", os.Args[1])
}
return nil
}
switch err := c.Parse(os.Args[1:]); {
case err == nil:
case errors.Is(err, flag.ErrHelp):
return
case strings.Contains(err.Error(), "flag provided but not defined"):
os.Exit(2)
default:
fmt.Fprintf(os.Stderr, "subtrace: error: %v\n", err)
os.Exit(1)
}
if err := c.Run(ctx); err != nil {
fmt.Fprintf(os.Stderr, "subtrace: error: %v\n", err)
os.Exit(1)
}
}