2023-12-07 11:21:04 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2023-12-15 14:27:22 +00:00
|
|
|
"os"
|
|
|
|
"strings"
|
2023-12-07 11:21:04 +00:00
|
|
|
|
|
|
|
"github.com/fatih/color"
|
2023-12-15 14:27:22 +00:00
|
|
|
"github.com/hexops/gotextdiff"
|
|
|
|
"github.com/hexops/gotextdiff/myers"
|
|
|
|
"github.com/hexops/gotextdiff/span"
|
2023-12-07 11:21:04 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
|
|
|
"github.com/crowdsecurity/go-cs-lib/coalesce"
|
|
|
|
|
|
|
|
"github.com/crowdsecurity/crowdsec/cmd/crowdsec-cli/require"
|
|
|
|
"github.com/crowdsecurity/crowdsec/pkg/cwhub"
|
|
|
|
)
|
|
|
|
|
|
|
|
type cliHelp struct {
|
|
|
|
// Example is required, the others have a default value
|
|
|
|
// generated from the item type
|
|
|
|
use string
|
|
|
|
short string
|
|
|
|
long string
|
|
|
|
example string
|
|
|
|
}
|
|
|
|
|
2023-12-11 09:32:54 +00:00
|
|
|
type cliItem struct {
|
2023-12-07 11:21:04 +00:00
|
|
|
name string // plural, as used in the hub index
|
|
|
|
singular string
|
|
|
|
oneOrMore string // parenthetical pluralizaion: "parser(s)"
|
|
|
|
help cliHelp
|
|
|
|
installHelp cliHelp
|
|
|
|
removeHelp cliHelp
|
|
|
|
upgradeHelp cliHelp
|
|
|
|
inspectHelp cliHelp
|
|
|
|
inspectDetail func(item *cwhub.Item) error
|
|
|
|
listHelp cliHelp
|
|
|
|
}
|
|
|
|
|
2023-12-15 14:27:22 +00:00
|
|
|
func (cli cliItem) NewCommand() *cobra.Command {
|
2023-12-07 11:21:04 +00:00
|
|
|
cmd := &cobra.Command{
|
2023-12-15 14:27:22 +00:00
|
|
|
Use: coalesce.String(cli.help.use, fmt.Sprintf("%s <action> [item]...", cli.name)),
|
|
|
|
Short: coalesce.String(cli.help.short, fmt.Sprintf("Manage hub %s", cli.name)),
|
|
|
|
Long: cli.help.long,
|
|
|
|
Example: cli.help.example,
|
2023-12-07 11:21:04 +00:00
|
|
|
Args: cobra.MinimumNArgs(1),
|
2023-12-15 14:27:22 +00:00
|
|
|
Aliases: []string{cli.singular},
|
2023-12-07 11:21:04 +00:00
|
|
|
DisableAutoGenTag: true,
|
|
|
|
}
|
|
|
|
|
2024-02-01 16:26:06 +00:00
|
|
|
cmd.AddCommand(cli.newInstallCmd())
|
|
|
|
cmd.AddCommand(cli.newRemoveCmd())
|
|
|
|
cmd.AddCommand(cli.newUpgradeCmd())
|
|
|
|
cmd.AddCommand(cli.newInspectCmd())
|
|
|
|
cmd.AddCommand(cli.newListCmd())
|
2023-12-07 11:21:04 +00:00
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2024-02-01 16:26:06 +00:00
|
|
|
func (cli cliItem) install(args []string, downloadOnly bool, force bool, ignoreError bool) error {
|
2023-12-19 16:20:09 +00:00
|
|
|
hub, err := require.Hub(csConfig, require.RemoteHub(csConfig), log.StandardLogger())
|
2023-12-07 11:21:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, name := range args {
|
2023-12-15 14:27:22 +00:00
|
|
|
item := hub.GetItem(cli.name, name)
|
2023-12-07 11:21:04 +00:00
|
|
|
if item == nil {
|
2023-12-15 14:27:22 +00:00
|
|
|
msg := suggestNearestMessage(hub, cli.name, name)
|
2023-12-07 11:21:04 +00:00
|
|
|
if !ignoreError {
|
|
|
|
return fmt.Errorf(msg)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Errorf(msg)
|
|
|
|
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := item.Install(force, downloadOnly); err != nil {
|
|
|
|
if !ignoreError {
|
|
|
|
return fmt.Errorf("error while installing '%s': %w", item.Name, err)
|
|
|
|
}
|
2024-01-03 09:55:41 +00:00
|
|
|
|
2023-12-07 11:21:04 +00:00
|
|
|
log.Errorf("Error while installing '%s': %s", item.Name, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Infof(ReloadMessage())
|
2024-01-03 09:55:41 +00:00
|
|
|
|
2023-12-07 11:21:04 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-02-01 16:26:06 +00:00
|
|
|
func (cli cliItem) newInstallCmd() *cobra.Command {
|
|
|
|
var (
|
|
|
|
downloadOnly bool
|
|
|
|
force bool
|
|
|
|
ignoreError bool
|
|
|
|
)
|
|
|
|
|
2023-12-07 11:21:04 +00:00
|
|
|
cmd := &cobra.Command{
|
2023-12-15 14:27:22 +00:00
|
|
|
Use: coalesce.String(cli.installHelp.use, "install [item]..."),
|
|
|
|
Short: coalesce.String(cli.installHelp.short, fmt.Sprintf("Install given %s", cli.oneOrMore)),
|
|
|
|
Long: coalesce.String(cli.installHelp.long, fmt.Sprintf("Fetch and install one or more %s from the hub", cli.name)),
|
|
|
|
Example: cli.installHelp.example,
|
2023-12-07 11:21:04 +00:00
|
|
|
Args: cobra.MinimumNArgs(1),
|
|
|
|
DisableAutoGenTag: true,
|
|
|
|
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
|
2023-12-15 14:27:22 +00:00
|
|
|
return compAllItems(cli.name, args, toComplete)
|
2023-12-07 11:21:04 +00:00
|
|
|
},
|
2024-02-01 16:26:06 +00:00
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
return cli.install(args, downloadOnly, force, ignoreError)
|
|
|
|
},
|
2023-12-07 11:21:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
flags := cmd.Flags()
|
2024-02-01 16:26:06 +00:00
|
|
|
flags.BoolVarP(&downloadOnly, "download-only", "d", false, "Only download packages, don't enable")
|
|
|
|
flags.BoolVar(&force, "force", false, "Force install: overwrite tainted and outdated files")
|
|
|
|
flags.BoolVar(&ignoreError, "ignore", false, fmt.Sprintf("Ignore errors when installing multiple %s", cli.name))
|
2023-12-07 11:21:04 +00:00
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
|
|
|
// return the names of the installed parents of an item, used to check if we can remove it
|
|
|
|
func istalledParentNames(item *cwhub.Item) []string {
|
|
|
|
ret := make([]string, 0)
|
|
|
|
|
|
|
|
for _, parent := range item.Ancestors() {
|
|
|
|
if parent.State.Installed {
|
|
|
|
ret = append(ret, parent.Name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2024-02-01 16:26:06 +00:00
|
|
|
func (cli cliItem) remove(args []string, purge bool, force bool, all bool) error {
|
2023-12-19 16:20:09 +00:00
|
|
|
hub, err := require.Hub(csConfig, nil, log.StandardLogger())
|
2023-12-07 11:21:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if all {
|
|
|
|
getter := hub.GetInstalledItems
|
|
|
|
if purge {
|
|
|
|
getter = hub.GetAllItems
|
|
|
|
}
|
|
|
|
|
2023-12-15 14:27:22 +00:00
|
|
|
items, err := getter(cli.name)
|
2023-12-07 11:21:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
removed := 0
|
|
|
|
|
|
|
|
for _, item := range items {
|
|
|
|
didRemove, err := item.Remove(purge, force)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-01-03 09:55:41 +00:00
|
|
|
|
2023-12-07 11:21:04 +00:00
|
|
|
if didRemove {
|
|
|
|
log.Infof("Removed %s", item.Name)
|
|
|
|
removed++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-15 14:27:22 +00:00
|
|
|
log.Infof("Removed %d %s", removed, cli.name)
|
2024-01-03 09:55:41 +00:00
|
|
|
|
2023-12-07 11:21:04 +00:00
|
|
|
if removed > 0 {
|
|
|
|
log.Infof(ReloadMessage())
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(args) == 0 {
|
2023-12-15 14:27:22 +00:00
|
|
|
return fmt.Errorf("specify at least one %s to remove or '--all'", cli.singular)
|
2023-12-07 11:21:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
removed := 0
|
|
|
|
|
|
|
|
for _, itemName := range args {
|
2023-12-15 14:27:22 +00:00
|
|
|
item := hub.GetItem(cli.name, itemName)
|
2023-12-07 11:21:04 +00:00
|
|
|
if item == nil {
|
2023-12-15 14:27:22 +00:00
|
|
|
return fmt.Errorf("can't find '%s' in %s", itemName, cli.name)
|
2023-12-07 11:21:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
parents := istalledParentNames(item)
|
|
|
|
|
|
|
|
if !force && len(parents) > 0 {
|
|
|
|
log.Warningf("%s belongs to collections: %s", item.Name, parents)
|
2023-12-15 14:27:22 +00:00
|
|
|
log.Warningf("Run 'sudo cscli %s remove %s --force' if you want to force remove this %s", item.Type, item.Name, cli.singular)
|
|
|
|
|
2023-12-07 11:21:04 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
didRemove, err := item.Remove(purge, force)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if didRemove {
|
|
|
|
log.Infof("Removed %s", item.Name)
|
|
|
|
removed++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-15 14:27:22 +00:00
|
|
|
log.Infof("Removed %d %s", removed, cli.name)
|
2024-01-03 09:55:41 +00:00
|
|
|
|
2023-12-07 11:21:04 +00:00
|
|
|
if removed > 0 {
|
|
|
|
log.Infof(ReloadMessage())
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-02-01 16:26:06 +00:00
|
|
|
func (cli cliItem) newRemoveCmd() *cobra.Command {
|
|
|
|
var (
|
|
|
|
purge bool
|
|
|
|
force bool
|
|
|
|
all bool
|
|
|
|
)
|
|
|
|
|
2023-12-07 11:21:04 +00:00
|
|
|
cmd := &cobra.Command{
|
2023-12-15 14:27:22 +00:00
|
|
|
Use: coalesce.String(cli.removeHelp.use, "remove [item]..."),
|
|
|
|
Short: coalesce.String(cli.removeHelp.short, fmt.Sprintf("Remove given %s", cli.oneOrMore)),
|
|
|
|
Long: coalesce.String(cli.removeHelp.long, fmt.Sprintf("Remove one or more %s", cli.name)),
|
|
|
|
Example: cli.removeHelp.example,
|
2023-12-07 11:21:04 +00:00
|
|
|
Aliases: []string{"delete"},
|
|
|
|
DisableAutoGenTag: true,
|
|
|
|
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
|
2023-12-15 14:27:22 +00:00
|
|
|
return compInstalledItems(cli.name, args, toComplete)
|
2023-12-07 11:21:04 +00:00
|
|
|
},
|
2024-02-01 16:26:06 +00:00
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
return cli.remove(args, purge, force, all)
|
|
|
|
},
|
2023-12-07 11:21:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
flags := cmd.Flags()
|
2024-02-01 16:26:06 +00:00
|
|
|
flags.BoolVar(&purge, "purge", false, "Delete source file too")
|
|
|
|
flags.BoolVar(&force, "force", false, "Force remove: remove tainted and outdated files")
|
|
|
|
flags.BoolVar(&all, "all", false, fmt.Sprintf("Remove all the %s", cli.name))
|
2023-12-07 11:21:04 +00:00
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2024-02-01 16:26:06 +00:00
|
|
|
func (cli cliItem) upgrade(args []string, force bool, all bool) error {
|
2023-12-19 16:20:09 +00:00
|
|
|
hub, err := require.Hub(csConfig, require.RemoteHub(csConfig), log.StandardLogger())
|
2023-12-07 11:21:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if all {
|
2023-12-15 14:27:22 +00:00
|
|
|
items, err := hub.GetInstalledItems(cli.name)
|
2023-12-07 11:21:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
updated := 0
|
|
|
|
|
|
|
|
for _, item := range items {
|
|
|
|
didUpdate, err := item.Upgrade(force)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-01-03 09:55:41 +00:00
|
|
|
|
2023-12-07 11:21:04 +00:00
|
|
|
if didUpdate {
|
|
|
|
updated++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-15 14:27:22 +00:00
|
|
|
log.Infof("Updated %d %s", updated, cli.name)
|
2023-12-07 11:21:04 +00:00
|
|
|
|
|
|
|
if updated > 0 {
|
|
|
|
log.Infof(ReloadMessage())
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(args) == 0 {
|
2023-12-15 14:27:22 +00:00
|
|
|
return fmt.Errorf("specify at least one %s to upgrade or '--all'", cli.singular)
|
2023-12-07 11:21:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
updated := 0
|
|
|
|
|
|
|
|
for _, itemName := range args {
|
2023-12-15 14:27:22 +00:00
|
|
|
item := hub.GetItem(cli.name, itemName)
|
2023-12-07 11:21:04 +00:00
|
|
|
if item == nil {
|
2023-12-15 14:27:22 +00:00
|
|
|
return fmt.Errorf("can't find '%s' in %s", itemName, cli.name)
|
2023-12-07 11:21:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
didUpdate, err := item.Upgrade(force)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if didUpdate {
|
|
|
|
log.Infof("Updated %s", item.Name)
|
|
|
|
updated++
|
|
|
|
}
|
|
|
|
}
|
2024-01-03 09:55:41 +00:00
|
|
|
|
2023-12-07 11:21:04 +00:00
|
|
|
if updated > 0 {
|
|
|
|
log.Infof(ReloadMessage())
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-02-01 16:26:06 +00:00
|
|
|
func (cli cliItem) newUpgradeCmd() *cobra.Command {
|
|
|
|
var (
|
|
|
|
all bool
|
|
|
|
force bool
|
|
|
|
)
|
|
|
|
|
2023-12-07 11:21:04 +00:00
|
|
|
cmd := &cobra.Command{
|
2023-12-15 14:27:22 +00:00
|
|
|
Use: coalesce.String(cli.upgradeHelp.use, "upgrade [item]..."),
|
|
|
|
Short: coalesce.String(cli.upgradeHelp.short, fmt.Sprintf("Upgrade given %s", cli.oneOrMore)),
|
|
|
|
Long: coalesce.String(cli.upgradeHelp.long, fmt.Sprintf("Fetch and upgrade one or more %s from the hub", cli.name)),
|
|
|
|
Example: cli.upgradeHelp.example,
|
2023-12-07 11:21:04 +00:00
|
|
|
DisableAutoGenTag: true,
|
|
|
|
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
|
2023-12-15 14:27:22 +00:00
|
|
|
return compInstalledItems(cli.name, args, toComplete)
|
2023-12-07 11:21:04 +00:00
|
|
|
},
|
2024-02-01 16:26:06 +00:00
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
return cli.upgrade(args, force, all)
|
|
|
|
},
|
2023-12-07 11:21:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
flags := cmd.Flags()
|
2024-02-01 16:26:06 +00:00
|
|
|
flags.BoolVarP(&all, "all", "a", false, fmt.Sprintf("Upgrade all the %s", cli.name))
|
|
|
|
flags.BoolVar(&force, "force", false, "Force upgrade: overwrite tainted and outdated files")
|
2023-12-07 11:21:04 +00:00
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2024-02-01 16:26:06 +00:00
|
|
|
func (cli cliItem) inspect(args []string, url string, diff bool, rev bool, noMetrics bool) error {
|
|
|
|
if rev && !diff {
|
|
|
|
return fmt.Errorf("--rev can only be used with --diff")
|
2023-12-07 11:21:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if url != "" {
|
|
|
|
csConfig.Cscli.PrometheusUrl = url
|
|
|
|
}
|
|
|
|
|
2023-12-15 14:27:22 +00:00
|
|
|
remote := (*cwhub.RemoteHubCfg)(nil)
|
|
|
|
|
|
|
|
if diff {
|
|
|
|
remote = require.RemoteHub(csConfig)
|
|
|
|
}
|
|
|
|
|
2023-12-19 16:20:09 +00:00
|
|
|
hub, err := require.Hub(csConfig, remote, log.StandardLogger())
|
2023-12-07 11:21:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, name := range args {
|
2023-12-15 14:27:22 +00:00
|
|
|
item := hub.GetItem(cli.name, name)
|
2023-12-07 11:21:04 +00:00
|
|
|
if item == nil {
|
2023-12-15 14:27:22 +00:00
|
|
|
return fmt.Errorf("can't find '%s' in %s", name, cli.name)
|
|
|
|
}
|
|
|
|
|
|
|
|
if diff {
|
|
|
|
fmt.Println(cli.whyTainted(hub, item, rev))
|
|
|
|
|
|
|
|
continue
|
2023-12-07 11:21:04 +00:00
|
|
|
}
|
2023-12-15 14:27:22 +00:00
|
|
|
|
2024-02-01 16:26:06 +00:00
|
|
|
if err = inspectItem(item, !noMetrics); err != nil {
|
2023-12-07 11:21:04 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-12-15 14:27:22 +00:00
|
|
|
if cli.inspectDetail != nil {
|
|
|
|
if err = cli.inspectDetail(item); err != nil {
|
2023-12-07 11:21:04 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-02-01 16:26:06 +00:00
|
|
|
func (cli cliItem) newInspectCmd() *cobra.Command {
|
|
|
|
var (
|
|
|
|
url string
|
|
|
|
diff bool
|
|
|
|
rev bool
|
|
|
|
noMetrics bool
|
|
|
|
)
|
|
|
|
|
2023-12-07 11:21:04 +00:00
|
|
|
cmd := &cobra.Command{
|
2023-12-15 14:27:22 +00:00
|
|
|
Use: coalesce.String(cli.inspectHelp.use, "inspect [item]..."),
|
|
|
|
Short: coalesce.String(cli.inspectHelp.short, fmt.Sprintf("Inspect given %s", cli.oneOrMore)),
|
|
|
|
Long: coalesce.String(cli.inspectHelp.long, fmt.Sprintf("Inspect the state of one or more %s", cli.name)),
|
|
|
|
Example: cli.inspectHelp.example,
|
2023-12-07 11:21:04 +00:00
|
|
|
Args: cobra.MinimumNArgs(1),
|
|
|
|
DisableAutoGenTag: true,
|
|
|
|
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
|
2023-12-15 14:27:22 +00:00
|
|
|
return compInstalledItems(cli.name, args, toComplete)
|
|
|
|
},
|
2024-02-01 16:26:06 +00:00
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
return cli.inspect(args, url, diff, rev, noMetrics)
|
2023-12-07 11:21:04 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
flags := cmd.Flags()
|
2024-02-01 16:26:06 +00:00
|
|
|
flags.StringVarP(&url, "url", "u", "", "Prometheus url")
|
|
|
|
flags.BoolVar(&diff, "diff", false, "Show diff with latest version (for tainted items)")
|
|
|
|
flags.BoolVar(&rev, "rev", false, "Reverse diff output")
|
|
|
|
flags.BoolVar(&noMetrics, "no-metrics", false, "Don't show metrics (when cscli.output=human)")
|
2023-12-07 11:21:04 +00:00
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|
|
|
|
|
2024-02-01 16:26:06 +00:00
|
|
|
func (cli cliItem) list(args []string, all bool) error {
|
2023-12-19 16:20:09 +00:00
|
|
|
hub, err := require.Hub(csConfig, nil, log.StandardLogger())
|
2023-12-07 11:21:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
items := make(map[string][]*cwhub.Item)
|
|
|
|
|
2023-12-15 14:27:22 +00:00
|
|
|
items[cli.name], err = selectItems(hub, cli.name, args, !all)
|
2023-12-07 11:21:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-12-15 14:27:22 +00:00
|
|
|
if err = listItems(color.Output, []string{cli.name}, items, false); err != nil {
|
2023-12-07 11:21:04 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-02-01 16:26:06 +00:00
|
|
|
func (cli cliItem) newListCmd() *cobra.Command {
|
|
|
|
var all bool
|
|
|
|
|
2023-12-07 11:21:04 +00:00
|
|
|
cmd := &cobra.Command{
|
2023-12-15 14:27:22 +00:00
|
|
|
Use: coalesce.String(cli.listHelp.use, "list [item... | -a]"),
|
|
|
|
Short: coalesce.String(cli.listHelp.short, fmt.Sprintf("List %s", cli.oneOrMore)),
|
|
|
|
Long: coalesce.String(cli.listHelp.long, fmt.Sprintf("List of installed/available/specified %s", cli.name)),
|
|
|
|
Example: cli.listHelp.example,
|
2023-12-07 11:21:04 +00:00
|
|
|
DisableAutoGenTag: true,
|
2024-02-01 16:26:06 +00:00
|
|
|
RunE: func(_ *cobra.Command, args []string) error {
|
|
|
|
return cli.list(args, all)
|
|
|
|
},
|
2023-12-07 11:21:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
flags := cmd.Flags()
|
2024-02-01 16:26:06 +00:00
|
|
|
flags.BoolVarP(&all, "all", "a", false, "List disabled items as well")
|
2023-12-07 11:21:04 +00:00
|
|
|
|
|
|
|
return cmd
|
|
|
|
}
|
2023-12-15 14:27:22 +00:00
|
|
|
|
|
|
|
// return the diff between the installed version and the latest version
|
|
|
|
func (cli cliItem) itemDiff(item *cwhub.Item, reverse bool) (string, error) {
|
|
|
|
if !item.State.Installed {
|
|
|
|
return "", fmt.Errorf("'%s' is not installed", item.FQName())
|
|
|
|
}
|
|
|
|
|
|
|
|
latestContent, remoteURL, err := item.FetchLatest()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
localContent, err := os.ReadFile(item.State.LocalPath)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("while reading %s: %w", item.State.LocalPath, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
file1 := item.State.LocalPath
|
|
|
|
file2 := remoteURL
|
|
|
|
content1 := string(localContent)
|
|
|
|
content2 := string(latestContent)
|
2024-01-03 09:55:41 +00:00
|
|
|
|
2023-12-15 14:27:22 +00:00
|
|
|
if reverse {
|
|
|
|
file1, file2 = file2, file1
|
|
|
|
content1, content2 = content2, content1
|
|
|
|
}
|
|
|
|
|
|
|
|
edits := myers.ComputeEdits(span.URIFromPath(file1), content1, content2)
|
|
|
|
diff := gotextdiff.ToUnified(file1, file2, content1, edits)
|
|
|
|
|
|
|
|
return fmt.Sprintf("%s", diff), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cli cliItem) whyTainted(hub *cwhub.Hub, item *cwhub.Item, reverse bool) string {
|
|
|
|
if !item.State.Installed {
|
|
|
|
return fmt.Sprintf("# %s is not installed", item.FQName())
|
|
|
|
}
|
|
|
|
|
|
|
|
if !item.State.Tainted {
|
|
|
|
return fmt.Sprintf("# %s is not tainted", item.FQName())
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(item.State.TaintedBy) == 0 {
|
|
|
|
return fmt.Sprintf("# %s is tainted but we don't know why. please report this as a bug", item.FQName())
|
|
|
|
}
|
|
|
|
|
|
|
|
ret := []string{
|
|
|
|
fmt.Sprintf("# Let's see why %s is tainted.", item.FQName()),
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, fqsub := range item.State.TaintedBy {
|
|
|
|
ret = append(ret, fmt.Sprintf("\n-> %s\n", fqsub))
|
|
|
|
|
|
|
|
sub, err := hub.GetItemFQ(fqsub)
|
|
|
|
if err != nil {
|
|
|
|
ret = append(ret, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
diff, err := cli.itemDiff(sub, reverse)
|
|
|
|
if err != nil {
|
|
|
|
ret = append(ret, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
if diff != "" {
|
|
|
|
ret = append(ret, diff)
|
|
|
|
} else if len(sub.State.TaintedBy) > 0 {
|
|
|
|
taintList := strings.Join(sub.State.TaintedBy, ", ")
|
2024-01-03 08:33:52 +00:00
|
|
|
if sub.FQName() == taintList {
|
|
|
|
// hack: avoid message "item is tainted by itself"
|
|
|
|
continue
|
|
|
|
}
|
2023-12-15 14:27:22 +00:00
|
|
|
ret = append(ret, fmt.Sprintf("# %s is tainted by %s", sub.FQName(), taintList))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return strings.Join(ret, "\n")
|
|
|
|
}
|