Class: Simp::Cli::Commands::Passgen

Inherits:
Simp::Cli
  • Object
show all
Defined in:
lib/simp/cli/commands/passgen.rb

Constant Summary

Constants inherited from Simp::Cli

VERSION

Class Method Summary collapse

Methods inherited from Simp::Cli

help, menu

Class Method Details

.run(args = Array.new) ⇒ 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
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
136
# File 'lib/simp/cli/commands/passgen.rb', line 54

def self.run(args = Array.new)
  super

  raise "The SIMP Passgen Tool requires at least one argument to work" if args.empty?
  raise "Target directory '#{@target_dir}' does not exist" unless File.directory?(@target_dir)

  begin
    Dir.chdir(@target_dir) do
        #File.ftype("#{@target_dir}/#{name}").eql?("file")
      @user_names = Dir.glob("*").select do |x|
        File.file?(x) && (x !~ /\..+$/)
      end
    end
  rescue => err
    raise "Error occured while accessing '#{@target_dir}':\n  Causing Error: #{err}"
  end

  if @show_list
    puts "Usernames:\n\t#{@user_names.join("\n\t")}"
    puts
  end

  @show_users.each do |user|
    if @user_names.include?(user)
      Dir.chdir(@target_dir) do
        puts "Username: #{user}"
        current_password = File.open("#{@target_dir}/#{user}", 'r').gets
        puts "  Current:  #{current_password}"
        last_password = nil
        last_password_file = "#{@target_dir}/#{user}.last"
        if File.exists?(last_password_file)
          last_password = File.open(last_password_file, 'r').gets
        end
        puts "  Previous: #{last_password}" if last_password
      end
    else
      raise "Invalid username '#{user}' selected.\n\n Valid: #{@user_names.join(', ')}"
    end
    puts
  end

  @set_users.each do |user|
    password_filename = "#{@target_dir}/#{user}"

    puts "Username: #{user}"
    password = Utils.get_password
    if File.exists?(password_filename)
      if Utils.yes_or_no("Would you like to rotate the old password?", false)
        begin
          FileUtils.mv(password_filename, password_filename + '.last')
        rescue => err
          raise "Error occurred while moving '#{password_filename}' to '#{password_filename + '.last'}'\n  Causing Error: #{err}"
        end
      end
    end
    begin
      File.open(password_filename, 'w') { |file| file.puts password }
    rescue => err
      raise "Error occurred while writing '#{password_filename}'\n  Causing Error: #{err}"
    end
    puts
  end

  @remove_users.each do |user|
    password_filename = "#{@target_dir}/#{user}"

    if File.exists?(password_filename)
      if Utils.yes_or_no("Are you sure you want to remove all entries for #{user}?", false)
        show_password(user)

        last_password_filename = password_filename + '.last'
        if File.exists?(last_password_filename)
          File.delete(last_password_filename)
          puts "#{last_password_filename} deleted"
        end

        File.delete(password_filename)
        puts "#{password_filename} deleted"
      end
    end
    puts
  end
end