Merge pull request #70 from liangchenye/read-manifest
contrib: load image history from 'manifest.json'
This commit is contained in:
commit
1866821a80
@ -72,7 +72,10 @@ func main() {
|
||||
|
||||
// Retrieve history
|
||||
fmt.Println("Getting image's history")
|
||||
layerIDs, err := history(imageName)
|
||||
layerIDs, err := historyFromManifest(path)
|
||||
if err != nil {
|
||||
layerIDs, err = historyFromCommand(imageName)
|
||||
}
|
||||
if err != nil || len(layerIDs) == 0 {
|
||||
log.Fatalf("- Could not get image's history: %s\n", err)
|
||||
}
|
||||
@ -168,7 +171,34 @@ func save(imageName string) (string, error) {
|
||||
return path, nil
|
||||
}
|
||||
|
||||
func history(imageName string) ([]string, error) {
|
||||
func historyFromManifest(path string) ([]string, error) {
|
||||
mf, err := os.Open(path + "/manifest.json")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer mf.Close()
|
||||
|
||||
// https://github.com/docker/docker/blob/master/image/tarexport/tarexport.go#L17
|
||||
type manifestItem struct {
|
||||
Config string
|
||||
RepoTags []string
|
||||
Layers []string
|
||||
}
|
||||
|
||||
var manifest []manifestItem
|
||||
if err = json.NewDecoder(mf).Decode(&manifest); err != nil {
|
||||
return nil, err
|
||||
} else if len(manifest) != 1 {
|
||||
return nil, err
|
||||
}
|
||||
var layers []string
|
||||
for _, layer := range manifest[0].Layers {
|
||||
layers = append(layers, strings.TrimSuffix(layer, "/layer.tar"))
|
||||
}
|
||||
return layers, nil
|
||||
}
|
||||
|
||||
func historyFromCommand(imageName string) ([]string, error) {
|
||||
var stderr bytes.Buffer
|
||||
cmd := exec.Command("docker", "history", "-q", "--no-trunc", imageName)
|
||||
cmd.Stderr = &stderr
|
||||
|
Loading…
Reference in New Issue
Block a user