module Ptf::Commands::Group::Add

Public Class Methods

add(group, abbrev) click to toggle source
# File lib/ptf/commands/group/add.rb, line 7
def add(group, abbrev)
  if Ptf::Group.group_exist?(group)
    return "Group already exists using the name #{group}."
  elsif Ptf::Group.group_exist?(abbrev)
    return "Group already exists using the abbreviation #{abbrev}."
  end

  if abbrev.length > 4
    return "Abbreviation cannot be longer than 4 characters."
  end

  if group != group.downcase
    return "Group names must be entirely lowercase."
  elsif abbrev != abbrev.upcase
    return "Group abbreviations must be entirely uppercase."
  end

  new_group = Ptf::Group.new(group, abbrev)

  group_file = Ptf::FileSystem.group_list_file

  File.open(group_file, 'a') { |f| f.write("#{new_group.to_s}\n") }

  open_dir = Ptf::FileSystem.metadata_open_dir
  closed_dir = Ptf::FileSystem.metadata_closed_dir

  [open_dir, closed_dir].each do |d|
    Dir.mkdir(File.join(d, new_group.name), Ptf::FileSystem.file_permission)
  end

  return "Added group #{new_group.to_s} successfully!"
end