github.com/containers/libpod@v1.9.4-0.20220419124438-4284fd425507/pkg/api/handlers/compat/images_history.go (about)

     1  package compat
     2  
     3  import (
     4  	"net/http"
     5  
     6  	"github.com/containers/libpod/libpod"
     7  	"github.com/containers/libpod/pkg/api/handlers"
     8  	"github.com/containers/libpod/pkg/api/handlers/utils"
     9  	"github.com/pkg/errors"
    10  )
    11  
    12  func HistoryImage(w http.ResponseWriter, r *http.Request) {
    13  	runtime := r.Context().Value("runtime").(*libpod.Runtime)
    14  	name := utils.GetName(r)
    15  	var allHistory []handlers.HistoryResponse
    16  
    17  	newImage, err := runtime.ImageRuntime().NewFromLocal(name)
    18  	if err != nil {
    19  		utils.Error(w, "Something went wrong.", http.StatusNotFound, errors.Wrapf(err, "Failed to find image %s", name))
    20  		return
    21  
    22  	}
    23  	history, err := newImage.History(r.Context())
    24  	if err != nil {
    25  		utils.InternalServerError(w, err)
    26  		return
    27  	}
    28  	for _, h := range history {
    29  		l := handlers.HistoryResponse{
    30  			ID:        h.ID,
    31  			Created:   h.Created.Unix(),
    32  			CreatedBy: h.CreatedBy,
    33  			Tags:      h.Tags,
    34  			Size:      h.Size,
    35  			Comment:   h.Comment,
    36  		}
    37  		allHistory = append(allHistory, l)
    38  	}
    39  	utils.WriteResponse(w, http.StatusOK, allHistory)
    40  }