init InspectService method

This commit is contained in:
Pavel Sviderski
2024-12-02 20:36:31 +10:00
parent 6a4a7f2e6a
commit 6eb373e4de
10 changed files with 561 additions and 110 deletions
+28 -2
View File
@@ -29,7 +29,15 @@ type ContainerRecord struct {
type ListOptions struct {
// MachineIDs filters containers by the machine IDs they are running on.
MachineIDs []string
MachineIDs []string
ServiceIDOrName ServiceIDOrNameOptions
}
// ServiceIDOrNameOptions filters containers by the service ID or name they are part of. If both ID and Name are
// provided, they are combined with an OR operator.
type ServiceIDOrNameOptions struct {
ID string
Name string
}
type DeleteOptions struct {
@@ -70,14 +78,32 @@ func (s *Store) ListContainers(ctx context.Context, opts ListOptions) ([]*Contai
query := "SELECT container, machine_id, sync_status, updated_at FROM containers"
var args []any
var whereConditions []string
if len(opts.MachineIDs) > 0 {
query += " WHERE machine_id IN (?" + strings.Repeat(", ?", len(opts.MachineIDs)-1) + ")"
whereConditions = append(whereConditions, "machine_id IN (?"+strings.Repeat(", ?", len(opts.MachineIDs)-1)+")")
args = make([]any, len(opts.MachineIDs))
for i, id := range opts.MachineIDs {
args[i] = id
}
}
var serviceConditions []string
var serviceArgs []any
if opts.ServiceIDOrName.ID != "" {
serviceConditions = append(serviceConditions, "service_id = ?")
serviceArgs = append(serviceArgs, opts.ServiceIDOrName.ID)
}
if opts.ServiceIDOrName.Name != "" {
serviceConditions = append(serviceConditions, "service_name = ?")
serviceArgs = append(serviceArgs, opts.ServiceIDOrName.Name)
}
if len(serviceConditions) > 0 {
whereConditions = append(whereConditions, "("+strings.Join(serviceConditions, " OR ")+")")
args = append(args, serviceArgs...)
}
query += " WHERE " + strings.Join(whereConditions, " AND ")
rows, err := s.corro.QueryContext(ctx, query, args...)
if err != nil {
return nil, fmt.Errorf("select query: %w", err)