Module: Browserctl::Commands::Session

Extended by:
CliOutput
Defined in:
lib/browserctl/commands/session.rb

Constant Summary collapse

USAGE =
"Usage: browserctl session <save|load|list|delete|export|import> [args]"

Class Method Summary collapse

Methods included from CliOutput

print_result

Class Method Details

.run(client, args) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/browserctl/commands/session.rb', line 12

def self.run(client, args)
  sub = args.shift or abort USAGE
  case sub
  when "save"   then run_save(client, args)
  when "load"   then run_load(client, args)
  when "list"   then run_list(client)
  when "delete" then run_delete(client, args)
  when "export" then run_export(args)
  when "import" then run_import(args)
  else abort "unknown session subcommand '#{sub}'\n#{USAGE}"
  end
end

.run_delete(client, args) ⇒ Object



39
40
41
42
# File 'lib/browserctl/commands/session.rb', line 39

def self.run_delete(client, args)
  name = args.shift or abort "usage: browserctl session delete <name>"
  print_result(client.session_delete(name))
end

.run_export(args) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
# File 'lib/browserctl/commands/session.rb', line 44

def self.run_export(args)
  name = args.shift or abort "usage: browserctl session export <name> <path>"
  dest = args.shift or abort "usage: browserctl session export <name> <path>"
  session_dir = File.join(Browserctl::BROWSERCTL_DIR, "sessions", name)
  abort "session '#{name}' not found" unless Dir.exist?(session_dir)

  dest = File.expand_path(dest)
  pid = Process.spawn("zip", "-r", dest, name, chdir: File.join(Browserctl::BROWSERCTL_DIR, "sessions"))
  Process.wait(pid)
  puts({ ok: true, path: dest }.to_json)
end

.run_import(args) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/browserctl/commands/session.rb', line 56

def self.run_import(args)
  zip_path = args.shift or abort "usage: browserctl session import <path>"
  zip_path = File.expand_path(zip_path)
  abort "zip file not found: #{zip_path}" unless File.exist?(zip_path)

  sessions_dir = File.join(Browserctl::BROWSERCTL_DIR, "sessions")
  FileUtils.mkdir_p(sessions_dir)
  pid = Process.spawn("unzip", "-o", zip_path, "-d", sessions_dir)
  Process.wait(pid)
  puts({ ok: true }.to_json)
end

.run_list(client) ⇒ Object



35
36
37
# File 'lib/browserctl/commands/session.rb', line 35

def self.run_list(client)
  print_result(client.session_list)
end

.run_load(client, args) ⇒ Object



30
31
32
33
# File 'lib/browserctl/commands/session.rb', line 30

def self.run_load(client, args)
  name = args.shift or abort "usage: browserctl session load <name>"
  print_result(client.session_load(name))
end

.run_save(client, args) ⇒ Object



25
26
27
28
# File 'lib/browserctl/commands/session.rb', line 25

def self.run_save(client, args)
  name = args.shift or abort "usage: browserctl session save <name>"
  print_result(client.session_save(name))
end