Repository: moby/moby
File: api/server/router/build/build_routes.go (Go)

Code before:
		return "", invalidParam{errors.Errorf("invalid version %q", s)}
	}
}

func (br *buildRouter) postPrune(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
	if err := httputils.ParseForm(r); err != nil {
		return err
	}
	fltrs, err := filters.FromJSON(r.Form.Get("filters"))
	if err != nil {
		return err
	}

	opts := types.BuildCachePruneOptions{
		All:     httputils.BoolValue(r, "all"),
		Filters: fltrs,
	}

	if fv := r.FormValue("reserved-space"); fv != "" {
		bs, err := strconv.Atoi(fv)
		if err != nil {
			return invalidParam{errors.Wrapf(err, "reserved-space is in bytes and expects an integer, got %v", fv)}
		}
		opts.ReservedSpace = int64(bs)
	} else if fv := r.FormValue("keep-storage"); fv != "" {
		// Deprecated parameter. Only checked if reserved-space is not used.
		bs, err := strconv.Atoi(fv)
		if err != nil {
			return invalidParam{errors.Wrapf(err, "keep-storage is in bytes and expects an integer, got %v", fv)}
		}
		opts.ReservedSpace = int64(bs)
	}

	if fv := r.FormValue("max-used-space"); fv != "" {
		bs, err := strconv.Atoi(fv)
		if err != nil {
			return invalidParam{errors.Wrapf(err, "max-used-space is in bytes and expects an integer, got %v", fv)}
		}
		opts.MaxUsedSpace = int64(bs)
	}

	if fv := r.FormValue("min-free-space"); fv != "" {
		bs, err := strconv.Atoi(fv)
		if err != nil {
			return invalidParam{errors.Wrapf(err, "min-free-space is in bytes and expects an integer, got %v", fv)}
		}
		opts.MinFreeSpace = int64(bs)
	}

	report, err := br.backend.PruneCache(ctx, opts)
	if err != nil {

Review comment (suggestion):
As this is a new API-level feature - we should either:

1. Not backport this to 27.x
2. Ship API v1.48 in 27.4 and move the current 1.48 API features to 1.49


Code after:
		return "", invalidParam{errors.Errorf("invalid version %q", s)}
	}
}

func (br *buildRouter) postPrune(ctx context.Context, w http.ResponseWriter, r *http.Request, vars map[string]string) error {
	if err := httputils.ParseForm(r); err != nil {
		return err
	}
	fltrs, err := filters.FromJSON(r.Form.Get("filters"))
	if err != nil {
		return err
	}

	opts := types.BuildCachePruneOptions{
		All:     httputils.BoolValue(r, "all"),
		Filters: fltrs,
	}

	parseBytesFromFormValue := func(name string) (int64, error) {
		if fv := r.FormValue(name); fv != "" {
			bs, err := strconv.Atoi(fv)
			if err != nil {
				return 0, invalidParam{errors.Wrapf(err, "%s is in bytes and expects an integer, got %v", name, fv)}
			}
			return int64(bs), nil
		}
		return 0, nil
	}

	version := httputils.VersionFromContext(ctx)
	if versions.GreaterThanOrEqualTo(version, "1.48") {
		bs, err := parseBytesFromFormValue("reserved-space")
		if err != nil {
			return err
		} else if bs == 0 {
			// Deprecated parameter. Only checked if reserved-space is not used.
			bs, err = parseBytesFromFormValue("keep-storage")
			if err != nil {
				return err
			}
		}
		opts.ReservedSpace = bs

		if bs, err := parseBytesFromFormValue("max-used-space"); err != nil {
			return err
		} else {
			opts.MaxUsedSpace = bs
		}

		if bs, err := parseBytesFromFormValue("min-free-space"); err != nil {
			return err