func bootstrap()

in commands/helpers/proxy_exec.go [105:158]


func bootstrap(dst string) error {
	src, _ := os.Executable()

	_ = os.MkdirAll(dst, 0o777)

	pathname := filepath.Join(dst, "gitlab-runner-helper")
	_, err := os.Stat(pathname)
	if err == nil {
		// if the path exists, check to see if it's identical by comparing build info
		buildInfoDst, err := buildinfo.ReadFile(pathname)
		if err != nil {
			return fmt.Errorf("reading build info of existing binary: %w", err)
		}

		buildInfoSrc, ok := debug.ReadBuildInfo()
		if ok && buildInfoDst.String() == buildInfoSrc.String() {
			return nil
		}
	}
	if err != nil && !errors.Is(err, os.ErrNotExist) {
		return fmt.Errorf("checking helper install: %w", err)
	}

	fsrc, err := os.Open(src)
	if err != nil {
		return fmt.Errorf("opening helper: %w", err)
	}
	defer fsrc.Close()

	fdst, err := os.CreateTemp(dst, "")
	if err != nil {
		return fmt.Errorf("creating temp file: %w", err)
	}
	defer os.RemoveAll(fdst.Name())
	defer fdst.Close()

	if _, err := io.Copy(fdst, fsrc); err != nil {
		return fmt.Errorf("copying helper: %w", err)
	}

	if err := fdst.Close(); err != nil {
		return fmt.Errorf("closing helper: %w", err)
	}

	if err := os.Rename(fdst.Name(), pathname); err != nil {
		return fmt.Errorf("renaming helper: %w", err)
	}

	if err := os.Chmod(pathname, 0o777); err != nil {
		return fmt.Errorf("changing helper permissions: %w", err)
	}

	return nil
}