def main()

in marketplace/deployer_util/set_app_labels.py [0:0]


def main():
  parser = ArgumentParser(description=_PROG_HELP)
  parser.add_argument(
      "--manifests",
      help="The folder containing the manifest templates, "
      "or - to read from stdin",
      required=True)
  parser.add_argument(
      "--dest",
      help="The output file for the resulting manifest, "
      "or - to write to stdout",
      required=True)
  parser.add_argument(
      "--name", help="The name of the application instance", required=True)
  parser.add_argument(
      "--namespace",
      help="The namespace where the application is installed",
      required=True)
  args = parser.parse_args()

  resources = []
  if args.manifests == "-":
    resources = parse_resources_yaml(sys.stdin.read())
  elif os.path.isfile(args.manifests):
    resources = load_resources_yaml(args.manifests)
  else:
    resources = []
    for filename in os.listdir(args.manifests):
      resources += load_resources_yaml(os.path.join(args.manifests, filename))

  # Modify resources inlined.
  for resource in resources:
    labels = resource['metadata'].get('labels', {})
    resource['metadata']['labels'] = labels
    labels['app.kubernetes.io/name'] = args.name
    # For a resource that doesn't have a namespace (i.e. cluster resource),
    # also all label it with the namespace of the application.
    if 'namespace' not in resource['metadata']:
      labels['app.kubernetes.io/namespace'] = args.namespace

  if args.dest == "-":
    write_resources(resources, sys.stdout)
    sys.stdout.flush()
  else:
    with open(args.dest, "w", encoding='utf-8') as outfile:
      write_resources(resources, outfile)