Class: Fusebox::Cli

Inherits:
Thor
  • Object
show all
Includes:
Thor::Actions
Defined in:
lib/fusebox/cli.rb

Overview

TODO:
  • Allow specification of group admin username-part instead of 'postmaster' (via yaml? fuseboxrc?)

  • Exit codes

  • Flags for simple, non-table output for report methods (to allow piping)

These methods are executed via the `bin/fusebox` executable

Instance Method Summary collapse

Instance Method Details

#add(username, password, first_name, last_name) ⇒ Object


15
16
17
18
19
# File 'lib/fusebox/cli.rb', line 15

def add (username, password, first_name, last_name)
  group_parent = username.gsub /.*(@.*)/, 'postmaster\1'
  response = Fusebox::Request.new.order(:account_type => 'group_subaccount', :group_parent => group_parent, :user => username, :password => password, :first_name => first_name, :last_name => last_name)
  say response.detail
end

#alias_add(username, *aliases) ⇒ Object


22
23
24
25
# File 'lib/fusebox/cli.rb', line 22

def alias_add(username, *aliases)
  response = Fusebox::Request.new.modify(:user => username, :alias => aliases)
  say response.detail
end

#alias_ls(username) ⇒ Object


28
29
30
31
32
33
34
35
# File 'lib/fusebox/cli.rb', line 28

def alias_ls (username)
  response = Fusebox::Request.new.reportmail(:user => username, :report_type => 'alias')
  if response.success?
    response.records.sort { |x,y| x[:email_name] <=> y[:email_name]}.each { |a| say a[:email_name] }
  else
    warn response.detail
  end
end

#alias_rm(username, alias_address) ⇒ Object


38
39
40
41
# File 'lib/fusebox/cli.rb', line 38

def alias_rm (username, alias_address)
  response = Fusebox::Request.new.removealias(:user => username, :alias => alias_address)
  say response.detail
end

#domain_add(username, domain) ⇒ Object


44
45
46
47
48
# File 'lib/fusebox/cli.rb', line 44

def domain_add (username, domain)
  username = "[email protected]#{username}" unless username =~ /@/
  response = Fusebox::Request.new.adddomain(:user => username, :domain => domain)
  say response.detail
end

#domain_rm(domain) ⇒ Object


52
53
54
55
56
57
# File 'lib/fusebox/cli.rb', line 52

def domain_rm (domain)
  if options[:force] || yes?("Are you sure you want to remove domain: #{domain}? [y/N]")
    response = Fusebox::Request.new.removedomain(:domain => domain, :confirm => true)
    say response.detail
  end
end

#enable(username) ⇒ Object


60
61
62
63
# File 'lib/fusebox/cli.rb', line 60

def enable (username)
  response = Fusebox::Request.new.enable(:user => username)
  say response.detail
end

#get(username) ⇒ Object


67
68
69
70
71
72
73
74
75
# File 'lib/fusebox/cli.rb', line 67

def get (username)
  username = "[email protected]#{username}" unless username =~ /@/
  response = Fusebox::Request.new.reportmail(:user => username)
  if response.success?
    say response.records.sort { |x,y| x[:email_name] <=> y[:email_name]}.to_ascii_table [:username, :internal_account_id, :email_type, :email_name, :email_destination], %w(Username ID Type Name Destination)
  else
    warn response.detail
  end
end

#group_add(username, password = nil) ⇒ Object


78
79
80
81
82
83
84
# File 'lib/fusebox/cli.rb', line 78

def group_add (username, password = nil)
  username = "[email protected]#{username}" unless username =~ /@/
  last, first = username.split /@/
  password ||= ActiveSupport::SecureRandom.hex
  response = Fusebox::Request.new.order(:account_type => 'standard', :user => username, :password => password, :first_name => first, :last_name => last)
  say response.detail
end

#ls(username = 'all') ⇒ Object


89
90
91
92
93
94
95
96
97
98
# File 'lib/fusebox/cli.rb', line 89

def ls (username = 'all')
  username = "[email protected]#{username}" unless username =~ /@/ || username == 'all'
  recursive = options[:recursive] || (username == 'all' ? false : true)
  response = Fusebox::Request.new.report(:user => username, :group_subaccount => recursive, :report_type => 'extended')
  if response.success?
    say response.records.sort { |x,y| x[:username] <=> y[:username]}.to_ascii_table([:username, :creation_date, :internal_account_id, :status, :disk_usage], %w(Username Created ID Status Disk))
  else
    warn response.detail
  end
end

#passwd(username, password = nil) ⇒ Object


101
102
103
104
105
# File 'lib/fusebox/cli.rb', line 101

def passwd (username, password = nil)
  password = ask "New Password:" unless password
  response = Fusebox::Request.new.modify(:user => username, :password => password)
  say response.detail
end

#rename(old_username, new_username) ⇒ Object


108
109
110
111
# File 'lib/fusebox/cli.rb', line 108

def rename (old_username, new_username)
  response = Fusebox::Request.new.changeusername(:user => old_username, :newuser => new_username)
  say response.detail
end

#rm(username) ⇒ Object


116
117
118
119
120
121
122
123
# File 'lib/fusebox/cli.rb', line 116

def rm (username)
  if options[:force] || yes?("Are you sure you want to remove user: #{username}? [y/N]")
    response = Fusebox::Request.new.terminate(:user => username, :purge => options[:purge])
    say response.detail
  else
    say "No action taken."
  end
end

#suspend(username) ⇒ Object


126
127
128
129
# File 'lib/fusebox/cli.rb', line 126

def suspend (username)
  response = Fusebox::Request.new.suspend(:user => username)
  say response.detail
end