#!/usr/bin/env ruby # frozen_string_literal: true require 'mtree' require 'optparse' options = { action: :check, group_mapping: {}, user_mapping: {}, } OptionParser.new do |opts| opts.banner = 'usage: mtree options' opts.on('-e', 'Noop. For compatibility only') do end opts.on('-f', '--file=FILE', 'Read the specification from FILE instead of standard input') do |specification| options[:specification] = specification end opts.on('-p', '--path=PATH', 'Use the file hierarchy rooted in PATH, instead of the current directory') do |path| options[:root] = path end opts.on('-j', 'Indent the output 4 spaces each time a directory level is descended') do options[:indent] = true end opts.on('-u', '--update') do options[:action] = :update end # Extensions opts.on('--check', 'Checks that the files hierarchy match the specification') do options[:action] = :check end opts.on('--print', 'Print the transformed specification') do options[:action] = :print end opts.on('--leaves', 'Only consider leaves of the specification') do options[:leaves] = true end opts.on('--map-gname=MAPPING', 'Apply from:to MAPPING to the gname field') do |mapping| mapping.split(',').each do |tuple| from, to = tuple.split(':', 2) options[:group_mapping][from] = to end end opts.on('--map-uname=MAPPING', 'Apply from:to MAPPING to the uname field') do |mapping| mapping.split(',').each do |tuple| from, to = tuple.split(':', 2) options[:user_mapping][from] = to end end opts.on('--symlink-to=DESTINATION', '') do |destination| options[:symlink_to] = destination end end.parse! parser = Mtree::Parser.new parser.parse_file(options[:specification]) specifications = parser.specifications specifications.leaves! if options[:leaves] options[:group_mapping].each do |from, to| specifications.each do |specification| specification.gname = to if specification.gname == from end end options[:user_mapping].each do |from, to| specifications.each do |specification| specification.uname = to if specification.uname == from end end specifications.symlink_to!(options[:symlink_to]) if options[:symlink_to] case options[:action] when :update specifications.enforce(options[:root] || '.') when :check exit specifications.match?(options[:root] || '.') ? 0 : 2 when :print puts specifications.to_s(indent: options[:indent]) end