github.com/containers/podman/v2@v2.2.2-0.20210501105131-c1e07d070c4c/pkg/api/handlers/compat/images_history.go (about)

     1  package compat
     2  
     3  import (
     4  	"net/http"
     5  
     6  	"github.com/containers/podman/v2/libpod"
     7  	"github.com/containers/podman/v2/pkg/api/handlers"
     8  	"github.com/containers/podman/v2/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  
    16  	newImage, err := runtime.ImageRuntime().NewFromLocal(name)
    17  	if err != nil {
    18  		utils.Error(w, "Something went wrong.", http.StatusNotFound, errors.Wrapf(err, "failed to find image %s", name))
    19  		return
    20  
    21  	}
    22  	history, err := newImage.History(r.Context())
    23  	if err != nil {
    24  		utils.InternalServerError(w, err)
    25  		return
    26  	}
    27  	allHistory := make([]handlers.HistoryResponse, 0, len(history))
    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  }