Skip to content

Commit 8f47c7d

Browse files
committed
time.Time support in CLI arguments
1 parent 46c4f3f commit 8f47c7d

File tree

1 file changed

+47
-5
lines changed

1 file changed

+47
-5
lines changed

pkg/cli/cli.go

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"github.com/pyroscope-io/pyroscope/pkg/build"
2020
"github.com/pyroscope-io/pyroscope/pkg/config"
2121
"github.com/pyroscope-io/pyroscope/pkg/convert"
22+
"github.com/pyroscope-io/pyroscope/pkg/dbmanager"
2223
"github.com/pyroscope-io/pyroscope/pkg/exec"
2324
"github.com/pyroscope-io/pyroscope/pkg/server"
2425
"github.com/pyroscope-io/pyroscope/pkg/storage"
@@ -33,6 +34,8 @@ import (
3334
"github.com/peterbourgon/ff/v3/ffcli"
3435
)
3536

37+
const timeFormat = "2006-01-02T15:04:05Z0700"
38+
3639
type arrayFlags []string
3740

3841
func (i *arrayFlags) String() string {
@@ -44,6 +47,26 @@ func (i *arrayFlags) Set(value string) error {
4447
return nil
4548
}
4649

50+
type timeFlag time.Time
51+
52+
func (tf *timeFlag) String() string {
53+
v := time.Time(*tf)
54+
return v.Format(timeFormat)
55+
}
56+
57+
func (tf *timeFlag) Set(value string) error {
58+
t2, err := time.Parse(timeFormat, value)
59+
if err != nil {
60+
return err
61+
}
62+
63+
t := (*time.Time)(tf)
64+
b, _ := t2.MarshalBinary()
65+
t.UnmarshalBinary(b)
66+
67+
return nil
68+
}
69+
4770
// this is mostly reflection magic
4871
func populateFlagSet(obj interface{}, flagSet *flag.FlagSet) {
4972
v := reflect.ValueOf(obj).Elem()
@@ -80,6 +103,10 @@ func populateFlagSet(obj interface{}, flagSet *flag.FlagSet) {
80103
case reflect.TypeOf(true):
81104
val := fieldV.Addr().Interface().(*bool)
82105
flagSet.BoolVar(val, nameVal, defaultValStr == "true", descVal)
106+
case reflect.TypeOf(time.Time{}):
107+
valTime := fieldV.Addr().Interface().(*time.Time)
108+
val := (*timeFlag)(valTime)
109+
flagSet.Var(val, nameVal, descVal)
83110
case reflect.TypeOf(time.Second):
84111
val := fieldV.Addr().Interface().(*time.Duration)
85112
var defaultVal time.Duration
@@ -147,18 +174,20 @@ func printUsage(c *ffcli.Command) string {
147174

148175
func Start(cfg *config.Config) error {
149176
var (
150-
rootFlagSet = flag.NewFlagSet("pyroscope", flag.ExitOnError)
151-
agentFlagSet = flag.NewFlagSet("pyroscope agent", flag.ExitOnError)
152-
serverFlagSet = flag.NewFlagSet("pyroscope server", flag.ExitOnError)
153-
convertFlagSet = flag.NewFlagSet("pyroscope convert", flag.ExitOnError)
154-
execFlagSet = flag.NewFlagSet("pyroscope exec", flag.ExitOnError)
177+
rootFlagSet = flag.NewFlagSet("pyroscope", flag.ExitOnError)
178+
agentFlagSet = flag.NewFlagSet("pyroscope agent", flag.ExitOnError)
179+
serverFlagSet = flag.NewFlagSet("pyroscope server", flag.ExitOnError)
180+
convertFlagSet = flag.NewFlagSet("pyroscope convert", flag.ExitOnError)
181+
execFlagSet = flag.NewFlagSet("pyroscope exec", flag.ExitOnError)
182+
dbmanagerFlagSet = flag.NewFlagSet("pyroscope dbmanager", flag.ExitOnError)
155183
)
156184

157185
populateFlagSet(cfg, rootFlagSet)
158186
populateFlagSet(&cfg.Agent, agentFlagSet)
159187
populateFlagSet(&cfg.Server, serverFlagSet)
160188
populateFlagSet(&cfg.Convert, convertFlagSet)
161189
populateFlagSet(&cfg.Exec, execFlagSet)
190+
populateFlagSet(&cfg.DbManager, dbmanagerFlagSet)
162191

163192
options := []ff.Option{
164193
ff.WithConfigFileParser(ffyaml.Parser),
@@ -203,6 +232,15 @@ func Start(cfg *config.Config) error {
203232
FlagSet: execFlagSet,
204233
}
205234

235+
dbmanagerCmd := &ffcli.Command{
236+
UsageFunc: printUsage,
237+
Options: options,
238+
Name: "dbmanager",
239+
ShortUsage: "pyroscope dbmanager [flags] <args>",
240+
ShortHelp: "tools for managing database",
241+
FlagSet: dbmanagerFlagSet,
242+
}
243+
206244
rootCmd := &ffcli.Command{
207245
UsageFunc: printUsage,
208246
Options: options,
@@ -214,6 +252,7 @@ func Start(cfg *config.Config) error {
214252
// convertCmd,
215253
serverCmd,
216254
execCmd,
255+
dbmanagerCmd,
217256
},
218257
}
219258

@@ -248,6 +287,9 @@ func Start(cfg *config.Config) error {
248287

249288
return exec.Cli(cfg, args)
250289
}
290+
dbmanagerCmd.Exec = func(_ context.Context, args []string) error {
291+
return dbmanager.Cli(cfg, args)
292+
}
251293
rootCmd.Exec = func(_ context.Context, args []string) error {
252294
if cfg.Version || len(args) > 0 && args[0] == "version" {
253295
fmt.Println(gradientBanner())

0 commit comments

Comments
 (0)