2024-03-04 12:21:53 +01:00
// Copyright (C) 2024, The Duplicati Team
// https://duplicati.com, hello@duplicati.com
2019-09-06 16:57:44 -04:00
//
2024-03-04 12:21:53 +01:00
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
2019-09-06 16:57:44 -04:00
//
2024-03-04 12:21:53 +01:00
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
2019-09-06 16:57:44 -04:00
//
2024-03-04 12:21:53 +01:00
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
2019-09-06 16:57:44 -04:00
using System ;
2024-03-15 14:18:56 +01:00
using System.IO ;
2019-09-06 16:57:44 -04:00
using System.Linq ;
2018-10-27 12:17:07 +02:00
2024-03-04 12:21:53 +01:00
namespace Duplicati.Library.AutoUpdater
2019-09-06 16:57:44 -04:00
{
public static class Program
{
2024-03-15 14:18:56 +01:00
public static int Main ( string [] _args )
2019-09-06 16:57:44 -04:00
{
2024-03-15 14:18:56 +01:00
var args = _args . ToList ();
2019-09-06 16:57:44 -04:00
Duplicati . Library . Utility . CommandLineParser . ExtractOptions ( args );
if ( args . Count == 0 )
{
WriteUsage ();
return 100 ;
}
else if ( args . Count != 1 )
{
Console . WriteLine ( "Invalid number of arguments, got {0}:{1}{1}{2}{1}{1}" , args . Count , Environment . NewLine , string . Join ( Environment . NewLine , args ));
return 100 ;
}
UpdaterManager . OnError += ( ex ) => Console . WriteLine ( "Error detected: {0}" , ex );
var cmd = args [ 0 ]. ToLowerInvariant (). Trim ();
switch ( cmd )
{
case "help" :
WriteUsage ();
return 0 ;
case "check" :
{
var update = UpdaterManager . CheckForUpdate ();
if ( update == null )
Console . WriteLine ( "No updates found" );
else if ( update . Version == UpdaterManager . SelfVersion . Version )
Console . WriteLine ( "You are running the latest version: {0}" , update . Displayname );
else
Console . WriteLine ( "New version is available: {0}" , update . Displayname );
return 0 ;
}
2024-03-15 14:18:56 +01:00
case "download" :
2019-09-06 16:57:44 -04:00
{
var update = UpdaterManager . CheckForUpdate ();
if ( update == null || update . Version == UpdaterManager . SelfVersion . Version )
{
Console . WriteLine ( "You are running the latest version: {0} ({1})" , UpdaterManager . SelfVersion . Displayname , System . Reflection . Assembly . GetExecutingAssembly (). GetName (). Version );
return 0 ;
}
2024-03-15 14:18:56 +01:00
var package = update . FindPackage ();
if ( package == null )
{
Console . WriteLine ( $"Failed to locate a matching package for this machine, please visit this link and select the correct package: {update.GetGenericUpdatePageUrl()}" );
}
else
{
var filename = Path . GetFullPath ( package . GetFilename ());
Console . WriteLine ( "Downloading update \"{0}\" to {1} ..." , update . Displayname , filename );
long lastpg = 0 ;
2024-04-15 13:17:49 +02:00
UpdaterManager . DownloadUpdate ( update , package , filename , f =>
{
var npg = ( long )( f * 100 );
2024-03-15 14:18:56 +01:00
if ( Math . Abs ( npg - lastpg ) >= 5 || ( npg == 100 && lastpg != 100 ))
{
lastpg = npg ;
Console . WriteLine ( "Downloading {0}% ..." , npg );
}
});
}
2019-09-06 16:57:44 -04:00
2024-03-15 14:18:56 +01:00
Console . WriteLine ( "New version \"{0}\" downloaded!" , update . Displayname );
2019-09-06 16:57:44 -04:00
return 0 ;
}
default :
Console . WriteLine ( "Unknown command: \"{0}\"" , args [ 0 ]);
Console . WriteLine ();
Console . WriteLine ( "Try the command \"{0}\" instead" , "help" );
return 100 ;
}
}
private static void WriteUsage ()
{
2024-04-15 13:17:49 +02:00
Console . WriteLine ( "Usage:{0}\t{1}{2} [CHECK|DOWNLOAD|HELP]" , Environment . NewLine , PackageHelper . GetExecutableName ( PackageHelper . NamedExecutable . AutoUpdater ));
2019-09-06 16:57:44 -04:00
Console . WriteLine ();
Console . WriteLine ( "Environment variables:" );
Console . WriteLine ();
Console . WriteLine ( "{0} - Disables updates completely" , string . Format ( UpdaterManager . SKIPUPDATE_ENVNAME_TEMPLATE , AutoUpdateSettings . AppName ));
Console . WriteLine ( "{0} - Use alternate updates urls" , string . Format ( AutoUpdateSettings . UPDATEURL_ENVNAME_TEMPLATE , AutoUpdateSettings . AppName ));
2024-04-15 13:17:49 +02:00
Console . WriteLine ( "{0} - Choose different channel than the default {1}, valid settings: {2}" , string . Format ( AutoUpdateSettings . UPDATECHANNEL_ENVNAME_TEMPLATE , AutoUpdateSettings . AppName ), AutoUpdater . AutoUpdateSettings . DefaultUpdateChannel , string . Join ( "," , Enum . GetNames ( typeof ( ReleaseType )). Where ( x => x != ReleaseType . Unknown . ToString ())));
2019-09-06 16:57:44 -04:00
Console . WriteLine ();
Console . WriteLine ( "Updates are downloaded from: {0}" , string . Join ( ";" , AutoUpdateSettings . URLs ));
2024-03-15 14:18:56 +01:00
Console . WriteLine ( "Machine settings are installed in: {0}" , UpdaterManager . UPDATEDIR );
2024-04-15 13:17:49 +02:00
Console . WriteLine ( "This version is \"{0}\" ({1}) and is installed in: {2}" , UpdaterManager . SelfVersion . Displayname , System . Reflection . Assembly . GetExecutingAssembly (). GetName (). Version , UpdaterManager . INSTALLATIONDIR );
2019-09-06 16:57:44 -04:00
Console . WriteLine ();
}
}
}