forked from gotracker/gotracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathroot.go
More file actions
62 lines (53 loc) · 1.54 KB
/
Copy pathroot.go
File metadata and controls
62 lines (53 loc) · 1.54 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
package command
import (
"fmt"
"os"
"github.com/spf13/cobra"
"github.com/gotracker/gotracker/internal/config"
"github.com/gotracker/gotracker/internal/profiling"
)
type profilerCfg struct {
Profiler bool `pflag:"profiler" env:"profiler" usage:"enable profiler (and supporting http server)"`
ProfilerBindAddress string `pflag:"profiler-bind-addr" env:"profiler_bind_addr" usage:"profiler bind address (if enabled)"`
}
// flags
var profilerConfig = config.NewConfig(profilerCfg{
Profiler: false,
ProfilerBindAddress: "localhost:6060",
})
var rootCmd = &cobra.Command{
Use: "gotracker",
Short: "Gotracker is a tracked music player",
Long: `Gotracker is a tracked music player written entirely in Go`,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
if cfg := profilerConfig.Get(); cfg.Profiler {
profiling.Activate(cfg.ProfilerBindAddress)
}
return nil
},
Args: cobra.ExactArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
return cmd.Usage()
},
}
func Execute() {
args := os.Args[1:]
cmd, _, err := rootCmd.Find(args)
cmdExec := rootCmd
if err != nil || (cmd == rootCmd && len(os.Args) > 1) {
// assume play command if command argument not provided
cmdExec = playCmd
os.Args = append([]string{os.Args[0], "play"}, args...)
}
if err := cmdExec.Execute(); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
func init() {
if profiling.Allowed {
if err := profilerConfig.Overlay(config.StandardOverlays...).Update(rootCmd); err != nil {
panic(err)
}
}
}