Module: Libis::Tools::Cli::Reorg

Included in:
Libis::Tools::CommandLine
Defined in:
lib/libis/tools/cli/reorg.rb

Constant Summary collapse

DEFAULT_CONFIG =

noinspection RubyExpressionInStringInspection

{
    base: '.',
    filter: '^(.*)$',
    expression: 'target/#{file_name}',
    action: 'move',
    overwrite: false,
    interactive: false,
    report: nil,
    dummy: false,
    config: nil,
    unattended: false
}
VALID_ACTIONS =

noinspection RubyStringKeysInHashInspection

{
    'move' => 'moved',
    'copy' => 'copied',
    'link' => 'linked'
}
STRING_CONFIG =
{
    base: "Source Directory to organize",
    filter: "File matching filter",
    expression: "New file path expression",
    action: "Action to perform",
    overwrite: "Overwite target files if newer",
    interactive: "Ask for action on changed files",
    report: "Report file",
    dummy: "Perform phantom actions (not affecting files)",
    config: "Load saved configuration parameters"
}
REQ_HEADERS =
{term: 'Term'}
OPT_HEADERS =
{pid: 'Pid', filename: 'File'}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



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
# File 'lib/libis/tools/cli/reorg.rb', line 45

def self.included(klass)
  klass.class_exec do
    def klass.description(field)
      "#{STRING_CONFIG[field]}." + (DEFAULT_CONFIG[field].nil? ? '' : " default: #{DEFAULT_CONFIG[field]}")
    end

    desc 'reorg [options]', 'Reorganize files'
    long_desc <<-DESC
      
    'reorg [options]' will reorganize files based on the name of the files.

    The base directory will be scanned for files that match the FILTER regular expression. For each matching
    file, an action will be performed. The outcome of the action is determined by the expression that is given.

    The expression will be evaluated as a Ruby string expression and supports string interpolation in the form
    '\#{<thing>}', where <thing> can be any of:

    . $x : refers to the x-th group in the FILTER. Groups are numbered by the order of the opening '('

    . file_name : the original file name

    The action that will be performed on the action depens on the configured ACTION. The valid ACTIONs are; 
    'move', copy' and 'link'. Please note that in the latter case only the files will be soft-linked and any 
    directory in the target path will be created. The tool will therefore never create soft-links to directories. 
    The soft-links are created with an absolute reference path. This allows you to later move and rename the 
    soft-links later as you seem fit without affecting the source files. You could for instance run this tool on 
    the soft-links with the 'move' action to do so.

    By default, if the target file already exists, the file ACTION will not be performed. The '--overwrite'
    option will cause the tool to compare the file dates and checksums of source and target files in that case.
    Only if the checksums are different and the source file has a more recent modification date, the target file 
    will be overwritten. If you want to be asked for overwrite confirmation for each such file, you can add the 
    '--interactive' option.

    The tool can generate a report on all the file actions that have been performed. To do so, specify a file
    name for the '--report' option. The format of the report will be determined by the file extension you supply:

    - *.csv : comma-separated file

    - *.tsv : tab-separated file

    - *.yml : YAML file

    - *.xml : XML file

    By adding the --dummy option, you can test your settings without performing the real actions on the file.
    The tool will still report on its progress as if it would perform the actions.

    All the options can be saved into a configuration file to be reused later. You can specify which 
    configuration file you want to use with the '--config' option. If you specify a configuration file, the tool
    will first load the options from the configuration file and then process the command-line options. The 
    command-line options therefore have priority over the options in the configuration file.

    By default the tool allows you to review the activated options and gives you the opportunity to modify them
    before continuing of bailing out. If you are confident the settings are fine, you can skip this with the
    '--unatttended' option. Handle with care!

    Unless you have specified the '--unattended' options, you will be presented with a menu that allows you to
    change the configuration parameters, run the tool with the current config or bail out.

    DESC

    method_option :base, aliases: '-b',
                  desc: description(:base)
    method_option :filter, aliases: '-f',
                  desc: description(:filter)
    method_option :expression, aliases: '-e',
                  desc: description(:expression)

    method_option :action, aliases: '-a', enum: VALID_ACTIONS.keys,
                  desc: description(:action)
    method_option :overwrite, aliases: '-o', type: :boolean,
                  desc: description(:overwrite)
    method_option :interactive, aliases: '-i', type: :boolean,
                  desc: description(:interactive)

    method_option :report, aliases: '-r', banner: 'FILE',
                  desc: description(:report)

    method_option :dummy, aliases: '-d', type: :boolean,
                  desc: description(:dummy)

    method_option :config, aliases: '-c', type: :string,
                  desc: description(:config)

    method_option :unattended, aliases: '-u', type: :boolean,
                  desc: description(:unattended)

  end

end

Instance Method Details

#reorgObject



137
138
139
140
141
142
143
144
145
146
147
# File 'lib/libis/tools/cli/reorg.rb', line 137

def reorg
  @config_file_prefix = '.reorg.'

  # return config_write

  DEFAULT_CONFIG.each {|key, value| config.set(key, value: value) unless value.nil?}
  config_read(options[:config]) if options[:config]
  DEFAULT_CONFIG.each {|key, _| config.set(key, value: options[key]) if options.has_key?(key.to_s)}
  run_menu unless options[:unattended]
  do_reorg
end