Class: SvCtl::Cli::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/svctl/cli/command.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(global_opts, opts, args) ⇒ Command

Returns a new instance of Command



14
15
16
17
18
# File 'lib/svctl/cli/command.rb', line 14

def initialize(global_opts, opts, args)
  @gopts = global_opts
  @opts = opts
  @args = args
end

Instance Attribute Details

#argsObject (readonly)

Returns the value of attribute args



12
13
14
# File 'lib/svctl/cli/command.rb', line 12

def args
  @args
end

#goptsObject (readonly)

Returns the value of attribute gopts



12
13
14
# File 'lib/svctl/cli/command.rb', line 12

def gopts
  @gopts
end

#optsObject (readonly)

Returns the value of attribute opts



12
13
14
# File 'lib/svctl/cli/command.rb', line 12

def opts
  @opts
end

Class Method Details

.run(method) ⇒ Object



5
6
7
8
9
10
# File 'lib/svctl/cli/command.rb', line 5

def self.run(method)
  Proc.new do |global_opts, opts, args|
    cmd = new(global_opts, opts, args)
    cmd.method(method).call
  end
end

Instance Method Details

#disableObject

Raises:

  • (GLI::BadCommandLine)


49
50
51
52
# File 'lib/svctl/cli/command.rb', line 49

def disable
  raise GLI::BadCommandLine, 'missing argument <service>' unless args[0]
  SvCtl.disable(args[0], args[1] || 'current')
end

#enableObject

Raises:

  • (GLI::BadCommandLine)


44
45
46
47
# File 'lib/svctl/cli/command.rb', line 44

def enable
  raise GLI::BadCommandLine, 'missing argument <service>' unless args[0]
  SvCtl.enable(args[0], args[1] || 'current')
end

#gen_bash_completionObject



67
68
69
70
71
72
73
74
75
76
77
# File 'lib/svctl/cli/command.rb', line 67

def gen_bash_completion
  c = OsCtl::Lib::Cli::Completion::Bash.new(Cli::App.get)

  services = 'ls -1 /etc/runit/services'
  runlevels = 'ls -1 /etc/runit/runsvdir | grep -v previous'

  c.arg(cmd: :all, name: :service, expand: services)
  c.arg(cmd: :all, name: :runlevel, expand: runlevels)

  puts c.generate
end

#list_allObject



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/svctl/cli/command.rb', line 20

def list_all
  all_runlevels = SvCtl.runlevels

  SvCtl.all_services.sort.each do |s|
    sv_runlevels = s.runlevels
    rlv_line = all_runlevels.map do |rlv|
      sprintf('%-10s', sv_runlevels.include?(rlv) ? rlv : '')
    end.join('  ')

    puts sprintf('%-20s    %s', s.name, rlv_line)
  end
end

#list_runlevelsObject



54
55
56
# File 'lib/svctl/cli/command.rb', line 54

def list_runlevels
  SvCtl.runlevels.each { |v| puts v }
end

#list_servicesObject



33
34
35
36
37
38
39
40
41
42
# File 'lib/svctl/cli/command.rb', line 33

def list_services
  if opts[:all]
    list_all

  else
    SvCtl.runlevel_services(args[0] || 'current').each do |s|
      puts s.name
    end
  end
end

#runlevelObject



58
59
60
# File 'lib/svctl/cli/command.rb', line 58

def runlevel
  puts SvCtl.runlevel
end

#switchObject

Raises:

  • (GLI::BadCommandLine)


62
63
64
65
# File 'lib/svctl/cli/command.rb', line 62

def switch
  raise GLI::BadCommandLine, 'missing argument <runlevel>' unless args[0]
  SvCtl.switch(args[0])
end