Minor changes

This commit is contained in:
David Allen 2024-09-18 20:18:53 -06:00
parent db2b0a7372
commit 4cf854313a
Signed by: towk
GPG key ID: 793B2924A49B3A3F
2 changed files with 22 additions and 6 deletions

View file

@ -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)
}

View file

@ -69,7 +69,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)