listDockerImages function

Future<List<DockerImage>> listDockerImages()

Get a list of all Docker images

Implementation

Future<List<DockerImage>> listDockerImages()async{
  List<DockerImage> images = [];
  ProcessResult result = await Process.run(
    dockerCommand,
    [
      'images',
      '--format=json',
    ],
    runInShell: true,
  );
  if(result.exitCode == 0){
    String resultAsString = result.stdout;
    resultAsString = resultAsString.replaceAll("}\n{", "}[split_me]{");
    List<String> jsons = resultAsString.split("[split_me]");
    //print(jsons);
    for(String json in jsons){
      try{
        Map<String,dynamic> parsedJSON = jsonDecode(json);
        images.add(DockerImage(
          containers: parsedJSON["Containers"],
          createdAt: parsedJSON["CreatedAt"],
          createdSince: parsedJSON["CreatedSince"],
          digest: parsedJSON["Digest"],
          id: parsedJSON["ID"],
          repository: parsedJSON["Repository"],
          sharedSize: parsedJSON["SharedSize"],
          size: parsedJSON["Size"],
          tag: parsedJSON["Tag"],
          uniqueSize: parsedJSON["UniqueSize"],
          virtualSize: parsedJSON["VirtualSize"],
        ));
      }catch(error){
        //Do nothing
      }
    }
  }else{
    throw result.stderr;
  }
  return images;
}