Created
September 12, 2020 20:41
-
-
Save andersevenrud/c1e7a1c63354f6a7deec037ac8201e21 to your computer and use it in GitHub Desktop.
Parse fstab
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Parse fstab from either string (from file) or an array of lines | |
* @author Anders Evenrud <[email protected]> | |
* @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 | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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