Package uk.org.microbase.filesystem.cli

Source Code of uk.org.microbase.filesystem.cli.FSClient2

/*
* Copyright 2011 Keith Flanagan
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* File created: 20-Sep-2010, 17:48:17
*/
package uk.org.microbase.filesystem.cli;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Iterator;
import java.util.ServiceLoader;
import joptsimple.OptionParser;
import joptsimple.OptionSet;
import uk.org.microbase.filesystem.spi.MicrobaseFS;
import uk.org.microbase.filesystem.spi.MicrobaseFSFactory;

/**
*
* @author Keith Flanagan
*/
public class FSClient2
{
  public static void main(String[] args) throws IOException
  {
    OptionParser parser = new OptionParser()
    {


      {
        acceptsAll(Arrays.asList("h", "?", "help"), "show help");

        //Commands
        accepts("upload", "Upload a file");
        accepts("download", "Download a file");
        accepts("exists", "Determines whether a file at the specified "
            + "bucket/remote-name exists");
        accepts("list-bucket", "Lists the files in the specified bucket");

        //Parameters
        accepts("local-file", "Path to local file to upload/download").
            withRequiredArg().ofType(File.class).describedAs("file");
        accepts("bucket", "Source/destination bucket on remote filesystem").
            withRequiredArg().ofType(String.class).describedAs("bucket");
        accepts("remote-path", "Source/destination path on remote bucket").
            withRequiredArg().ofType(String.class).describedAs("remote-path");
        accepts("remote-name", "Source/destination file within a bucket on a remote filesystem").
            withRequiredArg().ofType(String.class).describedAs("remote-name");
      }
    };

    OptionSet options = null;
    try
    {
      options = parser.parse(args);
    }
    catch (Exception e)
    {
      System.out.println(e.getMessage());
      System.out.println("Use --help for a list of valid command line parameters");
      System.exit(1);
    }

    if (options.has("?"))
    {
      parser.printHelpOn(System.out);
      System.exit(0);
    }


    System.out.println("Value of 'q' is: " + options.valueOf("q"));

    System.out.println("SPI Test **************************************");

    System.out.println("Found the following implementations:");

    String filename = "foo.txt";
    String sourcePath = "/tmp/" + filename;
    File sourceFile = new File(sourcePath);
    MicrobaseFS fs = MicrobaseFSFactory.getDefaultProvider();
    System.out.println("Classname: " + fs.getClass().toString());

    String bucketName = "bucket1";
    String path = "a/file/path";
    try
    {
      fs.upload(sourceFile, bucketName, path, sourceFile.getName(), null);
      System.out.println("Uploaded file successfully");

      System.out.println("Bucket root directory list:");
      System.out.println(fs.listFiles(bucketName));

      System.out.println("Bucket/path directory list");
      System.out.println(fs.listFiles(bucketName, path));

      System.out.println(filename + " exists? " + fs.exists(bucketName, path, filename));
      System.out.println("bar.txt" + " exists? " + fs.exists(bucketName, path, "bar.txt"));
      System.out.println("baz.txt" + " exists? " + fs.exists(bucketName, path, "baz.txt"));

      System.out.println("Bucket list for bucket2: "+fs.listFiles("bucket2"));

    }
    catch (Exception e)
    {
      e.printStackTrace();
    }

  }
}
TOP

Related Classes of uk.org.microbase.filesystem.cli.FSClient2

TOP
Copyright © 2015 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.