Module: NdrPseudonymise::NdrEncrypt::CommandLine

Defined in:
lib/ndr_pseudonymise/ndr_encrypt/command_line.rb

Overview

ndr_encrypt command line utility entry points.

Constant Summary collapse

USAGE =

ndr_encrypt tool needs to support ruby 2.0 rubocop:disable Layout/HeredocIndentation, Rails/Exit, Style/SlicingWithRange

"usage: ndr_encrypt [-v | --version] [-h | --help]\n             <command> [<args>]\n\nThese are common ndr_encrypt commands used in various situations:\n\nstart a working area\n   init          Create an empty Git ndr_encrypt working copy\n\nwork with files\n   add           Add file contents to the encrypted store and index\n   rm            TODO: Remove files from the encrypted store and index\n\nencryption key rotation and repository maintenance\n   gc            Cleanup unnecessary index entries and optimize the encrypted store\n   resilver      TODO\n   retire-key    TODO\n\ndecrypt data\n   cat-files     TODO: Retrieve local file based on git_blobid\n   cat-remote    Retrieve remote file based on git_blobid\n   get           Retrieve local file(s) based on path in CSV index\n   TODO: decrypt single file without git_blobid\n\nLow-level Commands / Interrogators\n   cat-file      TODO: Provide content or type and size information for encrypted objects\n   ls-files      TODO: Show information about files in the index\n\nLow-level Commands / Manipulators\n   hash-object   TODO: Compute object ID and optionally create an encrypted object from a file\n".gsub(/^      /, '').freeze
COMMANDS =
%w[init add cat-remote gc get].freeze

Class Method Summary collapse

Class Method Details

.add(args, options) ⇒ Object

rubocop:disable Metrics/MethodLength



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/ndr_pseudonymise/ndr_encrypt/command_line.rb', line 112

def self.add(args, options) # rubocop:disable Metrics/MethodLength
  required_options = i[key_name pub_key]
  allowed_options = required_options
  unless (options.keys - allowed_options).empty? &&
         required_options.all? { |sym| options.key?(sym) }
    warn "usage: ndr_encrypt add --key_name=<keyname> --pub_key=<path> [<path>...]\n    USAGE\n    exit 1\n  end\n  if args.empty?\n    warn 'Nothing specified, nothing added.'\n    return\n  end\n\n  path = Dir.pwd\n  repo = NdrEncrypt::Repository.new(repo_dir: path)\n  repo.add(args, key_name: options[:key_name], pub_key: options[:pub_key])\nend\n"

.cat_remote(args, options) ⇒ Object

rubocop:disable Metrics/MethodLength



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/ndr_pseudonymise/ndr_encrypt/command_line.rb', line 132

def self.cat_remote(args, options) # rubocop:disable Metrics/MethodLength
  # TODO: Add -e option to check whether remote file exists
  required_options = i[key_name private_key base_url pretty_print]
  allowed_options = required_options + i[passin]
  unless (options.keys - allowed_options).empty? &&
         required_options.all? { |sym| options.key?(sym) } &&
         args.size == 1
    warn "usage: ndr_encrypt cat-remote --key_name=<keyname> --private_key=<path> --base_url=<url>\n                          -p <git_blobid>\n    USAGE\n    exit 1\n  end\n\n  git_blobid = args[0]\n  remote_store = NdrEncrypt::RemoteRepository.new(base_url: options[:base_url])\n  blob = remote_store.cat_remote(git_blobid, key_name: options[:key_name],\n                                             private_key: options[:private_key],\n                                             passin: options[:passin])\n  # TODO: Add error handling, for connection issues / file not found\n  $stdout.print blob\nend\n"

.gc(args, options) ⇒ Object



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/ndr_pseudonymise/ndr_encrypt/command_line.rb', line 155

