Skip to content

Commit

Permalink
Merge pull request #11 from config-files-api/extend_grub_cfg
Browse files Browse the repository at this point in the history
extend grub.cfg with full path for set-default
  • Loading branch information
jreidinger committed Jul 11, 2016
2 parents 4b114fa + ce8a265 commit 3d493b4
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 3 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
-------------------------------------------------------------------
Mon Jul 11 13:40:06 UTC 2016 - jreidinger@suse.com

- extend grub.cfg model to construct also full path usable for
grub2-set-default (bnc#986005)
- 0.5.0

-------------------------------------------------------------------
Thu May 12 08:20:09 UTC 2016 - jreidinger@suse.com

Expand Down
2 changes: 1 addition & 1 deletion cfa_grub2.gemspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Gem::Specification.new do |s|
s.name = "cfa_grub2"
s.version = "0.4.1"
s.version = "0.5.0"
s.platform = Gem::Platform::RUBY
s.authors = ["Josef Reidinger"]
s.email = ["jreidinger@suse.cz"]
Expand Down
27 changes: 25 additions & 2 deletions lib/cfa/grub2/grub_cfg.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,14 @@ class GrubCfg < BaseModel
# @private only internal parser
class Parser
def self.parse(string)
menu_lines = string.lines.grep(/menuentry\s*'/)
menu_lines.map { |line| line[/\s*menuentry\s*'([^']+)'.*/, 1] }
submenu = ""
string.lines.each_with_object([]) do |line, result|
case line
when /menuentry\s*'/ then result << parse_entry(line, submenu)
when /^}\s*\n/ then submenu = ""
when /submenu\s/ then submenu = line[/\s*submenu\s+'([^']+)'.*/, 1]
end
end
end

def self.serialize(_string)
Expand All @@ -24,14 +30,31 @@ def self.serialize(_string)
def self.empty
[]
end

def self.parse_entry(line, submenu)
entry = line[/\s*menuentry\s+'([^']+)'.*/, 1]
{
title: entry,
path: submenu.empty? ? entry : "#{submenu}>#{entry}"
}
end
private_class_method :parse_entry
end

def initialize(file_handler: nil)
super(Parser, PATH, file_handler: file_handler)
end

# @return [Array<String>] sections from grub.cfg in order as they appear
# @deprecated use instead boot_entries
def sections
data.map { |p| p[:title] }
end

# @return [Array<Hash>] return boot entries containing `title:` as shown
# on screen and `path:` whole path usable for grub2-set-default including
# also submenu part of path
def boot_entries
data
end
end
Expand Down
34 changes: 34 additions & 0 deletions spec/grub_cfg_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,38 @@
)
end
end

describe "#boot_entries" do
it "gets boot entries list with title: and path: elements" do
expect(config.boot_entries).to eq(
[
{
title: "openSUSE Leap 42.1",
path: "openSUSE Leap 42.1"
},
{
title: "openSUSE Leap 42.1, with Linux 4.1.12-1-default",
path: "Advanced options for openSUSE Leap 42.1>" \
"openSUSE Leap 42.1, with Linux 4.1.12-1-default"
},
{
title: "openSUSE Leap 42.1, with Linux 4.1.12-1-default " \
"(recovery mode)",
path: "Advanced options for openSUSE Leap 42.1>" \
"openSUSE Leap 42.1, with Linux 4.1.12-1-default (recovery mode)"
},
{
title: "halt",
path: "halt"
}
]
)
end
end

describe "#save" do
it "raises NotImplementedError" do
expect { config.save }.to raise_error(NotImplementedError)
end
end
end

0 comments on commit 3d493b4

Please sign in to comment.