Skip to content

dliulabs/groovy-vfs

 
 

Repository files navigation

A DSL for Groovy to wrap around the Apache VFS libraries.

If you like it, then tweet about it using #groovyvfs as the hashtag.

Groovy Code

Download

@Grapes([
	@Grab( 'org.ysb33r.groovy:groovy-vfs:0.5' ),
	@Grab( 'commons-net:commons-net:3.+' ), // If you want to use ftp 
    @Grab( 'commons-httpclient:commons-httpclient:3.1'), // If you want http/https
    @Grab( 'com.jcraft:jsch:0.1.48' ) // If you want sftp
])
import org.ysb33r.groovy.dsl.vfs.VFS

def vfs = new VFS()
 
// Simple copy operation
vfs.cp 'ftp://foo.example/myfile', 'sftp://bar.example/yourfile'
 
// Utilising the DSL
vfs {
   
    // Copy file from one site to anther using two different protocols
    cp 'http://first.example/myfile', 'sftp://second.example/yourfile'
 
    // Not implemented yet - move file between two sites using different protocols
    mv 'sftp://second.example/yourfile', 'ftp://third.example/theirfile'
 
    // Lists all files on a remote site
    ls ('http://first.example') {
        println it.name
    }
  
    // Streams the output
    cat ('http://first.example/myfile') { strm->
        println strm.text
    }
 
    // Create a new folder on a remote site
    mkdir 'sftp://second.example/my/new/folder'
    
    // Change default options via property Map
    options 'vfs.ftp.passiveMode' : true
 
    // Change default options DSL style
    options {
        ftp {
            passiveMode true
        }
    }
 
    // Use options on a per URL basis
    cp 'ftp://first.example/myfile?vfs.ftp.passiveMode=1', 'sftp://second.example/yourfile?vfs.sftp.compression=zlib'
    
    // Download a compressed archive and unpack to local directory
    cp 'tbz2:ftp:/first.example/myFiles.tar.bz2", new File( '../unpack-here' ), recursive:true
     
}

Gradle plugin (INCUBATING)

Download

It is now possible to use this in Gradle as an extension to the project class. The interface is very experimental and may change without much warning in future releases of this plugin.

buildscript {
    repositories {
        jcenter()
	mavenCentral()
      }
      dependencies {
        classpath 'org.ysb33r.gradle:vfs-gradle-plugin:0.5.1'
        classpath 'commons-net:commons-net:3.+'  // If you want to use ftp 
        classpath 'commons-httpclient:commons-httpclient:3.1' // If you want http/https
        classpath 'com.jcraft:jsch:0.1.48'  // If you want sftp
      }
}
apply plugin : 'org.ysb33r.vfs'

// Create a VFS task
task copyReadme << { 
  vfs {
    cp 'https://raw.github.com/ysb33r/groovy-vfs/master/README.md', new File("${buildDir}/tmp/README.md")
  }
}

// it is also possible to update global options for vfs
vfs {
  options {
    http {
      maxTotalConnections 4
    }
  }
}

If you want to see what VFS is going run gradle with --debug

Adding extra plugins

From v0.6 onwards additional plugins can be loaded via a new extend block. For more details see this gist: https://gist.github.com/ysb33r/9916940

SMB provider (EXPERIMENTAL)

A provider for accessing SMB shares is now possible and will be supported from v0.6 onwards. The plugin must be loaded separately.

@Grab( 'org.ysb33r.groovy:groovy-vfs-smb-provider:0.0.1-SNAPSHOT' ),
@Grab( 'jcifs:jcifs:1.3.17' ),

vfs {
  extend {
    provider className: 'org.ysb33r.groovy.vfsplugin.smb.SmbFileProvider', schemes: ['smb','cifs']
  }

  cp 'smb://someserver/share/dir/file', new File('localfile.txt)
}

NOTE: when embedding windows credentials in the URL use %5C in place of backslash i.e.

  smb://DOMAIN%5cUSERNAME:PASSWORD@HOSTNAME/SHARE/PATH

Documentation

Credits

It is seldom that these kind of libraries happen in isolation. It is therefore prudent that I acknowledge the inputs of others in the creation of groovy-vfs

About

A DSL for Groovy on top of Apache VFS2

Resources

License

Stars

Watchers

Forks

Packages

No packages published