mirror of
https://github.com/davidallendj/magellan.git
synced 2025-12-20 11:37:01 -07:00
Fixed removing from cache with --with-* flags
This commit is contained in:
parent
1539aa587c
commit
3c078909dc
2 changed files with 40 additions and 8 deletions
23
cmd/cache.go
23
cmd/cache.go
|
|
@ -62,11 +62,28 @@ var cacheRemoveCmd = &cobra.Command{
|
||||||
assets = append(assets, asset)
|
assets = append(assets, asset)
|
||||||
}
|
}
|
||||||
|
|
||||||
// add all assets with specified hosts (same host different different ports)
|
// Add all assets with specified hosts (same host different different ports)
|
||||||
|
// This should produce the following SQL:
|
||||||
|
// DELETE FROM magellan_scanned_assets WHERE host=:host
|
||||||
for _, host := range withHosts {
|
for _, host := range withHosts {
|
||||||
|
assets = append(assets, magellan.RemoteAsset{
|
||||||
|
Host: host,
|
||||||
|
Port: -1,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
// Add all assets with specified ports (same port different hosts)
|
||||||
|
// This should produce the following SQL:
|
||||||
|
// DELETE FROM magellan_scanned_assets WHERE port=:port
|
||||||
|
for _, port := range withPorts {
|
||||||
|
assets = append(assets, magellan.RemoteAsset{
|
||||||
|
Host: "",
|
||||||
|
Port: port,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
if len(assets) <= 0 {
|
||||||
|
log.Error().Msg("nothing to do")
|
||||||
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
// add all assets with specified ports (same port different hosts)
|
|
||||||
sqlite.DeleteScannedAssets(cachePath, assets...)
|
sqlite.DeleteScannedAssets(cachePath, assets...)
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
|
||||||
25
internal/cache/sqlite/sqlite.go
vendored
25
internal/cache/sqlite/sqlite.go
vendored
|
|
@ -2,6 +2,7 @@ package sqlite
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/OpenCHAMI/magellan/internal/util"
|
"github.com/OpenCHAMI/magellan/internal/util"
|
||||||
magellan "github.com/OpenCHAMI/magellan/pkg"
|
magellan "github.com/OpenCHAMI/magellan/pkg"
|
||||||
|
|
@ -63,22 +64,36 @@ func InsertScannedAssets(path string, assets ...magellan.RemoteAsset) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
func DeleteScannedAssets(path string, assets ...magellan.RemoteAsset) error {
|
func DeleteScannedAssets(path string, assets ...magellan.RemoteAsset) error {
|
||||||
|
var (
|
||||||
|
db *sqlx.DB
|
||||||
|
tx *sqlx.Tx
|
||||||
|
err error
|
||||||
|
)
|
||||||
if assets == nil {
|
if assets == nil {
|
||||||
return fmt.Errorf("no assets found")
|
return fmt.Errorf("no assets found")
|
||||||
}
|
}
|
||||||
db, err := sqlx.Open("sqlite3", path)
|
db, err = sqlx.Open("sqlite3", path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to open database: %v", err)
|
return fmt.Errorf("failed to open database: %v", err)
|
||||||
}
|
}
|
||||||
tx := db.MustBegin()
|
tx = db.MustBegin()
|
||||||
for _, asset := range assets {
|
for _, asset := range assets {
|
||||||
|
// skip if neither host nor port are specified
|
||||||
if asset.Host == "" && asset.Port <= 0 {
|
if asset.Host == "" && asset.Port <= 0 {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
sql := fmt.Sprintf(`DELETE FROM %s WHERE port=:port;`, TABLE_NAME)
|
sql := fmt.Sprintf(`DELETE FROM %s`, TABLE_NAME)
|
||||||
if asset.Host != "" {
|
where := []string{}
|
||||||
sql += "AND host=:host"
|
if asset.Port > 0 {
|
||||||
|
where = append(where, "port=:port")
|
||||||
}
|
}
|
||||||
|
if asset.Host != "" {
|
||||||
|
where = append(where, "host=:host")
|
||||||
|
}
|
||||||
|
if len(where) <= 0 {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
sql += fmt.Sprintf(" WHERE %s;", strings.Join(where, " AND "))
|
||||||
_, err := tx.NamedExec(sql, &asset)
|
_, err := tx.NamedExec(sql, &asset)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Printf("failed to execute DELETE transaction: %v\n", err)
|
fmt.Printf("failed to execute DELETE transaction: %v\n", err)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue