module Ptf::Commands::Group::Remove

Public Class Methods

remove(group) click to toggle source
# File lib/ptf/commands/group/remove.rb, line 7
def remove(group)
  if !Ptf::Group.group_exist?(group)
    return "Group #{group} does not exist. Add it with ptf group add GROUP ABBREVIATION."
  end

  group = Ptf::Group.get_group(group)

  if group.is_default?
    return "#{group.to_s} is the default group. It cannot be removed."
  end

  # Prompt for user confirmation
  puts "Are you sure you want to remove #{group.to_s}? This will delete ALL tasks associated with this group."
  confirm = STDIN.gets.chomp

  if confirm.downcase != "y" && confirm.downcase != "yes"
    return "Group removal aborted."
  end

  # Remove from group_list
  tmp_file = "#{Ptf::FileSystem.group_list_file}.tmp"
  File.open(Ptf::FileSystem.group_list_file, "r") do |original|
    File.open(tmp_file, "w") do |tmp|
      original.each_line do |line|
        tmp.write(line) unless line.gsub(/\s+/, "") == group.to_s
      end
    end
  end

  File.rename(tmp_file, Ptf::FileSystem.group_list_file)

  # Remove from info/open/[group]
  FileUtils.rm_r File.join(Ptf::FileSystem.metadata_open_dir, group.name)

  # Remove from info/closed/[group]
  FileUtils.rm_r File.join(Ptf::FileSystem.metadata_closed_dir, group.name)

  return "Group removed."
end