Module: Obfus

Defined in:
lib/obfus.rb,
lib/obfus/main.rb,
lib/obfus/version.rb

Constant Summary collapse

CONFIG_LOCATIONS =
[
  File.join(ENV['HOME'], '.config', 'obfus', '{.,}config*'),
  File.join(ENV['HOME'], '.config', 'obfus', '.obfus{config*,rc}'),
  File.join(ENV['HOME'], '.obfus{config*,rc}')
].freeze
OPTIONS =
{
  mode: :compress,
  preset: nil,
  level: 9,
  keep: true,
  recipients: [],
  verbosity: :normal
}.freeze
VERSION =
'0.1.5'.freeze

Class Method Summary collapse

Class Method Details

.apply_config(options) ⇒ Object


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
# File 'lib/obfus/main.rb', line 54

def apply_config(options)
  file = find_config
  opts = OpenStruct.new OPTIONS
  config = nil
  unless file.nil?
    config = parse_config(file)
    if options.preset.nil?
      if config['default'].nil?
        # use native defaults
      else
        # use config file
        opts = OpenStruct.new config['default']
        opts.preset = 'default'
      end
    else
      # use preset
      opts = OpenStruct.new config[options.preset]
    end
  end

  # override
  options.each_pair do |k, v|
    opts[k] = v
  end

  opts['keep'] = true if opts['keep'].nil?

  opts['config'] = file

  opts
end

.compress(files, options) ⇒ Object


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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/obfus/main.rb', line 91

def compress(files, options)
  archive_name = 'Archive'
  if files.count < 1
    puts 'error: no files specified'
    puts 'try `obfus --help`'
    exit 1
  elsif files.count == 1
    # call it the name of the file
    archive_name = File.basename files[0], '.*'
  end

  unless options.output.nil?
    archive_name = options.output.strip
    archive_name.gsub!(/^.*(\\|\/)/, '')
  end

  unless options.force
    if ensure_file(archive_name)
      # file already exists
      puts "error: file #{archive_name} already exists"
      puts 'to overwrite the file use `--force`'
      exit 1
    end
  end

  files.each do |file|
    next if ensure_file(file)
    # TODO: file <file> does not exist!
    puts "error: #{file} does not exist"
    exit 1
  end

  if options.recipients.count < 1
    # TODO: throw error! no recipients specified
    puts 'error: no recipients specified'
    puts 'try `obfus --help`'
    exit 1
  end

  if options.verbosity == :verbose
    unless options.verbosity == :quiet
      puts 'using config file: ' + options.config
      puts 'using preset: ' + options.preset
      puts 'keeping original files: ' + options.keep.to_s
      puts 'compression quality: ' + options.level.to_s
      puts 'compressing:'
      files.each { |f| puts '- ' + f }
      puts
      puts 'recipients:'
      options.recipients.each { |r| puts '- ' + r }
      puts
    end
  end

  File.open archive_name, 'w' do |f|
    # add recipients `-r <recipient>`
    recipients = []

    options.recipients.each do |r|
      recipients << '-r'
      recipients << r
    end

    Open3.pipeline_r(
      ['tar', 'cf', '-', *files],
      ['brotli', '-cq', options.level.to_s],
      ['gpg', '-eq', *recipients]
    ) do |o, _ts|
      # ts.each { |t| puts t.pid, t.status }
      f.write o.read
    end
  end
end

.decompress(file) ⇒ Object


165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'lib/obfus/main.rb', line 165

def decompress(file)
  # TODO: iterate over files
  unless ensure_file(file)
    puts "error: file #{file} does not exist"
    exit 1
  end

  Open3.pipeline_r(
    ['gpg', '-dq', file],
    ['brotli', '-dc'],
    ['tar', '-x']
  ) do |o, ts|
    # ts.each { |t| puts t.pid, t.status }
  end
end

.ensure_file(name) ⇒ Object


86
87
88
89
# File 'lib/obfus/main.rb', line 86

def ensure_file(name)
  path = File.expand_path name
  File.exist? path
end

.exec(args) ⇒ Object


181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# File 'lib/obfus/main.rb', line 181

def exec(args)
  options = OpenStruct.new

  opt_parser = OptionParser.new do |opts|
    opts.banner = "\nUsage: obfus [options] <file...>"
    opts.separator ''
    opts.separator 'Operation Modes:'
    opts.on('-z', '--compress', 'Compress operation mode (default)') do
      options.mode = :compress
    end
    opts.on('-d', '--decompress', 'Decompress operation mode') do
      options.mode = :decompress
    end

    opts.separator ''
    opts.separator 'Options:'
    opts.on('-o', '--output NAME', 'Specify the output file name') do |name|
      options.output = name
    end
    opts.on('-f', '--force', 'Overwrites output file if conflicts with an existing one') do |v|
      options.force = v
    end
    opts.on('-p', '--preset NAME', 'Use a configuration preset') do |name|
      options.preset = name
    end
    opts.on('-l', '--level [0..9]', Integer, 'Specify compression level (defaults to 9)') do |v|
      options.level = v
    end
    opts.on('-k', '--[no-]keep', 'Keep the original files') do |keep|
      options.keep = keep
    end
    opts.on('-r', '--recipients x,y,z', Array, 'Add recipients list') do |list|
      options.recipients = [] if options.recipient.nil?
      options.recipients += list
    end

    opts.on('-v', '--verbose', 'Run verbosely') do
      options.verbosity = :verbose
    end
    opts.on('-q', '--quiet', 'Suppress any output') do
      options.verbosity = :quiet
    end

    opts.separator ''
    opts.separator 'Other options:'
    opts.on_tail('--version', 'Show the version number') do
      puts Obfus::VERSION
      exit
    end

    opts.on_tail('-h', '--help', 'Show this message') do
      puts opts
      exit
    end
  end

  opt_parser.parse!(args)

  options = apply_config(options)

  if options.mode == :decompress
    # decompress archive
    decompress(ARGV[0])
  else
    # compress
    compress(ARGV, options)
  end

  # puts options
  # puts ARGV
end

.find_configObject


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/obfus/main.rb', line 24

def find_config
  list = []
  CONFIG_LOCATIONS.each do |pattern|
    list += Dir.glob pattern
  end

  if list.count < 1
    # no config file detected
    nil
  elsif list.count > 1
    # TODO: print warning (multiple config files: <list...>, reading from <path>)
    puts 'error: encountered multiple configuration files:'
    list.each do |l|
      puts File.expand_path l
    end
    puts "reading from: #{File.expand_path list[0]}"
    nil
  else
    list[0]
  end
end

.parse_config(file) ⇒ Object


46
47
48
49
50
51
52
# File 'lib/obfus/main.rb', line 46

def parse_config(file)
  parsed = nil
  File.open(file, 'r') do |f|
    parsed = YAML.safe_load f.read
  end
  parsed
end