package utils import ( "context" "database/sql" "flag" "fmt" "github.com/peterbourgon/ff/v3" "github.com/peterbourgon/ff/v3/ffcli" chariotClientApi "edge-infra.dev/pkg/edge/chariot/client" "edge-infra.dev/pkg/edge/constants/api/fleet" ) const ( Filter = "chariot" eyedeComponentOwner = "eyede" getBannerProjectID = "SELECT project_id from banners" GetClustersForBanner = "SELECT cluster_edge_id, cluster_name, project_id, bsl_site_id, location, banner_edge_id from clusters WHERE project_id = $1" LongHelp = ` CLI program written to populate existing edge info configmap with the correct cluster edge id. The cli program can be run like so: eyede --databaseHost=localhost --databaseName=dev0 --databaseUser=postgres --databasePassword=**** --topLevelProjectID=ret-dev-foreman --chariotTopic=chariot-rides The following flags are required for the cli to run: databaseHost, databaseName, databaseUser, databasePassword, topLevelProjectID and chariotTopic The dryRun flag is an optional and defaults to false if not specified. launchdarklyKey is also an optional flag that defaults to using cluster id if not set.` ) type EyedeCommand struct { DBConnection *sql.DB } func New() *EyedeCommand { return &EyedeCommand{} } func (e *EyedeCommand) SetDBConnection(db *sql.DB) *EyedeCommand { e.DBConnection = db return e } func (e EyedeCommand) Exec(name, shortUsage, shortHelp, foremanProjectID, topic, edgeAPIURL string, dryRun bool, callback func(ctx context.Context, db *sql.DB, projectID, foremanProjectID, topic, edgeAPIURL string, dryRun bool) bool, fs *flag.FlagSet) *ffcli.Command { return &ffcli.Command{ Name: name, ShortUsage: shortUsage, ShortHelp: shortHelp, LongHelp: LongHelp, FlagSet: fs, Options: []ff.Option{ff.WithEnvVarNoPrefix()}, Exec: func(ctx context.Context, _ []string) error { return queryBanners(ctx, e.DBConnection, foremanProjectID, topic, edgeAPIURL, dryRun, callback) }, } } func queryBanners(ctx context.Context, db *sql.DB, foremanProjectID, topic, edgeAPIURL string, dryRun bool, callback func(ctx context.Context, db *sql.DB, projectID, foremanProjectID, topic, edgeAPIURL string, dryRun bool) bool) error { banners, err := db.QueryContext(ctx, getBannerProjectID) if err != nil { fmt.Println("db error occurred ", err) return err } for banners.Next() { var projectID string if err := banners.Scan(&projectID); err != nil { fmt.Println("failed to scan banner projectID ", err) return err } next := callback(ctx, db, projectID, foremanProjectID, topic, edgeAPIURL, dryRun) //nolint if next { continue } } return nil } func CreateChariotMessage(projectID, id, filter, configMap string) *chariotClientApi.ChariotMessage { return chariotClientApi.NewChariotMessage(). SetBanner(projectID). SetCluster(id). SetDir(filter). SetOperation(chariotClientApi.Create). SetOwner(eyedeComponentOwner). AddObject(configMap) } func InvokeChariot(ctx context.Context, topLevelProjectID, chariotTopic string, message *chariotClientApi.ChariotMessage) error { return message.Publish(ctx, topLevelProjectID, chariotTopic, nil) } func AssertFleetAndStoreType(fleetType, storeType string) { if err := fleet.IsValid(fleetType); err != nil { AssertFleetAndStoreType(storeType, fleetType) } }