Class: IpTracker::CLI

Inherits:
Thor
  • Object
show all
Defined in:
lib/ip_tracker/cli.rb,
lib/ip_tracker/cli/methods/sync.rb,
lib/ip_tracker/cli/methods/update.rb,
lib/ip_tracker/cli/methods/register.rb

Instance Method Summary collapse

Instance Method Details

#registerObject



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/ip_tracker/cli/methods/register.rb', line 5

def register
  unless config.host_token
    hostname = options[:hostname] || get_option(:hostname)

    say "Attempting to register this computer.", :yellow

    token = client.register(hostname)
    say "Registration completed.", :green
    config.update(:host_token, token)
  else
    say "This computer has already been registered."
  end
rescue IpTracker::Client::TargetError
  say "Registration failed."
rescue IpTracker::Client::HostTakenError
  say "You must enter a unique name."
end

#syncObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/ip_tracker/cli/methods/sync.rb', line 8

def sync
  command = options[:stop] ? :stop : :start
  daemonize = options[:daemon] && Process.respond_to?(:fork)

  case command
  when :start
    if config.pid
      say "IpMe is already running."
    else
      if daemonize
        pid = Process.fork do
          SyncDaemon.new.run
          Process.daemon
        end

        # TODO verify if detach and daemon work together
        config.update(:pid, pid)
        Process.detach(pid)
      else
        SyncDaemon.new.run
      end
    end
  when :stop
    if config.pid
      say "Killing #{config.pid}"

      begin
        Process.kill("TERM", config.pid)
      rescue Errno::ESRCH
        say "Process is already dead"
      end

      config.update(:pid, nil)
    else
      say "No PID saved"
    end
  end

end

#updateObject



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/ip_tracker/cli/methods/update.rb', line 5

def update
  ip = options[:ip]
  host = config.host_token
  if host.nil?
    say "Please first register this computer."
  else
    say "Attempting to update ip.", :yellow

    client.update(host, :ip, ip)

    say "Update completed", :green
  end
rescue IpTracker::Client::TargetError
  say "Update failed."
end