#!/usr/bin/env python
# vsphere-get-files --- retrieve a file from a datastore

# Author: Noah Friedman <friedman@splode.com>
# Created: 2018-11-13
# Public domain

# $Id: vsphere-get-files,v 1.2 2018/12/11 08:03:20 friedman Exp $

# Commentary:

# TODO:
#	* retrieve modification time and restore on local copies
#	* create file hierarchy mapping remote files, instead of writing
#	  everything to one file.
#	* more error checking, especially overwriting existing files

# Code:

from   __future__ import print_function
from   pyVmomi    import vim, vmodl
import vspherelib     as vsl
import sys

def get_args():
    p = vsl.ArgumentParser( loadrc=True )
    p.add( '-d', '--datastore',           help='Default datastore for remaining paths' )
    p.add( '-O', '--output', default='-', help='Write output to named file; default is stdout' )
    p.add( 'files', nargs='+',            help='Remote file(s) to retrieve' )
    return p.parse()

def open_output( name ):
    if name in ['-', '/dev/stdout', '/proc/self/fd/1' ]:
        return sys.stdout
    else:
        return open( name, 'wb' )

def main():
    args = get_args()
    vsi  = vsl.vmomiConnect( args )

    with open_output( args.output ) as out:
        for dsfile in args.files:
            with vsi.datastore_file_ops( dsfile, dsName=args.datastore ) as dsop:
                for chunk in dsop.get():
                    out.write( chunk )

if __name__ == '__main__':
    try:
        main()
    except KeyboardInterrupt:
        pass

# vsphere-get-files
