Skip to content

Instantly share code, notes, and snippets.

@andersevenrud
Created September 12, 2020 20:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andersevenrud/c1e7a1c63354f6a7deec037ac8201e21 to your computer and use it in GitHub Desktop.
Save andersevenrud/c1e7a1c63354f6a7deec037ac8201e21 to your computer and use it in GitHub Desktop.
Parse fstab
/**
* Parse fstab from either string (from file) or an array of lines
* @author Anders Evenrud <andersevenrud@gmail.com>
* @license MIT
*/
const parse = input => (input instanceof Array ? input : input.split('\n'))
.map(line => line.replace(/\s+/g, ' ').replace(/(\s+)?,(\s+)?/g, ',').trim())
.filter(line => line.length > 0 && line.substring(0) !== '#')
.map(line => line.split(/\s/g))
.map(([dev, dir, type, options, dump, fsck]) => ({
dev,
dir,
type,
options: options.split(','),
dump: parseInt(dump, 10),
fsck: parseInt(fsck, 10)
}))
module.exports = {
parse
}
const { parse } = require('./index')
describe('Parse fstab', () => {
test('should parse multiple lines without failure', () => {
const result = parse([
'/dev/sda / ext4 noatime 0 1',
'/dev/sdb /home ext2 noatime,ro 1 1',
'/dev/sdc /tmp ntfs noatime, auto , rw 0 0 '
])
expect(result).toEqual([
{
dev: '/dev/sda',
dir: '/',
type: 'ext4',
dump: 0,
fsck: 1,
options: ['noatime']
},
{
dev: '/dev/sdb',
dir: '/home',
type: 'ext2',
dump: 1,
fsck: 1,
options: ['noatime', 'ro']
},
{
dev: '/dev/sdc',
dir: '/tmp',
type: 'ntfs',
dump: 0,
fsck: 0,
options: ['noatime', 'auto', 'rw']
},
])
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment