/
configAdd.go
64 lines (58 loc) · 1.8 KB
/
configAdd.go
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
63
64
package cmd
import (
"fmt"
"os"
"strings"
"github.com/ddworken/hishtory/client/hctx"
"github.com/ddworken/hishtory/client/lib"
"github.com/spf13/cobra"
)
var configAddCmd = &cobra.Command{
Use: "config-add",
Short: "Add a config option",
GroupID: GROUP_ID_CONFIG,
Run: func(cmd *cobra.Command, args []string) {
lib.CheckFatalError(cmd.Help())
os.Exit(1)
},
}
var addCustomColumnsCmd = &cobra.Command{
Use: "custom-columns",
Aliases: []string{"custom-column"},
Short: "Add a custom column",
Args: cobra.ExactArgs(2),
Run: func(cmd *cobra.Command, args []string) {
columnName := args[0]
command := args[1]
ctx := hctx.MakeContext()
config := hctx.GetConf(ctx)
if config.CustomColumns == nil {
config.CustomColumns = make([]hctx.CustomColumnDefinition, 0)
}
for _, existingColumn := range config.CustomColumns {
if strings.EqualFold(existingColumn.ColumnName, columnName) {
lib.CheckFatalError(fmt.Errorf("cannot create a column named %#v since there is already one named %#v", existingColumn.ColumnName, columnName))
}
}
config.CustomColumns = append(config.CustomColumns, hctx.CustomColumnDefinition{ColumnName: columnName, ColumnCommand: command})
lib.CheckFatalError(hctx.SetConfig(config))
},
}
var addDisplayedColumnsCmd = &cobra.Command{
Use: "displayed-columns",
Aliases: []string{"displayed-column"},
Short: "Add a column to be displayed",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
ctx := hctx.MakeContext()
config := hctx.GetConf(ctx)
vals := args
config.DisplayedColumns = append(config.DisplayedColumns, vals...)
lib.CheckFatalError(hctx.SetConfig(config))
},
}
func init() {
rootCmd.AddCommand(configAddCmd)
configAddCmd.AddCommand(addCustomColumnsCmd)
configAddCmd.AddCommand(addDisplayedColumnsCmd)
}