Commit 0e529268 authored by Yuanle Song's avatar Yuanle Song
Browse files

support multiple ways of specifying target

cli option > env var > config file
parent 98d8bb36
Loading
Loading
Loading
Loading
+76 −65
Original line number Diff line number Diff line
@@ -38,7 +38,7 @@ with
        member s.Usage =
            match s with
            | Dry_Run _ -> "only show what will be done, do not transfer any file"
            | Target _ -> "rsync target, could be local dir or remote ssh dir"
            | Target _ -> "rsync target, could be local dir in Windows or mingw format or remote ssh dir"
            | Remote_User _ -> "remote linux user to own the backup files"
            | Itemize_Changes _ -> "add -i option to rsync"
            | Node_Name _ -> "local node's name, used in remote logging"
@@ -66,7 +66,9 @@ let runtimeDir = appDataLocalDir + "mbackup/"

let mbackupConfigFile = userConfigDirWin + "mbackup.txt"

let isLocalTarget (target: string) = target.StartsWith "/"
// return true if target is a local dir. local dir can be unix style or windows style.
let isLocalTarget (target: string) =
  target.StartsWith "/" || Regex.IsMatch(target, "^[c-z]:", RegexOptions.IgnoreCase)

// expand user file to mingw64 rsync supported path.
// abc -> /cygdrive/c/Users/<user>/abc
@@ -176,14 +178,25 @@ let main argv =
    let sshPrivateKeyFile = results.GetResult(Ssh_Key, defaultValue = userHome + ".ssh/id_rsa") |> toMingwPath
    let rsyncCmd = List.append rsyncCmd [sprintf "-e \"%s -F %s -i %s -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null\"" sshExeFile sshConfigFile sshPrivateKeyFile]

    // precedence: command line argument > environment variable > config file
    let normalizeTarget target =
        if isLocalTarget target then
            toMingwPath target
        else
            target
    let backupTargetMaybe =
        match results.TryGetResult Target with
        | None ->
            let mbackupConfig = WellsConfig(mbackupConfigFile)
            let backupTargetMaybe = mbackupConfig.GetStr("target")
            Option.map normalizeTarget backupTargetMaybe
        | Some backupTarget ->
            Some (normalizeTarget backupTarget)
    match backupTargetMaybe with
    | None ->
        logger.Error "TARGET is not defined"
        ExitBadParam
    | Some backupTarget ->
          let backupTarget = toMingwPath backupTarget
        let rsyncCmd =
          if not (isLocalTarget backupTarget)
            then
@@ -195,11 +208,9 @@ let main argv =
              rsyncCmd
            else
              rsyncCmd

        let rsyncCmd = List.append rsyncCmd ["/"]
        let rsyncCmd = List.append rsyncCmd [backupTarget]
        let rsyncArgs = rsyncCmd |> String.concat " "

        let rsyncExe = mbackupInstallDirWinTest + "rsync-w64\\usr\\bin\\rsync.exe"
        let echoExe = "C:\\Program Files\\Git\\usr\\bin\\echo.exe"
        try
+3 −0
Original line number Diff line number Diff line
# example config file
# target=d:\backup\
# target=myhost.example.com:/data/backup/somedir/
+12 −0
Original line number Diff line number Diff line
@@ -2,6 +2,7 @@ module MbackupTests

open NUnit.Framework
open Mbackup.Lib
open Mbackup.Program

[<SetUp>]
let Setup () =
@@ -57,3 +58,14 @@ let TestStringSplit () =
  let r = "a=b=c".Split('=', 2)
  Assert.That("a", Is.EqualTo(r.[0]))
  Assert.That("b=c", Is.EqualTo(r.[1]))

[<Test>]
let TestIsLocalTarget () =
  Assert.That(isLocalTarget("D:\\backup"))
  Assert.That(isLocalTarget("d:\\backup"))
  Assert.That(isLocalTarget("C:\\backup"))
  Assert.That(isLocalTarget("d:/backup"))
  Assert.That(isLocalTarget("D:/backup"))
  Assert.That(isLocalTarget("F:\\mbackup"))
  Assert.That(isLocalTarget("/cygdrive/d/backup"))
  Assert.That(isLocalTarget("/d/backup"))
+11 −1
Original line number Diff line number Diff line
@@ -20,7 +20,7 @@ remote logging works.
read target from config file works. now I can just run
dotnet run -- -i

** 2019-11-12 docs
** 2019-11-12 docs                                                :documents:
- rsync
  https://www.samba.org/ftp/rsync/rsync.html
- Basic Editing in Visual Studio Code
@@ -86,6 +86,16 @@ C:\ProgramData\mbackup\mbackup-default.list
C:\ProgramData\mbackup\user-default.list
C:\ProgramData\mbackup\mbackup.ini

** 2019-11-14 notes                                             :development:
- Argu optional param support.

  if option Target is optional, when try to get its value, you should use

  results.TryGetResult Target
  or
  results.GetResult(Target, defaultValue = xxx)
- 

* later                                                               :entry:
** 2019-11-14 supports expand Downloads dir in user-default.list
** 2019-11-14 vscode f# doesn't support open a module