1 /**
2 Copyright: Copyright (c) 2019, Joakim Brännström. All rights reserved.
3 License: $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost Software License 1.0)
4 Author: Joakim Brännström (joakim.brannstrom@gmx.com)
5 */
6 module dsnapshot.config;
7 
8 import logger = std.experimental.logger;
9 import std.stdio : writeln;
10 
11 import colorlog;
12 
13 public import dsnapshot.types;
14 
15 struct Config {
16     import std.datetime : SysTime;
17     import std.variant : Algebraic, visit;
18     static import std.getopt;
19 
20     struct Help {
21     }
22 
23     struct Backup {
24         /// The name of the snapshot to backup. If none is specified then all are backed up.
25         Name name;
26         ///
27         int[] ignoreRsyncErrorCodes;
28         std.getopt.GetoptResult helpInfo;
29     }
30 
31     struct Remotecmd {
32         /// Single path to modify on the remote host.
33         string path;
34         RemoteSubCmd cmd;
35         std.getopt.GetoptResult helpInfo;
36     }
37 
38     struct Diskusage {
39         /// Name of the snapshot to calculate the disk usage of.
40         Name name;
41         std.getopt.GetoptResult helpInfo;
42     }
43 
44     struct Restore {
45         /// The name of the snapshot to restore.
46         Name name;
47         /// The time to restore.
48         SysTime time;
49         /// Path to restore the named snapshot to.
50         string restoreTo;
51         /// Delete files from restoreTo if they have been removed in src
52         bool deleteFromTo;
53 
54         std.getopt.GetoptResult helpInfo;
55     }
56 
57     struct Verifyconfig {
58     }
59 
60     struct Global {
61         /// Configuration file to read
62         Path confFile;
63 
64         VerboseMode verbosity;
65         bool help;
66         std.getopt.GetoptResult helpInfo;
67         string progName;
68     }
69 
70     alias Type = Algebraic!(Help, Backup, Remotecmd, Diskusage, Restore, Verifyconfig);
71     Type data;
72 
73     Global global;
74     Snapshot[] snapshots;
75 
76     void printHelp() {
77         import std.format : format;
78         import std.getopt : defaultGetoptPrinter;
79         import std.path : baseName;
80         import std..string : toLower;
81 
82         defaultGetoptPrinter(format("usage: %s <command>\n", global.progName),
83                 global.helpInfo.options);
84         writeln("Command groups:");
85         static foreach (T; Type.AllowedTypes) {
86             writeln("  ", T.stringof.toLower);
87         }
88 
89         data.visit!((Help a) {}, (Backup a) {
90             defaultGetoptPrinter("backup:", a.helpInfo.options);
91         }, (Remotecmd a) {
92             defaultGetoptPrinter("remotecmd:", a.helpInfo.options);
93         }, (Diskusage a) {
94             defaultGetoptPrinter("diskusage:", a.helpInfo.options);
95         }, (Restore a) { defaultGetoptPrinter("restore:", a.helpInfo.options); }, (Verifyconfig a) {
96             writeln("verifyconfig:");
97             writeln("Verify the configuration file without actually executing it");
98         });
99     }
100 }