forked from wincent/wincent
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Very simple "line" operation here, a subset of Ansible's "lineinfile" functionality.
- Loading branch information
Showing
11 changed files
with
119 additions
and
17 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"description": "Sets the use shell to zsh" | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import {line, task} from 'fig'; | ||
|
||
task('add /usr/local/bin/zsh to /etc/shells', async () => { | ||
await line({ | ||
group: 'wheel', | ||
line: '/usr/local/bin/zsh', | ||
owner: 'root', | ||
path: '/etc/shells', | ||
sudo: true, | ||
}); | ||
}); |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import ErrorWithMetadata from '../../ErrorWithMetadata.js'; | ||
import Scanner from '../../Scanner.js'; | ||
import {log} from '../../console.js'; | ||
import {promises as fs} from '../../fs.js'; | ||
import stat from '../../fs/stat.js'; | ||
import path from '../../path.js'; | ||
import file from './file.js'; | ||
|
||
/** | ||
* @see https://docs.ansible.com/ansible/latest/modules/lineinfile_module.html | ||
*/ | ||
export default async function line({ | ||
path: dest, | ||
group, | ||
line, | ||
mode, | ||
owner, | ||
state = 'present', | ||
sudo, | ||
}: { | ||
path: string; | ||
group?: string; | ||
line: string; | ||
mode?: Mode; | ||
owner?: string; | ||
state?: 'absent' | 'present'; | ||
sudo?: boolean; | ||
}): Promise<void> { | ||
log.debug(`Line \`${line}\` in \`${path}\``); | ||
|
||
const normalized = `${line.trimEnd()}\n`; | ||
|
||
const target = path(dest).resolve; | ||
|
||
const stats = await stat(target); | ||
|
||
let contents = state === 'present' ? normalized : ''; | ||
|
||
if (stats instanceof Error) { | ||
throw stats; | ||
} else if (stats !== null) { | ||
if (stats.type !== 'file') { | ||
// TODO: make this work with symlinks (problem with file) | ||
throw new ErrorWithMetadata( | ||
`Cannot replace line in ${dest} because its type is ${stats.type}` | ||
); | ||
} | ||
|
||
// TODO: deal with potential permission issue here (eg. file only readable by root) | ||
const scanner = new Scanner(await fs.readFile(dest, 'utf8')); | ||
|
||
contents = ''; | ||
|
||
let found = false; | ||
|
||
while (!scanner.atEnd()) { | ||
const current = scanner.scan(/[^\r\n]*/); | ||
|
||
if (current === line) { | ||
found = true; | ||
|
||
if (state === 'present') { | ||
contents += current; | ||
} else { | ||
// Slurp unwanted line, plus the following newline. | ||
scanner.scan(/\r?\n/); | ||
continue; | ||
} | ||
} else { | ||
contents += current || ''; | ||
} | ||
|
||
contents += scanner.scan(/[\r\n]+/) || ''; | ||
} | ||
|
||
if (!found && state === 'present') { | ||
contents = contents.replace(/(\r?\n|)$/, `\n${normalized}`); | ||
} | ||
} | ||
|
||
await file({ | ||
contents, | ||
group, | ||
mode, | ||
owner, | ||
path: target, | ||
state: 'file', | ||
sudo, | ||
}); | ||
} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ | |
"iterm", | ||
"karabiner", | ||
"launchd", | ||
"shell", | ||
"terminfo", | ||
"vim", | ||
"cron", | ||
|
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.