1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
|
// Copyright (C) 2015, The Duplicati Team
// http://www.duplicati.com, info@duplicati.com
//
// 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
using System;
using System.Linq;
using System.Collections.Generic;
namespace Duplicati.CommandLine.RecoveryTool
{
static 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);
}
private delegate int CommandRunner(List<string> args, Dictionary<string, string> options, Library.Utility.IFilter filter);
public static int RealMain(string[] _args)
{
try
{
var args = new List<string>(_args);
var tmpparsed = Library.Utility.FilterCollector.ExtractOptions(args);
var options = tmpparsed.Item1;
var filter = tmpparsed.Item2;
if (!options.ContainsKey("auth_password") && !string.IsNullOrEmpty(System.Environment.GetEnvironmentVariable("AUTH_PASSWORD")))
options["auth_password"] = System.Environment.GetEnvironmentVariable("AUTH_PASSWORD");
if (!options.ContainsKey("auth_username") && !string.IsNullOrEmpty(System.Environment.GetEnvironmentVariable("AUTH_USERNAME")))
options["auth_username"] = System.Environment.GetEnvironmentVariable("AUTH_USERNAME");
if (options.ContainsKey("tempdir") && !string.IsNullOrEmpty(options["tempdir"]))
Library.Utility.TempFolder.SetSystemTempPath(options["tempdir"]);
bool isHelp = args.Count == 0 || (args.Count >= 1 && string.Equals(args[0], "help", StringComparison.InvariantCultureIgnoreCase));
if (!isHelp && ((options.ContainsKey("parameters-file") && !string.IsNullOrEmpty("parameters-file")) || (options.ContainsKey("parameter-file") && !string.IsNullOrEmpty("parameter-file")) || (options.ContainsKey("parameterfile") && !string.IsNullOrEmpty("parameterfile"))))
{
string filename;
if (options.ContainsKey("parameters-file") && !string.IsNullOrEmpty("parameters-file"))
{
filename = options["parameters-file"];
options.Remove("parameters-file");
}
else if (options.ContainsKey("parameter-file") && !string.IsNullOrEmpty("parameter-file"))
{
filename = options["parameter-file"];
options.Remove("parameter-file");
}
else
{
filename = options["parameterfile"];
options.Remove("parameterfile");
}
if (!ReadOptionsFromFile(filename, ref filter, args, options))
return 100;
}
var actions = new Dictionary<string, CommandRunner>(StringComparer.InvariantCultureIgnoreCase);
actions["download"] = Download.Run;
actions["index"] = Index.Run;
actions["list"] = List.Run;
actions["restore"] = Restore.Run;
actions["help"] = Help.Run;
CommandRunner command;
actions.TryGetValue(args.FirstOrDefault() ?? "", out command);
command = command ?? actions["help"];
return command(args, options, filter);
}
catch(Exception ex)
{
Console.WriteLine("Program crashed: {0}{1}", Environment.NewLine, ex.ToString());
return 200;
}
}
private static bool ReadOptionsFromFile(string filename, ref Library.Utility.IFilter filter, List<string> cargs, Dictionary<string, string> options)
{
try
{
List<string> fargs = new List<string>(Library.Utility.Utility.ReadFileWithDefaultEncoding(Library.Utility.Utility.ExpandEnvironmentVariables(filename)).Replace("\r\n", "\n").Replace("\r", "\n").Split(new String[] { "\n" }, StringSplitOptions.RemoveEmptyEntries).Select(x => x.Trim()));
var tmpparsed = Library.Utility.FilterCollector.ExtractOptions(fargs);
var opt = tmpparsed.Item1;
var newfilter = tmpparsed.Item2;
// If the user specifies parameters-file, all filters must be in the file.
// Allowing to specify some filters on the command line could result in wrong filter ordering
if (!filter.Empty && !newfilter.Empty)
throw new Exception("Filters cannot be specified on the commandline if filters are also present in the parameter file");
if (!newfilter.Empty)
filter = newfilter;
foreach(KeyValuePair<String, String> keyvalue in opt)
options[keyvalue.Key] = keyvalue.Value;
cargs.AddRange(
from c in fargs
where !string.IsNullOrWhiteSpace(c) && !c.StartsWith("#") && !c.StartsWith("!") && !c.StartsWith("REM ", StringComparison.InvariantCultureIgnoreCase)
select c
);
return true;
}
catch (Exception e)
{
Console.WriteLine(@"Unable to read the parameters file ""{0}"", reason: {1}", filename, e.Message);
return false;
}
}
}
}
|