def self.gc(args, options)
  required_options = i[]
  allowed_options = required_options
  unless (options.keys - allowed_options).empty? &&
         required_options.all? { |sym| options.key?(sym) } &&
         args.empty?
    warn "usage: ndr_encrypt gc\n    USAGE\n    exit 1\n  end\n\n  path = Dir.pwd\n  NdrEncrypt::Repository.new(repo_dir: path).gc(output_stream: $stdout)\nend\n"

.get(args, options) ⇒ Object

rubocop:disable Metrics/MethodLength



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/ndr_pseudonymise/ndr_encrypt/command_line.rb', line 171

def self.get(args, options) # rubocop:disable Metrics/MethodLength
  required_options = i[key_name private_key]
  allowed_options = required_options + i[passin]
  unless (options.keys - allowed_options).empty? &&
         required_options.all? { |sym| options.key?(sym) }
    warn "usage: ndr_encrypt get --key_name=<keyname> --private_key=<path> [<path>...]\n    USAGE\n    exit 1\n  end\n  if args.empty?\n    warn 'Nothing specified, nothing to get.'\n    return\n  end\n\n  path = Dir.pwd\n  repo = NdrEncrypt::Repository.new(repo_dir: path)\n  repo.get(args, key_name: options[:key_name], private_key: options[:private_key],\n                 passin: options[:passin])\nend\n"

.init(args, options) ⇒ Object

rubocop:disable Metrics/MethodLength



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/ndr_pseudonymise/ndr_encrypt/command_line.rb', line 90

def self.init(args, options) # rubocop:disable Metrics/MethodLength
  required_options = i[]
  allowed_options = required_options
  unless (options.keys - allowed_options).empty? &&
         required_options.all? { |sym| options.key?(sym) } &&
         args.size <= 1
    warn "usage: ndr_encrypt init [<path>]\n    USAGE\n    exit 1\n  end\n\n  path = args[0] || Dir.pwd\n  action = if NdrEncrypt::Repository.new(repo_dir: path).init\n             'Initialized empty'\n           else\n             'Reinitialized existing'\n           end\n  $stdout.puts \"\#{action} ndr_encrypted encrypted store in \" \\\n               \"\#{File.join(File.expand_path(path), NdrEncrypt::Repository::ENCRYPTED_DIR)}\"\nend\n"

.run!Object

rubocop:disable Metrics/AbcSize, Metrics/MethodLength



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
# File 'lib/ndr_pseudonymise/ndr_encrypt/command_line.rb', line 43

def self.run! # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
  options = {}
  parser = OptionParser.new do |opts|
    # Hide TODOs in usage message
    # rubocop:disable Style/SelectByRegexp
    usage = USAGE.split("\n").reject { |s| s =~ /TODO/ }.join("\n")
    # rubocop:enable Style/SelectByRegexp
    opts.banner = "#{usage.chomp}\n\nAdditional options:"

    opts.on('--base_url=URL', 'Remote repository URL') do |url|
      options[:base_url] = url
    end
    opts.on('--key_name=NAME', /[A-Z0-9_-]*/i, 'Key name') do |name|
      options[:key_name] = name
    end
    opts.on('--private_key=NAME', 'Private key filename') do |name|
      options[:private_key] = name
    end
    opts.on('--pub_key=NAME', 'Public key filename') do |name|
      options[:pub_key] = name
    end
    opts.on('--passin=OPTIONS', 'Pass in private key passphrase') do |name|
      options[:passin] = name
    end
    opts.on('-p', 'Print downloaded object') do |v|
      options[:pretty_print] = v
    end
  end

  begin
    parser.parse!
  rescue OptionParser::ParseError => e
    warn e
    exit 1
  end

  parser.parse('--help') if ARGV.empty?
  command = ARGV[0]
  unless COMMANDS.include?(command)
    warn "ndr_encrypt: '\#{command}' is not an ndr_encrypt command. See 'ndr_encrypt --help'.\n    UNKNOWN_CMD\n    exit 1\n  end\n  send(command.gsub('-', '_'), ARGV[1..-1], options)\nend\n"