-
Notifications
You must be signed in to change notification settings - Fork 37
/
cmd_root.go
109 lines (98 loc) · 4.36 KB
/
cmd_root.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
package main
import (
"fmt"
"os"
"strings"
"github.com/spf13/cobra"
libhoney "github.com/honeycombio/libhoney-go"
)
func commandRoot(cfg *libhoney.Config, filename *string, ciProvider *string, serviceName *string) *cobra.Command {
root := &cobra.Command{
Version: Version,
Use: "buildevents",
Short: "buildevents creates events for your CI builds",
Long: `
The buildevents executable creates Honeycomb events and tracing information
about your Continuous Integration builds.`,
PersistentPreRun: func(cmd *cobra.Command, args []string) {
quiet, _ := cmd.Flags().GetBool("quiet")
if cfg.IsClassic() {
// if we're in classic mode, we want to behave the same as we always have
if cfg.Dataset == "" {
cfg.Dataset = "buildevents"
}
if *serviceName != "" && !quiet {
fmt.Fprintf(os.Stderr, "WARN: classic mode ignores the service name parameter.\n")
}
} else {
// This is the non-classic behavior
if *serviceName != "" {
// service name was specified, so use it as the dataset
if cfg.Dataset != "" && !quiet {
// warn if we're going to ignore a specified dataset
fmt.Fprintf(os.Stderr, "WARN: service name was specified, dataset is ignored.\n")
}
trimmed := strings.TrimSpace(*serviceName)
if trimmed != *serviceName && !quiet {
fmt.Fprintf(os.Stderr, "WARN: service name contained leading or trailing whitespace, sending to '%s'.\n", trimmed)
}
cfg.Dataset = trimmed
} else {
// service_name was not specified
if cfg.Dataset == "" {
// neither was specified, so just use the default
cfg.Dataset = "buildevents"
}
}
}
},
}
root.PersistentFlags().StringVarP(&cfg.APIKey, "apikey", "k", "", "[env.BUILDEVENT_APIKEY] the Honeycomb authentication token")
if apikey, ok := os.LookupEnv("BUILDEVENT_APIKEY"); ok {
// https://github.com/spf13/viper/issues/461#issuecomment-366831834
root.PersistentFlags().Lookup("apikey").Value.Set(apikey)
}
root.PersistentFlags().StringVarP(&cfg.Dataset, "dataset", "d", "", "[env.BUILDEVENT_DATASET] the name of the Honeycomb dataset to which to send these events")
if dataset, ok := os.LookupEnv("BUILDEVENT_DATASET"); ok {
root.PersistentFlags().Lookup("dataset").Value.Set(dataset)
}
root.PersistentFlags().StringVarP(serviceName, "service_name", "n", "", "[env.BUILDEVENT_SERVICE_NAME] the name of the service to which to send these events; overrides dataset")
if service_name, ok := os.LookupEnv("BUILDEVENT_SERVICE_NAME"); ok {
root.PersistentFlags().Lookup("service_name").Value.Set(service_name)
}
root.PersistentFlags().StringVarP(&cfg.APIHost, "apihost", "a", "https://api.honeycomb.io", "[env.BUILDEVENT_APIHOST] the hostname for the Honeycomb API server to which to send this event")
if apihost, ok := os.LookupEnv("BUILDEVENT_APIHOST"); ok {
root.PersistentFlags().Lookup("apihost").Value.Set(apihost)
}
root.PersistentFlags().StringVarP(filename, "filename", "f", "", "[env.BUILDEVENT_FILE] the path of a text file holding arbitrary key=val pairs (multi-line-capable, logfmt style) to be added to the Honeycomb event")
if fname, ok := os.LookupEnv("BUILDEVENT_FILE"); ok {
root.PersistentFlags().Lookup("filename").Value.Set(fname)
}
root.PersistentFlags().StringVarP(ciProvider, "provider", "p", "", "[env.BUILDEVENT_CIPROVIDER] if unset, will inspect the environment to try to detect common CI providers.")
prov := os.Getenv("BUILDEVENT_CIPROVIDER")
if prov == "" {
if _, present := os.LookupEnv("TRAVIS"); present {
prov = providerTravis
} else if _, present := os.LookupEnv("CIRCLECI"); present {
prov = providerCircle
} else if _, present := os.LookupEnv("GITLAB_CI"); present {
prov = providerGitLab
} else if _, present := os.LookupEnv("BUILDKITE"); present {
prov = providerBuildkite
} else if _, present := os.LookupEnv("JENKINS-X"); present {
prov = providerJenkinsX
} else if _, present := os.LookupEnv("GOOGLE-CLOUD-BUILD"); present {
prov = providerGoogleCloudBuild
} else if _, present := os.LookupEnv("TF_BUILD"); present {
prov = providerAzurePipelines
} else if _, present := os.LookupEnv("GITHUB_ACTIONS"); present {
prov = providerGitHubActions
} else if _, present := os.LookupEnv("BITBUCKET_BUILD_NUMBER"); present {
prov = providerBitbucketPipelines
}
}
if prov != "" {
root.PersistentFlags().Lookup("provider").Value.Set(prov)
}
return root
}