2022-11-14 03:26:23 +01:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
2023-12-19 05:32:11 +01:00
|
|
|
"os"
|
2022-11-14 03:26:23 +01:00
|
|
|
|
|
|
|
"github.com/ddworken/hishtory/client/hctx"
|
|
|
|
"github.com/ddworken/hishtory/client/lib"
|
2024-08-11 21:19:41 +02:00
|
|
|
|
2022-11-14 03:26:23 +01:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
)
|
|
|
|
|
|
|
|
var configDeleteCmd = &cobra.Command{
|
2022-11-15 05:18:22 +01:00
|
|
|
Use: "config-delete",
|
2023-12-20 00:03:55 +01:00
|
|
|
Aliases: []string{"config-remove"},
|
2022-11-15 05:18:22 +01:00
|
|
|
Short: "Delete a config option",
|
|
|
|
GroupID: GROUP_ID_CONFIG,
|
2022-11-14 03:26:23 +01:00
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
2022-11-27 20:59:06 +01:00
|
|
|
lib.CheckFatalError(cmd.Help())
|
2023-12-19 05:32:11 +01:00
|
|
|
os.Exit(1)
|
2022-11-14 03:26:23 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var deleteCustomColumnsCmd = &cobra.Command{
|
2023-12-19 18:34:12 +01:00
|
|
|
Use: "custom-columns",
|
|
|
|
Aliases: []string{"custom-column"},
|
|
|
|
Short: "Delete a custom column",
|
|
|
|
Args: cobra.ExactArgs(1),
|
2022-11-14 03:26:23 +01:00
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
|
ctx := hctx.MakeContext()
|
|
|
|
config := hctx.GetConf(ctx)
|
|
|
|
columnName := args[0]
|
|
|
|
if config.CustomColumns == nil {
|
|
|
|
log.Fatalf("Did not find a column with name %#v to delete (current columns = %#v)", columnName, config.CustomColumns)
|
|
|
|
}
|
2023-11-12 11:05:51 +01:00
|
|
|
// Delete it from the list of custom columns
|
2022-11-14 03:26:23 +01:00
|
|
|
newColumns := make([]hctx.CustomColumnDefinition, 0)
|
2023-11-12 11:05:51 +01:00
|
|
|
foundColumnToDelete := false
|
2022-11-14 03:26:23 +01:00
|
|
|
for _, c := range config.CustomColumns {
|
2023-11-12 11:05:51 +01:00
|
|
|
if c.ColumnName == columnName {
|
|
|
|
foundColumnToDelete = true
|
|
|
|
} else {
|
2022-11-14 03:26:23 +01:00
|
|
|
newColumns = append(newColumns, c)
|
|
|
|
}
|
|
|
|
}
|
2023-11-12 11:05:51 +01:00
|
|
|
if !foundColumnToDelete {
|
2022-11-14 03:26:23 +01:00
|
|
|
log.Fatalf("Did not find a column with name %#v to delete (current columns = %#v)", columnName, config.CustomColumns)
|
|
|
|
}
|
|
|
|
config.CustomColumns = newColumns
|
2023-11-12 11:05:51 +01:00
|
|
|
// And also delete it from the list of displayed columns
|
|
|
|
newDisplayedColumns := make([]string, 0)
|
|
|
|
for _, c := range config.DisplayedColumns {
|
|
|
|
if c != columnName {
|
|
|
|
newDisplayedColumns = append(newDisplayedColumns, c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
config.DisplayedColumns = newDisplayedColumns
|
2022-11-14 03:26:23 +01:00
|
|
|
lib.CheckFatalError(hctx.SetConfig(config))
|
|
|
|
},
|
|
|
|
}
|
2024-08-11 21:19:41 +02:00
|
|
|
|
2022-11-14 03:26:23 +01:00
|
|
|
var deleteDisplayedColumnCommand = &cobra.Command{
|
2023-12-19 18:34:12 +01:00
|
|
|
Use: "displayed-columns",
|
|
|
|
Aliases: []string{"displayed-column"},
|
|
|
|
Short: "Delete a displayed column",
|
|
|
|
Args: cobra.MinimumNArgs(1),
|
2022-11-14 03:26:23 +01:00
|
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
|
|
ctx := hctx.MakeContext()
|
|
|
|
config := hctx.GetConf(ctx)
|
|
|
|
deletedColumns := args
|
|
|
|
newColumns := make([]string, 0)
|
|
|
|
for _, c := range config.DisplayedColumns {
|
|
|
|
isDeleted := false
|
|
|
|
for _, d := range deletedColumns {
|
|
|
|
if c == d {
|
|
|
|
isDeleted = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !isDeleted {
|
|
|
|
newColumns = append(newColumns, c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
config.DisplayedColumns = newColumns
|
|
|
|
lib.CheckFatalError(hctx.SetConfig(config))
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
rootCmd.AddCommand(configDeleteCmd)
|
|
|
|
configDeleteCmd.AddCommand(deleteCustomColumnsCmd)
|
2022-11-15 05:55:10 +01:00
|
|
|
configDeleteCmd.AddCommand(deleteDisplayedColumnCommand)
|
2022-11-14 03:26:23 +01:00
|
|
|
}
|