11package ghcs
22
33import (
4- "fmt"
5- "log"
6- "strconv"
7- "strings"
8-
9- "github.com/lightstep/lightstep-tracer-go"
10- "github.com/opentracing/opentracing-go"
114 "github.com/spf13/cobra"
125)
136
147var version = "DEV" // Replaced in the release build process (by GoReleaser or Homebrew) by the git tag version number.
158
169func NewRootCmd (app * App ) * cobra.Command {
17- var lightstep string
18-
1910 root := & cobra.Command {
2011 Use : "ghcs" ,
2112 SilenceUsage : true , // don't print usage message after each error (see #80)
@@ -25,14 +16,8 @@ func NewRootCmd(app *App) *cobra.Command {
2516Running commands requires the GITHUB_TOKEN environment variable to be set to a
2617token to access the GitHub API with.` ,
2718 Version : version ,
28-
29- PersistentPreRunE : func (cmd * cobra.Command , args []string ) error {
30- return initLightstep (lightstep )
31- },
3219 }
3320
34- root .PersistentFlags ().StringVar (& lightstep , "lightstep" , "" , "Lightstep tracing endpoint (service:token@host:port)" )
35-
3621 root .AddCommand (newCodeCmd (app ))
3722 root .AddCommand (newCreateCmd (app ))
3823 root .AddCommand (newDeleteCmd (app ))
@@ -43,51 +28,3 @@ token to access the GitHub API with.`,
4328
4429 return root
4530}
46-
47- // initLightstep parses the --lightstep=service:token@host:port flag and
48- // enables tracing if non-empty.
49- func initLightstep (config string ) error {
50- if config == "" {
51- return nil
52- }
53-
54- cut := func (s , sep string ) (pre , post string ) {
55- if i := strings .Index (s , sep ); i >= 0 {
56- return s [:i ], s [i + len (sep ):]
57- }
58- return s , ""
59- }
60-
61- // Parse service:token@host:port.
62- serviceToken , hostPort := cut (config , "@" )
63- service , token := cut (serviceToken , ":" )
64- host , port := cut (hostPort , ":" )
65- portI , err := strconv .Atoi (port )
66- if err != nil {
67- return fmt .Errorf ("invalid Lightstep configuration: %s" , config )
68- }
69-
70- opentracing .SetGlobalTracer (lightstep .NewTracer (lightstep.Options {
71- AccessToken : token ,
72- Collector : lightstep.Endpoint {
73- Host : host ,
74- Port : portI ,
75- Plaintext : false ,
76- },
77- Tags : opentracing.Tags {
78- lightstep .ComponentNameKey : service ,
79- },
80- }))
81-
82- // Report failure to record traces.
83- lightstep .SetGlobalEventHandler (func (ev lightstep.Event ) {
84- switch ev := ev .(type ) {
85- case lightstep.EventStatusReport , lightstep.MetricEventStatusReport :
86- // ignore
87- default :
88- log .Printf ("[trace] %s" , ev )
89- }
90- })
91-
92- return nil
93- }
0 commit comments