func()

in command-runner/internal/containers/docker/docker_run.go [313:367]


func (cr *dockerContainer) tryReadID(opt string, cbk func(id int)) common.Executor {
	return func(ctx context.Context) error {
		idResp, err := cr.cli.ContainerExecCreate(ctx, cr.id, container.ExecOptions{
			Cmd:          []string{"id", opt},
			AttachStdout: true,
			AttachStderr: true,
		})
		if err != nil {
			log.Ctx(ctx).Debug().Err(err).Msgf("tryReadID - Unable to create exec for container=%s", cr.id)
			inspectResp, err := cr.cli.ContainerInspect(ctx, cr.id)
			if err != nil {
				log.Ctx(ctx).Debug().Err(err).Msgf("tryReadID - Unable to inspect container=%s", cr.id)
			} else {
				log.Ctx(ctx).Debug().Msgf("state=%#v", inspectResp.State)
				logResp, err := cr.cli.ContainerLogs(ctx, cr.id, container.LogsOptions{
					ShowStdout: true,
					ShowStderr: true,
				})
				if err != nil {
					log.Ctx(ctx).Debug().Err(err).Msgf("tryReadID - Unable to get logs for container=%s", cr.id)
				}
				if log.Ctx(ctx).Debug().Enabled() {
					_, _ = io.Copy(os.Stdout, logResp)
				}
			}
			return nil
		}

		resp, err := cr.cli.ContainerExecAttach(ctx, idResp.ID, container.ExecStartOptions{})
		if err != nil {
			log.Ctx(ctx).Warn().Err(err).Msgf("tryReadID - Unable to attach exec for container=%s", cr.id)
			return nil
		}
		defer resp.Close()

		sid, err := resp.Reader.ReadString('\n')
		if err != nil {
			return nil
		}
		log.Ctx(ctx).Debug().Msgf("Reading id with opt=%s and got back: %s", opt, sid)
		exp := regexp.MustCompile(`\d+\n`)
		found := exp.FindString(sid)
		if len(found) == 0 {
			log.Ctx(ctx).Warn().Msgf("Unable to read id with opt=%s - got back: %s", opt, sid)
			return nil
		}
		id, err := strconv.ParseInt(found[:len(found)-1], 10, 32)
		if err != nil {
			return nil
		}
		cbk(int(id))

		return nil
	}
}