Class: Pindo::Command::Utils::Encrypt

Inherits:
Pindo::Command::Utils show all
Defined in:
lib/pindo/command/utils/encrypt.rb

Constant Summary

Constants inherited from Pindo::Command

DEFAULT_OPTIONS, DEFAULT_ROOT_OPTIONS

Instance Attribute Summary

Attributes inherited from Pindo::Command

#args_help_flag

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Pindo::Command

command_name, #initialize_options, run, use_cache?

Methods included from Funlog::Mixin

#pindo_log_instance

Methods included from Pindoconfig::Mixin

#pindo_single_config

Constructor Details

#initialize(argv) ⇒ Encrypt

Returns a new instance of Encrypt.



63
64
65
66
67
68
69
# File 'lib/pindo/command/utils/encrypt.rb', line 63

def initialize(argv)
  @input_path = argv.shift_argument
  @output_dir = argv.option('output')
  @password = argv.option('password')
  super(argv)
  @additional_args = argv.remainder!
end

Class Method Details

.optionsObject



56
57
58
59
60
61
# File 'lib/pindo/command/utils/encrypt.rb', line 56

def self.options
  [
    ['--output=PATH', '指定加密文件输出目录(默认: ./encrypted)'],
    ['--password=PASSWORD', '加密密码(不推荐使用,建议交互式输入)']
  ].concat(super)
end

Instance Method Details

#runObject



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/pindo/command/utils/encrypt.rb', line 77

def run
  @input_path = File.expand_path(@input_path)

  # 获取加密密码
  @password ||= prompt_password

  # 确定输出目录
  @output_dir ||= File.join(Dir.pwd, 'encrypted')
  @output_dir = File.expand_path(@output_dir)
  FileUtils.mkdir_p(@output_dir)

  Funlog.instance.fancyinfo_start("开始加密...")
  Funlog.instance.fancyinfo_update("输出目录: #{@output_dir}")

  # 执行加密
  if File.file?(@input_path)
    encrypt_single_file(@input_path)
  elsif File.directory?(@input_path)
    encrypt_directory(@input_path)
  end

  Funlog.instance.fancyinfo_success("加密完成!")
  Funlog.instance.fancyinfo_update("加密文件保存在: #{@output_dir}")
end

#validate!Object



71
72
73
74
75
# File 'lib/pindo/command/utils/encrypt.rb', line 71

def validate!
  super
  help! '请指定要加密的文件或目录路径' unless @input_path
  help! "路径不存在: #{@input_path}" unless File.exist?(@input_path)
end