Skip to content

Commit 32e69d2

Browse files
authored
Minor improvements (#140)
- add more command examples and format with color - resolve build date as mod time of binary by default Signed-off-by: Ondrej Fabry <[email protected]>
1 parent f93fc7a commit 32e69d2

File tree

7 files changed

+79
-42
lines changed

7 files changed

+79
-42
lines changed

Diff for: .gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
# cmd
1313
cmd/binapi-generator/binapi-generator
1414
cmd/vpp-proxy/vpp-proxy
15+
cmd/govpp/govpp
1516

1617
# examples
1718
examples/api-trace/api-trace

Diff for: cmd/govpp/cmd.go

+25-31
Original file line numberDiff line numberDiff line change
@@ -23,65 +23,59 @@ import (
2323
)
2424

2525
const logo = `
26-
<fg=lightCyan> _________ ___ _________________ </>
27-
<fg=lightCyan> __ ____/_______ | / /__ __ \__ __ \ </>
28-
<fg=lightCyan> _ / __ _ __ \_ | / /__ /_/ /_ /_/ / </> <fg=blue;op=bold>%s</>
29-
<fg=lightCyan> / /_/ / / /_/ /_ |/ / _ ____/_ ____/ </> <lightBlue>%s</>
30-
<fg=lightCyan> \____/ \____/_____/ /_/ /_/ </> <blue>%s</>
26+
<fg=lightCyan;bg=black;op=bold> ______ _ _ _____ _____ </> <fg=lightWhite;op=bold>%s</>
27+
<fg=lightCyan;bg=black;op=bold> | ____ _____ \ / |_____] |_____] </> <fg=lightBlue>%s</>
28+
<fg=lightCyan;bg=black;op=bold> |_____| [_____] \/ | | </> <fg=blue>%s</>
29+
<fg=lightCyan;bg=black;op=bold> </>
3130
`
3231

33-
func Execute() {
34-
cli, err := NewCli()
35-
if err != nil {
36-
logrus.Fatalf("CLI init error: %v", err)
37-
}
38-
root := newRootCmd(cli)
39-
40-
if err := root.Execute(); err != nil {
41-
logrus.Fatalf("ERROR: %v", err)
42-
}
43-
}
44-
4532
func newRootCmd(cli Cli) *cobra.Command {
4633
var (
4734
glob GlobalOptions
4835
)
36+
4937
cmd := &cobra.Command{
50-
Use: "govpp [OPTIONS] COMMAND",
51-
Short: "GoVPP CLI",
52-
Long: color.Sprintf(logo, version.Short(), version.BuiltBy(), version.BuildTime()),
53-
Version: version.String(),
54-
SilenceUsage: true,
55-
SilenceErrors: true,
56-
TraverseChildren: true,
57-
CompletionOptions: cobra.CompletionOptions{HiddenDefaultCmd: true},
38+
Use: "govpp [OPTIONS] COMMAND",
39+
Short: "GoVPP CLI tool",
40+
Long: color.Sprintf(logo, version.Short(), version.BuiltBy(), version.BuildTime()),
41+
Version: version.String(),
5842
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
5943
InitOptions(cli, &glob)
6044
logrus.Tracef("global options: %+v", glob)
61-
6245
logrus.Tracef("args: %+v", args)
63-
6446
return nil
6547
},
48+
SilenceUsage: true,
49+
SilenceErrors: true,
50+
TraverseChildren: true,
51+
CompletionOptions: cobra.CompletionOptions{
52+
HiddenDefaultCmd: true,
53+
},
6654
}
6755

56+
// Setup options
6857
cmd.Flags().SortFlags = false
6958
cmd.PersistentFlags().SortFlags = false
7059

7160
// Global options
7261
glob.InstallFlags(cmd.PersistentFlags())
7362

63+
// Version option
64+
cmd.InitDefaultVersionFlag()
65+
cmd.Flags().Lookup("version").Shorthand = ""
66+
// Help option
67+
cmd.InitDefaultHelpFlag()
68+
cmd.Flags().Lookup("help").Hidden = true
69+
70+
// Commands
7471
cmd.AddCommand(
7572
newGenerateCmd(cli),
7673
newVppapiCmd(cli),
7774
newHttpCmd(cli),
7875
newCliCommand(cli),
7976
)
8077

81-
cmd.InitDefaultVersionFlag()
82-
cmd.InitDefaultHelpFlag()
83-
cmd.Flags().Lookup("help").Hidden = true
84-
78+
// Help command
8579
cmd.InitDefaultHelpCmd()
8680
for _, c := range cmd.Commands() {
8781
if c.Name() == "help" {

Diff for: cmd/govpp/cmd_cli.go

+6-5
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"os"
2323
"strings"
2424

25+
"github.com/gookit/color"
2526
"github.com/sirupsen/logrus"
2627
"github.com/spf13/cobra"
2728

@@ -36,16 +37,16 @@ import (
3637
// - try several ways to connect to VPP if not specified
3738

3839
const exampleCliCommand = `
39-
# Execute 'show version' command
40+
<note># Execute 'show version' command</>
4041
govpp cli show version
4142
42-
# Enter REPL mode to send commands interactively
43+
<note># Enter REPL mode to send commands interactively</>
4344
govpp cli
4445
45-
# Read CLI command(s) from stdin
46+
<note># Read CLI command(s) from stdin</>
4647
echo "show errors" | govpp cli
4748
48-
# Execute commands and write output to file
49+
<note># Execute commands and write output to file</>
4950
govpp cli -o cli.log show version
5051
`
5152

@@ -70,7 +71,7 @@ func newCliCommand(Cli) *cobra.Command {
7071
Aliases: []string{"c"},
7172
Short: "Send CLI via VPP API",
7273
Long: "Send VPP CLI command(s) via VPP API",
73-
Example: exampleCliCommand,
74+
Example: color.Sprint(exampleCliCommand),
7475
DisableFlagsInUseLine: true,
7576
RunE: func(cmd *cobra.Command, args []string) error {
7677
opts.Stdin = cmd.InOrStdin()

Diff for: cmd/govpp/cmd_vppapi.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -26,25 +26,25 @@ import (
2626
)
2727

2828
const exampleVppapi = `
29-
<gray># Specify input source of VPP API</>
29+
<note># Specify input source of VPP API</>
3030
govpp vppapi COMMAND [INPUT]
3131
govpp vppapi COMMAND ./vpp
3232
govpp vppapi COMMAND /usr/share/vpp/api
3333
govpp vppapi COMMAND vppapi.tar.gz
3434
govpp vppapi COMMAND http://github.com/FDio/vpp.git
3535
36-
<gray># List VPP API contents</>
36+
<note># List VPP API contents</>
3737
govpp vppapi ls [INPUT]
3838
govpp vppapi ls --show-contents
3939
40-
<gray># Export VPP API files</>
40+
<note># Export VPP API files</>
4141
govpp vppapi export [INPUT] --output vppapi
4242
govpp vppapi export [INPUT] --output vppapi.tar.gz
4343
44-
<gray># Lint VPP API definitions</>
44+
<note># Lint VPP API definitions</>
4545
govpp vppapi lint [INPUT]
4646
47-
<gray># Compare VPP API schemas</>
47+
<note># Compare VPP API schemas</>
4848
govpp vppapi diff [INPUT] --against http://github.com/FDio/vpp.git
4949
`
5050

Diff for: cmd/govpp/cmd_vppapi_diff.go

+6
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ import (
2929
// - table format for differences
3030
// - option to exit with non-zero status on breaking changes
3131

32+
const exampleVppApiDiffCommand = `
33+
<note># Compare VPP API schemas</>
34+
govpp vppapi diff . --against https://github.com/FDio/vpp
35+
`
36+
3237
type VppApiDiffCmdOptions struct {
3338
*VppApiCmdOptions
3439

@@ -46,6 +51,7 @@ func newVppApiDiffCmd(cli Cli, vppapiOpts *VppApiCmdOptions) *cobra.Command {
4651
Aliases: []string{"cmp", "compare"},
4752
Short: "Compare VPP API schemas",
4853
Long: "Compares two VPP API schemas and lists the differences.",
54+
Example: color.Sprint(exampleVppApiDiffCommand),
4955
Args: cobra.MaximumNArgs(1),
5056
RunE: func(cmd *cobra.Command, args []string) error {
5157
if len(args) > 0 {

Diff for: cmd/govpp/main.go

+16
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,22 @@
1414

1515
package main
1616

17+
import (
18+
"github.com/sirupsen/logrus"
19+
)
20+
1721
func main() {
1822
Execute()
1923
}
24+
25+
func Execute() {
26+
cli, err := NewCli()
27+
if err != nil {
28+
logrus.Fatalf("CLI init error: %v", err)
29+
}
30+
root := newRootCmd(cli)
31+
32+
if err := root.Execute(); err != nil {
33+
logrus.Fatalf("ERROR: %v", err)
34+
}
35+
}

Diff for: internal/version/version.go

+20-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ package version
1717

1818
import (
1919
"fmt"
20+
"os"
2021
"runtime"
2122
"strconv"
2223
"time"
@@ -56,11 +57,29 @@ var (
5657
func init() {
5758
buildstampInt64, _ := strconv.ParseInt(buildStamp, 10, 64)
5859
if buildstampInt64 == 0 {
59-
buildstampInt64 = time.Now().Unix()
60+
modTime, _ := binaryModTime()
61+
buildstampInt64 = modTime.Unix()
6062
}
6163
buildDate = time.Unix(buildstampInt64, 0)
6264
}
6365

66+
func binaryModTime() (time.Time, error) {
67+
// Get the path of the currently running binary
68+
binaryPath, err := os.Executable()
69+
if err != nil {
70+
return time.Time{}, fmt.Errorf("unable to get current binary path: %w", err)
71+
}
72+
73+
// Get the file info for the binary
74+
fileInfo, err := os.Stat(binaryPath)
75+
if err != nil {
76+
return time.Time{}, fmt.Errorf("unable to get file info for binary: %w", err)
77+
}
78+
79+
// Return the modification time
80+
return fileInfo.ModTime(), nil
81+
}
82+
6483
func Version() string {
6584
return version
6685
}

0 commit comments

Comments
 (0)