From 1539aa587c47fdaa59bfa0fa58c129020d3d2d97 Mon Sep 17 00:00:00 2001 From: "David J. Allen" Date: Wed, 18 Sep 2024 20:18:53 -0600 Subject: [PATCH] Minor changes --- cmd/cache.go | 20 +++++++++++++++----- internal/cache/sqlite/sqlite.go | 8 +++++++- 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/cmd/cache.go b/cmd/cache.go index 676581e..aefd15b 100644 --- a/cmd/cache.go +++ b/cmd/cache.go @@ -13,8 +13,8 @@ import ( ) var ( - withAllHosts bool - withAllPorts bool + withHosts []string + withPorts []int ) var cacheCmd = &cobra.Command{ @@ -34,6 +34,8 @@ var cacheRemoveCmd = &cobra.Command{ Short: "Remove a host from a scanned cache list.", Run: func(cmd *cobra.Command, args []string) { assets := []magellan.RemoteAsset{} + + // add all assets directly from positional args for _, arg := range args { var ( port int @@ -46,6 +48,9 @@ var cacheRemoveCmd = &cobra.Command{ } // convert port to its "proper" type + if uri.Port() == "" { + uri.Host += ":443" + } port, err = strconv.Atoi(uri.Port()) if err != nil { log.Error().Err(err).Msg("failed to convert port to integer type") @@ -54,16 +59,21 @@ var cacheRemoveCmd = &cobra.Command{ Host: fmt.Sprintf("%s://%s", uri.Scheme, uri.Hostname()), Port: port, } - fmt.Printf("%s:%d\n", asset.Host, asset.Port) assets = append(assets, asset) } + + // add all assets with specified hosts (same host different different ports) + for _, host := range withHosts { + + } + // add all assets with specified ports (same port different hosts) sqlite.DeleteScannedAssets(cachePath, assets...) }, } func init() { - cacheRemoveCmd.Flags().BoolVar(&withAllHosts, "--all-hosts", false, "Remove all assets with specified hosts") - cacheRemoveCmd.Flags().BoolVar(&withAllPorts, "--all-ports", false, "Remove all assets with specified ports") + 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) rootCmd.AddCommand(cacheCmd) } diff --git a/internal/cache/sqlite/sqlite.go b/internal/cache/sqlite/sqlite.go index b9884c1..f789268 100644 --- a/internal/cache/sqlite/sqlite.go +++ b/internal/cache/sqlite/sqlite.go @@ -72,7 +72,13 @@ func DeleteScannedAssets(path string, assets ...magellan.RemoteAsset) error { } tx := db.MustBegin() for _, asset := range assets { - sql := fmt.Sprintf(`DELETE FROM %s WHERE host=:host AND port=:port;`, TABLE_NAME) + if asset.Host == "" && asset.Port <= 0 { + continue + } + sql := fmt.Sprintf(`DELETE FROM %s WHERE port=:port;`, TABLE_NAME) + if asset.Host != "" { + sql += "AND host=:host" + } _, err := tx.NamedExec(sql, &asset) if err != nil { fmt.Printf("failed to execute DELETE transaction: %v\n", err)