-
Notifications
You must be signed in to change notification settings - Fork 2
refactor: move link logic to a separate module #49
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
claby2
wants to merge
5
commits into
plamorg:master
Choose a base branch
from
claby2:refactor-link-actions
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
6112192
refactor: move link logic to a separate module
claby2 9922a5b
Merge remote-tracking branch 'upstream/master' into refactor-link-act…
claby2 e1dd52a
fix: disable pattern matching for host paths
claby2 60b1731
fix: catch non-existent repo files earlier
claby2 9e5463c
refactor: reflect review changes
claby2 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| // Symlink function is dependent on OS | ||
| use crate::{ | ||
| config, | ||
| directories::AMBIT_PATHS, | ||
| error::{AmbitError, AmbitResult}, | ||
| }; | ||
| use std::{ | ||
| io::{self, Write}, | ||
| process::Command, | ||
| }; | ||
|
|
||
| // Initialize config and repository directory | ||
| fn ensure_no_repo_conflicts(force: bool) -> AmbitResult<()> { | ||
| let repo_exists = AMBIT_PATHS.repo.exists(); | ||
| if repo_exists | ||
| // No need to prompt if force is true. | ||
| && !force | ||
| // Ask user if they want to overwrite. | ||
| && !prompt_confirm("A repository already exists. Overwrite?")? | ||
| { | ||
| return Err(AmbitError::Other( | ||
| "Dotfile repository already exists.\nUse '-f' flag to overwrite.".to_owned(), | ||
| )); | ||
| } else if repo_exists { | ||
| // Remove if either force is enabled or if the user confirmed to overwrite. | ||
| AMBIT_PATHS.repo.remove()?; | ||
| } | ||
| Ok(()) | ||
| } | ||
|
|
||
| // Prompt user for confirmation with message. | ||
| pub fn prompt_confirm(message: &str) -> AmbitResult<bool> { | ||
| print!("{} [Y/n] ", message); | ||
| io::stdout().flush()?; | ||
| let mut answer = String::new(); | ||
| io::stdin().read_line(&mut answer)?; | ||
| Ok(answer.trim().to_lowercase() == "y") | ||
| } | ||
|
|
||
| // Initialize an empty dotfile repository | ||
| pub fn init(force: bool) -> AmbitResult<()> { | ||
| ensure_no_repo_conflicts(force)?; | ||
| AMBIT_PATHS.repo.create()?; | ||
| // Initialize an empty git repository | ||
| git(vec!["init"])?; | ||
| Ok(()) | ||
| } | ||
|
|
||
| // Clone an existing dotfile repository with given origin | ||
| pub fn clone(force: bool, clone_arguments: Vec<&str>) -> AmbitResult<()> { | ||
| ensure_no_repo_conflicts(force)?; | ||
| // Clone will handle creating the repository directory | ||
| let repo_path = AMBIT_PATHS.repo.to_str()?; | ||
| let status = Command::new("git") | ||
| .arg("clone") | ||
| .args(clone_arguments) | ||
| .args(vec!["--", repo_path]) | ||
| .status()?; | ||
| match status.success() { | ||
| true => { | ||
| println!("Successfully cloned repository to {}", repo_path); | ||
| Ok(()) | ||
| } | ||
| false => Err(AmbitError::Other("Failed to clone repository".to_owned())), | ||
| } | ||
| } | ||
|
|
||
| // Check ambit configuration for errors | ||
| pub fn check() -> AmbitResult<()> { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would probably be useful if this would check more than just the configuration, such as whether the symlinks specified in the config actually exist. |
||
| config::get_entries(&AMBIT_PATHS.config)?; | ||
| Ok(()) | ||
| } | ||
|
|
||
| // Run git commands from the dotfile repository | ||
| pub fn git(git_arguments: Vec<&str>) -> AmbitResult<()> { | ||
| // The path to repository (git-dir) and the working tree (work-tree) is | ||
| // passed to ensure that git commands are run from the dotfile repository | ||
| let mut command = Command::new("git"); | ||
| command.args(&[ | ||
| ["--git-dir=", AMBIT_PATHS.git.to_str()?].concat(), | ||
| ["--work-tree=", AMBIT_PATHS.repo.to_str()?].concat(), | ||
| ]); | ||
| command.args(git_arguments); | ||
| // Conditional compilation so that this still compiles on Windows. | ||
| #[cfg(unix)] | ||
| fn exec_git_command(mut command: Command) -> AmbitResult<()> { | ||
| use std::os::unix::process::CommandExt; | ||
| // Try to replace this process with the `git` process. | ||
| // This is to allow stuff like terminal colors. | ||
| // We just want `ambit git` to act like `cd ~/.config/ambit/repo; git`. | ||
| // If the `.exec()` method returns, it failed to execute, so it's automatically an error. | ||
| Err(AmbitError::Io(command.exec())) | ||
| } | ||
| #[cfg(not(unix))] | ||
| fn exec_git_command(mut command: Command) -> AmbitResult<()> { | ||
| // Not easy to do this on other systems, just use defaults | ||
| let output = command.output()?; | ||
| io::stdout().write_all(&output.stdout)?; | ||
| io::stdout().write_all(&output.stderr)?; | ||
| Ok(()) | ||
| } | ||
| exec_git_command(command) | ||
| } | ||
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,5 @@ | ||
| pub mod cmd; | ||
| pub mod config; | ||
| pub mod directories; | ||
| pub mod error; | ||
| pub mod linker; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.