cmd.New   A
last analyzed

Complexity

Conditions 2

Size

Total Lines 19
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 14
nop 0
dl 0
loc 19
rs 9.7
c 0
b 0
f 0
1
package cmd
2
3
import (
4
	"adrianolaselva.github.io/csvql/cmd/csvqlctl"
5
	"fmt"
6
	"github.com/spf13/cobra"
7
	"syscall"
8
)
9
10
const (
11
	commandBase = "csvql"
12
	bannerPrint = `csvql cli tools`
13
)
14
15
type CliBase interface {
16
	Execute() error
17
}
18
19
type cliBase struct {
20
	rootCmd *cobra.Command
21
}
22
23
func New() CliBase {
24
	var release = "latest"
25
	if value, ok := syscall.Getenv("VERSION"); ok {
26
		release = value
27
	}
28
29
	cmd := &cobra.Command{
30
		Use:     commandBase,
31
		Version: release,
32
		Long:    bannerPrint,
33
		CompletionOptions: cobra.CompletionOptions{
34
			DisableDefaultCmd:   false,
35
			DisableNoDescFlag:   false,
36
			DisableDescriptions: false,
37
			HiddenDefaultCmd:    true,
38
		},
39
	}
40
41
	return &cliBase{rootCmd: cmd}
42
}
43
44
func (c *cliBase) Execute() error {
45
	csvQlCtl, err := csvqlctl.New().Command()
46
	if err != nil {
47
		return fmt.Errorf("failed to execute command: %w", err)
48
	}
49
50
	c.rootCmd.AddCommand(csvQlCtl)
51
52
	if err := c.rootCmd.Execute(); err != nil {
53
		return fmt.Errorf("failed to execute command %w", err)
54
	}
55
56
	return nil
57
}
58