github.com/hanks177/podman/v4@v4.1.3-0.20220613032544-16d90015bc83/pkg/api/handlers/compat/images_history.go (about)

     1  package compat
     2  
     3  import (
     4  	"net/http"
     5  
     6  	"github.com/hanks177/podman/v4/libpod"
     7  	"github.com/hanks177/podman/v4/pkg/api/handlers"
     8  	"github.com/hanks177/podman/v4/pkg/api/handlers/utils"
     9  	api "github.com/hanks177/podman/v4/pkg/api/types"
    10  	"github.com/pkg/errors"
    11  )
    12  
    13  func HistoryImage(w http.ResponseWriter, r *http.Request) {
    14  	runtime := r.Context().Value(api.RuntimeKey).(*libpod.Runtime)
    15  	name := utils.GetName(r)
    16  
    17  	possiblyNormalizedName, err := utils.NormalizeToDockerHub(r, name)
    18  	if err != nil {
    19  		utils.Error(w, http.StatusInternalServerError, errors.Wrap(err, "error normalizing image"))
    20  		return
    21  	}
    22  
    23  	newImage, _, err := runtime.LibimageRuntime().LookupImage(possiblyNormalizedName, nil)
    24  	if err != nil {
    25  		utils.ImageNotFound(w, possiblyNormalizedName, errors.Wrapf(err, "failed to find image %s", possiblyNormalizedName))
    26  		return
    27  	}
    28  	history, err := newImage.History(r.Context())
    29  	if err != nil {
    30  		utils.InternalServerError(w, err)
    31  		return
    32  	}
    33  	allHistory := make([]handlers.HistoryResponse, 0, len(history))
    34  	for _, h := range history {
    35  		l := handlers.HistoryResponse{
    36  			ID:        h.ID,
    37  			Created:   h.Created.Unix(),
    38  			CreatedBy: h.CreatedBy,
    39  			Tags:      h.Tags,
    40  			Size:      h.Size,
    41  			Comment:   h.Comment,
    42  		}
    43  		allHistory = append(allHistory, l)
    44  	}
    45  	utils.WriteResponse(w, http.StatusOK, allHistory)
    46  }