github.com/containers/podman/v2@v2.2.2-0.20210501105131-c1e07d070c4c/pkg/bindings/images/rm.go (about)

     1  package images
     2  
     3  import (
     4  	"context"
     5  	"net/http"
     6  	"net/url"
     7  	"strconv"
     8  
     9  	"github.com/containers/podman/v2/pkg/api/handlers"
    10  	"github.com/containers/podman/v2/pkg/bindings"
    11  	"github.com/containers/podman/v2/pkg/domain/entities"
    12  	"github.com/containers/podman/v2/pkg/errorhandling"
    13  )
    14  
    15  // BachtRemove removes a batch of images from the local storage.
    16  func BatchRemove(ctx context.Context, images []string, opts entities.ImageRemoveOptions) (*entities.ImageRemoveReport, []error) {
    17  	// FIXME - bindings tests are missing for this endpoint. Once the CI is
    18  	// re-enabled for bindings, we need to add them.  At the time of writing,
    19  	// the tests don't compile.
    20  	var report handlers.LibpodImagesRemoveReport
    21  	conn, err := bindings.GetClient(ctx)
    22  	if err != nil {
    23  		return nil, []error{err}
    24  	}
    25  
    26  	params := url.Values{}
    27  	params.Set("all", strconv.FormatBool(opts.All))
    28  	params.Set("force", strconv.FormatBool(opts.Force))
    29  	for _, i := range images {
    30  		params.Add("images", i)
    31  	}
    32  
    33  	response, err := conn.DoRequest(nil, http.MethodDelete, "/images/remove", params, nil)
    34  	if err != nil {
    35  		return nil, []error{err}
    36  	}
    37  	if err := response.Process(&report); err != nil {
    38  		return nil, []error{err}
    39  	}
    40  
    41  	return &report.ImageRemoveReport, errorhandling.StringsToErrors(report.Errors)
    42  }
    43  
    44  // Remove removes an image from the local storage.  Use force to remove an
    45  // image, even if it's used by containers.
    46  func Remove(ctx context.Context, nameOrID string, force bool) (*entities.ImageRemoveReport, error) {
    47  	var report handlers.LibpodImagesRemoveReport
    48  	conn, err := bindings.GetClient(ctx)
    49  	if err != nil {
    50  		return nil, err
    51  	}
    52  
    53  	params := url.Values{}
    54  	params.Set("force", strconv.FormatBool(force))
    55  	response, err := conn.DoRequest(nil, http.MethodDelete, "/images/%s", params, nil, nameOrID)
    56  	if err != nil {
    57  		return nil, err
    58  	}
    59  	if err := response.Process(&report); err != nil {
    60  		return nil, err
    61  	}
    62  
    63  	errs := errorhandling.StringsToErrors(report.Errors)
    64  	return &report.ImageRemoveReport, errorhandling.JoinErrors(errs)
    65  }