mirror of
https://github.com/davidallendj/magellan.git
synced 2025-12-20 03:27:03 -07:00
refactor: updated cache editor implementation
This commit is contained in:
parent
5a02fd9b55
commit
54f3a98e6e
6 changed files with 78 additions and 28 deletions
59
cmd/cache.go
59
cmd/cache.go
|
|
@ -6,13 +6,18 @@ import (
|
|||
"os"
|
||||
"strconv"
|
||||
|
||||
tea "github.com/charmbracelet/bubbletea"
|
||||
"github.com/davidallendj/magellan/internal/cache"
|
||||
"github.com/davidallendj/magellan/internal/cache/sqlite"
|
||||
"github.com/davidallendj/magellan/internal/util"
|
||||
magellan "github.com/davidallendj/magellan/pkg"
|
||||
"github.com/rs/zerolog/log"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var (
|
||||
cacheOutputFormat string
|
||||
interactive bool
|
||||
withHosts []string
|
||||
withPorts []int
|
||||
)
|
||||
|
|
@ -20,13 +25,6 @@ var (
|
|||
var cacheCmd = &cobra.Command{
|
||||
Use: "cache",
|
||||
Short: "Manage found assets in cache.",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
// show the help for cache and exit
|
||||
if len(args) <= 0 {
|
||||
cmd.Help()
|
||||
os.Exit(0)
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
var cacheRemoveCmd = &cobra.Command{
|
||||
|
|
@ -88,9 +86,54 @@ var cacheRemoveCmd = &cobra.Command{
|
|||
},
|
||||
}
|
||||
|
||||
var cacheEditCmd = &cobra.Command{
|
||||
Use: "edit",
|
||||
Short: "Modify cache data either interactively or non-interactively.",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
// start the interactive editor
|
||||
if interactive {
|
||||
p := tea.NewProgram(cache.NewModel())
|
||||
if _, err := p.Run(); err != nil {
|
||||
fmt.Printf("failed to start the cache editor: %v", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
} else {
|
||||
// only edit data with arguments
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
var cacheInfoCmd = &cobra.Command{
|
||||
Use: "info",
|
||||
Short: "Show cache-related information.",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
printCacheInfo(cacheOutputFormat)
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
// remove
|
||||
cacheRemoveCmd.Flags().StringSliceVar(&withHosts, "with-hosts", []string{}, "Remove all assets with specified hosts")
|
||||
cacheRemoveCmd.Flags().IntSliceVar(&withPorts, "with-ports", []int{}, "Remove all assets with specified ports")
|
||||
cacheCmd.AddCommand(cacheRemoveCmd)
|
||||
|
||||
// edit
|
||||
cacheEditCmd.Flags().BoolVarP(&interactive, "interactive", "i", false, "Edit cache data using interactive editor")
|
||||
|
||||
cacheInfoCmd.Flags().StringVarP(&cacheOutputFormat, "format", "F", FORMAT_LIST, "Set the output format (list|json|yaml)")
|
||||
|
||||
// commands
|
||||
cacheCmd.AddCommand(cacheRemoveCmd, cacheEditCmd, cacheInfoCmd)
|
||||
rootCmd.AddCommand(cacheCmd)
|
||||
}
|
||||
|
||||
func printCacheInfo(format string) {
|
||||
assets, err := sqlite.GetScannedAssets(cachePath)
|
||||
if err != nil {
|
||||
log.Error().Err(err).Str("path", cachePath).Msg("failed to get assets to print cache info")
|
||||
}
|
||||
cacheData := map[string]any{
|
||||
"path": cachePath,
|
||||
"assets": len(assets),
|
||||
}
|
||||
util.PrintMapWithFormat(cacheData, format)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -42,9 +42,7 @@ var ListCmd = &cobra.Command{
|
|||
Run: func(cmd *cobra.Command, args []string) {
|
||||
// check if we just want to show cache-related info and exit
|
||||
if showCacheInfo {
|
||||
magellan.PrintMapWithFormat(map[string]any{
|
||||
"path": cachePath,
|
||||
}, listOutputFormat)
|
||||
printCacheInfo(listOutputFormat)
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -15,6 +15,10 @@ type Model struct {
|
|||
Table table.Model
|
||||
}
|
||||
|
||||
func NewModel() Model {
|
||||
return Model{}
|
||||
}
|
||||
|
||||
func (m Model) Init() tea.Cmd { return nil }
|
||||
|
||||
func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||
|
|
@ -3,6 +3,7 @@ package util
|
|||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/rs/zerolog/log"
|
||||
"gopkg.in/yaml.v2"
|
||||
|
|
@ -23,3 +24,22 @@ func PrintYAML(data any) {
|
|||
}
|
||||
fmt.Print(string(b))
|
||||
}
|
||||
|
||||
func PrintMap(data map[string]any) {
|
||||
for k, v := range data {
|
||||
fmt.Printf("%s: %v\n", k, v)
|
||||
}
|
||||
}
|
||||
|
||||
func PrintMapWithFormat(data map[string]any, format string) {
|
||||
switch strings.ToLower(format) {
|
||||
case "json":
|
||||
PrintJSON(data)
|
||||
case "yaml":
|
||||
PrintYAML(data)
|
||||
case "list":
|
||||
PrintMap(data)
|
||||
default:
|
||||
log.Error().Msg("PrintMapWithFormat: unrecognized format")
|
||||
}
|
||||
}
|
||||
|
|
|
|||
15
pkg/list.go
15
pkg/list.go
|
|
@ -27,21 +27,6 @@ func PrintRemoteAssets(data []RemoteAsset, format string) {
|
|||
}
|
||||
}
|
||||
|
||||
func PrintMapWithFormat(data map[string]any, format string) {
|
||||
switch strings.ToLower(format) {
|
||||
case "json":
|
||||
util.PrintJSON(data)
|
||||
case "yaml":
|
||||
util.PrintYAML(data)
|
||||
case "list":
|
||||
for k, v := range data {
|
||||
fmt.Printf("%s: %v\n", k, v)
|
||||
}
|
||||
default:
|
||||
log.Error().Msg("PrintMapWithFormat: unrecognized format")
|
||||
}
|
||||
}
|
||||
|
||||
func ListDrives(cc *crawler.CrawlerConfig) ([]*redfish.Drive, error) {
|
||||
user, err := cc.GetUserPass()
|
||||
if err != nil {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue