#!/usr/bin/python3

import argparse
import os
import sys
import time

from grove import config as grove_config
from grove import deploy as grove_deploy

def main():
  parser = argparse.ArgumentParser(description='Read service history.')
  args = parser.parse_args()

  config = grove_config.load_config()
  history = grove_deploy.read_history(config)["history"]

  for history_entry in history:
    hashed_service_parts = history_entry["hashed_service_parts"]
    deploy_directory = grove_deploy.make_deploy_directory(config, hashed_service_parts)
    service_yaml, service_type = grove_deploy.read_deploy_json(os.path.join(deploy_directory, "deploy.json"))
    service_pretty = "{}/{}".format(service_yaml["name"], service_type)
    deploy_pretty = ", ".join(service_yaml["deploy"])
    print("{0:24}{1:16}{2:25}{3}".format(
      time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime(history_entry["timestamp"])),
      hashed_service_parts,
      service_pretty,
      deploy_pretty
    ))

main()
sys.exit(0)
