Skip to content

Commit

Permalink
Add a SystemdService.build method
Browse files Browse the repository at this point in the history
  • Loading branch information
imobachgs committed Jul 30, 2018
1 parent c0e5489 commit 3c4a1e3
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 9 deletions.
12 changes: 10 additions & 2 deletions library/system/src/lib/yast2/system_service.rb
Expand Up @@ -70,12 +70,20 @@ class SystemService
:active_state, :sub_state, :name, :description, :static?

class << self
# Build a service instance based on the name
#
# @param name [String] Service name
# @return [SystemService,nil] System service based on the given name
def build(name)
new(Yast::SystemdService.build(name))
end

# Find a service
#
# @param name [String] Service name
# @return [SystemService,nil] System service or nil when not found
def find(name, allow_missing: false)
new(Yast::SystemdService.find(name, allow_missing: allow_missing))
def find(name)
new(Yast::SystemdService.find(name))
end

# Finds service names
Expand Down
3 changes: 1 addition & 2 deletions library/system/test/yast2/system_service_test.rb
Expand Up @@ -40,8 +40,7 @@
let(:systemd_service) { instance_double(Yast::SystemdServiceClass::Service) }

before do
allow(Yast::SystemdService).to receive(:find).with("cups", anything)
.and_return(systemd_service)
allow(Yast::SystemdService).to receive(:find).with("cups").and_return(systemd_service)
end

it "finds a systemd service" do
Expand Down
22 changes: 17 additions & 5 deletions library/systemd/src/modules/systemd_service.rb
Expand Up @@ -77,11 +77,9 @@ class SystemdServiceClass < Module
# @param service_name [String] "foo" or "foo.service"
# @param propmap [SystemdUnit::PropMap]
# @return [Service,nil] `nil` if not found
def find(service_name, propmap = {}, allow_missing: false)
service_name += UNIT_SUFFIX unless service_name.end_with?(UNIT_SUFFIX)
propmap = SERVICE_PROPMAP.merge(propmap)
service = Service.new(service_name, propmap)
return nil if service.properties.not_found? && !allow_missing
def find(service_name, propmap = {})
service = build(service_name, propmap)
return nil if service.properties.not_found?
service
end

Expand Down Expand Up @@ -138,6 +136,20 @@ def all(propmap = {})
end
end

# Instantiate a SystemdService object based on the given name
#
# Use with caution as the service might exist or not. If you need to react when
# the service does not exist, use SystemdServiceClass.find.
#
# @param service_name [String] "foo" or "foo.service"
# @param propmap [SystemdUnit::PropMap]
# @return [Service] `nil` if not found
def build(service_name, propmap = {})
service_name += UNIT_SUFFIX unless service_name.end_with?(UNIT_SUFFIX)
propmap = SERVICE_PROPMAP.merge(propmap)
Service.new(service_name, propmap)
end

class Service < SystemdUnit
include Yast::Logger

Expand Down

0 comments on commit 3c4a1e3

Please sign in to comment.