Files
duplicati/Duplicati/CommandLine/BackendTool/Program.cs
T

165 lines
7.7 KiB
C#
Raw Normal View History

2013-05-05 13:44:25 +02:00
#region Disclaimer / License
// Copyright (C) 2011, Kenneth Skovhede
// http://www.hexad.dk, opensource@hexad.dk
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
//
#endregion
using System;
using System.Collections.Generic;
using System.Text;
using Duplicati.Library.Interface;
namespace Duplicati.CommandLine.BackendTool
{
class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
public static int Main(string[] args)
{
return Duplicati.Library.AutoUpdater.UpdaterManager.RunFromMostRecent(typeof(Program).GetMethod("RealMain"), args);
}
2014-07-09 22:15:27 +02:00
public static int RealMain(string[] _args)
2013-05-05 13:44:25 +02:00
{
2014-06-09 09:12:15 +02:00
bool debugoutput = false;
2013-05-05 13:44:25 +02:00
try
{
List<string> args = new List<string>(_args);
Dictionary<string, string> options = Library.Utility.CommandLineParser.ExtractOptions(args);
if (!options.ContainsKey("auth_password") && !string.IsNullOrEmpty(System.Environment.GetEnvironmentVariable("AUTH_PASSWORD")))
options["auth_password"] = System.Environment.GetEnvironmentVariable("AUTH_PASSWORD");
2013-05-05 13:44:25 +02:00
if (!options.ContainsKey("auth_username") && !string.IsNullOrEmpty(System.Environment.GetEnvironmentVariable("AUTH_USERNAME")))
options["auth_username"] = System.Environment.GetEnvironmentVariable("AUTH_USERNAME");
2013-05-05 13:44:25 +02:00
if (options.ContainsKey("tempdir") && !string.IsNullOrEmpty(options["tempdir"]))
Library.Utility.TempFolder.SystemTempPath = options["tempdir"];
2014-06-09 09:12:15 +02:00
debugoutput = Duplicati.Library.Utility.Utility.ParseBoolOption(options, "debug-output");
2013-05-05 13:44:25 +02:00
string command = null;
if (args.Count >= 2)
{
if (args[0].Equals("list", StringComparison.InvariantCultureIgnoreCase))
command = "list";
else if (args[0].Equals("get", StringComparison.InvariantCultureIgnoreCase))
command = "get";
else if (args[0].Equals("put", StringComparison.InvariantCultureIgnoreCase))
command = "put";
else if (args[0].Equals("delete", StringComparison.InvariantCultureIgnoreCase))
command = "delete";
else if (args[0].Equals("create-folder", StringComparison.InvariantCultureIgnoreCase))
command = "create";
2014-06-09 09:12:25 +02:00
else if (args[0].Equals("createfolder", StringComparison.InvariantCultureIgnoreCase))
command = "create";
2013-05-05 13:44:25 +02:00
}
2013-05-05 13:51:23 +02:00
if (args.Count < 2 || args[0].ToLower() == "help" || args[0] == "?" || command == null)
2013-05-05 13:44:25 +02:00
{
if (command == null && args.Count >= 2)
{
Console.WriteLine("Unsupported command: {0}", args[0]);
Console.WriteLine();
}
Console.WriteLine("Usage: <command> <protocol>://<username>:<password>@<path> [filename]");
Console.WriteLine("Example: LIST ftp://user:pass@server/folder");
Console.WriteLine();
Console.WriteLine("Supported backends: " + string.Join(",", Duplicati.Library.DynamicLoader.BackendLoader.Keys));
Console.WriteLine("Supported commands: GET PUT LIST DELETE CREATEFOLDER");
return 200;
}
using(var backend = Library.DynamicLoader.BackendLoader.GetBackend(args[1], options))
{
if (backend == null)
throw new Exception("Backend not supported");
if (command == "list")
{
if (args.Count != 2)
throw new Exception(string.Format("too many arguments: {0}", string.Join(",", args)));
Console.WriteLine(string.Format("{0}\t{1}\t{2}\t{3}", "Name", "Dir/File", "LastChange", "Size"));
foreach(var e in backend.List())
Console.WriteLine(string.Format("{0}\t{1}\t{2}\t{3}", e.Name, e.IsFolder ? "Dir" : "File", e.LastModification, e.Size < 0 ? "" : Library.Utility.Utility.FormatSizeString(e.Size)));
return 0;
}
else if (command == "create")
{
if (args.Count != 2)
throw new Exception(string.Format("too many arguments: {0}", string.Join(",", args)));
2013-05-05 17:07:24 +02:00
backend.CreateFolder();
2013-05-05 13:44:25 +02:00
return 0;
}
else if (command == "delete")
{
if (args.Count < 3)
throw new Exception("DELETE requires a filename argument");
if (args.Count > 3)
throw new Exception(string.Format("too many arguments: {0}", string.Join(",", args)));
backend.Delete(args[2]);
return 0;
}
else if (command == "get")
{
if (args.Count < 3)
throw new Exception("GET requires a filename argument");
if (args.Count > 3)
throw new Exception(string.Format("too many arguments: {0}", string.Join(",", args)));
2013-05-05 13:51:23 +02:00
if (System.IO.File.Exists(args[2]))
throw new Exception("File already exists, not overwriting!");
2013-05-05 13:44:25 +02:00
backend.Get(args[2], args[2]);
return 0;
}
else if (command == "put")
{
if (args.Count < 3)
throw new Exception("PUT requires a filename argument");
if (args.Count > 3)
throw new Exception(string.Format("too many arguments: {0}", string.Join(",", args)));
backend.Put(args[2], args[2]);
return 0;
}
throw new Exception("Internal error");
}
}
catch (Exception ex)
{
Console.WriteLine("Command failed: " + ex.Message);
2014-06-09 09:12:15 +02:00
if (debugoutput)
Console.WriteLine(ex.ToString());
2013-05-05 13:44:25 +02:00
return 100;
}
}
}
}