Module: Bebox::RoleCommands

Defined in:
lib/bebox/commands/role_commands.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(base) ⇒ Object



5
6
7
# File 'lib/bebox/commands/role_commands.rb', line 5

def self.extended(base)
  base.load_commands
end

Instance Method Details

#generate_role_command(role_command, command, send_command, description) ⇒ Object

For add_profile remove_profile and remove_role commands



53
54
55
56
57
58
59
60
# File 'lib/bebox/commands/role_commands.rb', line 53

def generate_role_command(role_command, command, send_command, description)
  role_command.desc description
  role_command.command command do |generated_command|
    generated_command.action do |global_options,options,args|
      Bebox::RoleWizard.new.send(send_command, project_root)
    end
  end
end

#load_commandsObject



9
10
11
12
13
14
15
16
17
18
# File 'lib/bebox/commands/role_commands.rb', line 9

def load_commands
  desc 'Manage roles for the node provisioning phase.'
  command :role do |role_command|
    role_new_command(role_command)
    generate_role_command(role_command, :remove, :remove_role, 'Remove a role from the project')
    role_list_command(role_command)
    # These commands are available if there are at least one role and one profile
    load_role_profile_commands(role_command) if (Bebox::Role.roles_count(project_root) > 0 && Bebox::Profile.profiles_count(project_root) > 0)
  end
end

#load_role_profile_commands(role_command) ⇒ Object



46
47
48
49
50
# File 'lib/bebox/commands/role_commands.rb', line 46

def load_role_profile_commands(role_command)
  generate_role_command(role_command, :add_profile, :add_profile, 'Add a profile to a role')
  generate_role_command(role_command, :remove_profile, :remove_profile, 'Remove a profile from a role')
  role_list_profiles_command(role_command)
end


76
77
78
79
80
81
# File 'lib/bebox/commands/role_commands.rb', line 76

def print_profiles(role, profiles)
  title "Current profiles in '#{role}' role:"
  profiles.map{|profile| msg(profile)}
  warn("There are not profiles in role '#{role}'. You can add a new one with: 'bebox role add_profile' command.") if profiles.empty?
  linebreak
end

#role_list_command(role_command) ⇒ Object

Role list command



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/bebox/commands/role_commands.rb', line 21

def role_list_command(role_command)
  role_command.desc 'List the roles in the project'
  role_command.command :list do |role_list_command|
    role_list_command.action do |global_options,options,args|
      roles = Bebox::Role.list(project_root)
      title 'Current roles:'
      roles.map{|role| msg(role)}
      warn('There are not roles yet. You can create a new one with: \'bebox role new\' command.') if roles.empty?
      linebreak
    end
  end
end

#role_list_profiles_command(role_command) ⇒ Object

Role list profiles command



63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/bebox/commands/role_commands.rb', line 63

def role_list_profiles_command(role_command)
  role_command.desc 'List the profiles in a role'
  role_command.arg_name "[role_name]"
  role_command.command :list_profiles do |list_profiles_command|
    list_profiles_command.action do |global_options,options,args|
      help_now!(error('You did not supply a role name.')) if args.count == 0
      role = args.first
      exit_now!(error("The '#{role}' role does not exist.")) unless Bebox::RoleWizard.new.role_exists?(project_root, role)
      print_profiles(role, Bebox::Role.list_profiles(project_root, role))
    end
  end
end

#role_new_command(role_command) ⇒ Object

Role new command



35
36
37
38
39
40
41
42
43
44
# File 'lib/bebox/commands/role_commands.rb', line 35

def role_new_command(role_command)
  role_command.desc 'Add a role to the project'
  role_command.arg_name "[name]"
  role_command.command :new do |role_new_command|
    role_new_command.action do |global_options,options,args|
      help_now!(error('You did not supply a name')) if args.count == 0
      Bebox::RoleWizard.new.create_new_role(project_root, args.first)
    end
  end
end