-
Notifications
You must be signed in to change notification settings - Fork 11
/
main.go
117 lines (90 loc) · 3.66 KB
/
main.go
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
package main
import (
"fmt"
"net/http"
"os"
flag "github.com/jessevdk/go-flags"
)
// BuildID is set by CI
var BuildID string = "dev"
// UserAgent is what gets included in all http requests to the api
var UserAgent string
type Options struct {
WriteKey string `short:"k" long:"writekey" env:"HONEYCOMB_API_KEY" description:"Honeycomb configuration key from https://ui.honeycomb.io/<team>/environments/<environment>/api_keys"`
Dataset string `short:"d" long:"dataset" description:"Honeycomb dataset name from https://ui.honeycomb.io/dashboard (use __all__ for environment-wide markers)"`
APIHost string `long:"api_host" hidden:"true" default:"https://api.honeycomb.io/"`
AuthorizationHeader string `long:"authorization-header" hidden:"true"`
}
var options Options
var parser = flag.NewParser(&options, flag.Default)
var client = http.Client{}
var usage = `-k <writekey> -d <dataset> COMMAND [other flags]
honeymarker is a command line utility for manipulating markers in your
Honeycomb dataset.
Except for the Version command, Writekey and Dataset are both required. Most commands have additional
arguments.
'honeymarker COMMAND --help' will print command-specific flags`
// setVersion sets the internal version ID and updates libhoney's user-agent
func setVersionUserAgent() {
UserAgent = fmt.Sprintf("honeymarker/%s", BuildID)
}
func checkRequiredFlags() {
// In order to do the above, we can't use the "required" flag on the options, so we have to
// check them manually.
if options.WriteKey == "" {
fmt.Println("the required flag `-k, --writekey' was not specified")
os.Exit(1)
}
if options.Dataset == "" {
fmt.Println("the required flag `-d, --dataset' was not specified")
os.Exit(1)
}
}
func main() {
setVersionUserAgent()
parser.AddCommand("add", "Add a new marker",
`add creates a new marker with the specified attributes.
All parameters to add are optional.
If start_time is missing, the marker will be assigned the current time.
It is highly recommended that you fill in either message or type.
All markers of the same type will be shown with the same color in the UI.
The message will be visible above an individual marker.
If a URL is specified along with a message, the message will be shown
as a link in the UI, and clicking it will take you to the URL.`,
&AddCommand{})
parser.AddCommand("list", "List all markers",
`List all markers for the specified dataset.
Returned markers will be displayed in tabular format by default,
ordered by the marker's start time.`,
&ListCommand{})
parser.AddCommand("rm", "Delete a marker",
`Delete the marker in the specified dataset, as identified by its ID.
Marker IDs are available via the 'list' command.`,
&RmCommand{})
parser.AddCommand("update", "Update a marker",
`Update an existing marker in the specified dataset with the specified options.
The marker ID is required (available via the 'list' command). All other
parameters are optional, though an 'update' will be a no-op unless a parameter
is specified with a new value.`,
&UpdateCommand{})
parser.AddCommand("version", "Print tool version",
`Prints the version number of this tool and exits.`,
&VersionCommand{})
// run whichever command is chosen
parser.Usage = usage
if _, err := parser.Parse(); err != nil {
if flagErr, ok := err.(*flag.Error); ok {
if flagErr.Type == flag.ErrHelp {
// asking for help isn't a failed run.
os.Exit(0)
}
if flagErr.Type == flag.ErrCommandRequired ||
flagErr.Type == flag.ErrUnknownCommand ||
flagErr.Type == flag.ErrUnknownFlag ||
flagErr.Type == flag.ErrRequired {
fmt.Println(" run 'honeymarker --help' for full usage details")
}
}
os.Exit(1)
}
}