Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement basic pinning support
Browse files Browse the repository at this point in the history
Add a new subcommand:
```
flake-edit pin [id] [rev]
```

That will pin the specified input to either it's current rev,
if the rev is omitted, or the specified rev.
a-kenji committed Jul 22, 2024
1 parent d5add55 commit 4c4a4fc
Showing 11 changed files with 519 additions and 18 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -26,6 +26,8 @@ Commands:
List flake inputs
update
Update inputs to their latest specified release
pin
Pin inputs to their current or a specified rev
help
Print this message or the help of the given subcommand(s)
@@ -127,6 +129,27 @@ List the outputs, that are specified inside the inputs attribute.
List the outputs, that are specified inside the inputs attribute, in json format.
![flake-edit list example](https://vhs.charm.sh/vhs-35E6eiL63lFTSC70rQyE1Y.gif)

### `$ flake-edit pin`
`$ flake-edit help pin`

```
Pin inputs to their current or a specified rev
Usage: flake-edit pin <ID> [REV]
Arguments:
<ID>
The id of an input attribute
[REV]
Optionally specify a rev for the inputs attribute
Options:
-h, --help
Print help
```
![flake-edit pin](https://vhs.charm.sh/vhs-629lX7LqP4MS1aHffb4Ufh.gif)
Pin a specific input to it's current revision (rev).


## License
MIT
12 changes: 8 additions & 4 deletions assets/completions/fish/completions.fish
Original file line number Diff line number Diff line change
@@ -2,6 +2,10 @@ function __fish_complete_inputs
flake-edit list --format simple 2>/dev/null
end

function __fish_complete_inputs_toplevel
flake-edit list --format toplevel 2>/dev/null
end

function __fish_complete_add
flake-edit completion add 2>/dev/null
end
@@ -10,10 +14,10 @@ complete -c flake-edit -n "__fish_seen_subcommand_from rm" -f -a "(__fish_comple
complete -c flake-edit -n "__fish_seen_subcommand_from remove" -f -a "(__fish_complete_inputs)" -d Input
complete -c flake-edit -n "__fish_seen_subcommand_from change" -f -a "(__fish_complete_inputs)" -d Input
complete -c flake-edit -n "__fish_seen_subcommand_from c" -f -a "(__fish_complete_inputs)" -d Input
complete -c flake-edit -n "__fish_seen_subcommand_from pin" -f -a "(__fish_complete_inputs)" -d Input
complete -c flake-edit -n "__fish_seen_subcommand_from p" -f -a "(__fish_complete_inputs)" -d Input
complete -c flake-edit -n "__fish_seen_subcommand_from pin" -f -a "(__fish_complete_inputs_toplevel)" -d Input
complete -c flake-edit -n "__fish_seen_subcommand_from p" -f -a "(__fish_complete_inputs_toplevel)" -d Input

set -x ignore_space true
complete -c flake-edit -n "__fish_seen_subcommand_from a" -f -r --keep-order -a "(__fish_complete_add)" -d Add
complete -c flake-edit -n "__fish_seen_subcommand_from add" -f -r --keep-order -a "(__fish_complete_add)" -d Add
complete -c flake-edit -n "__fish_seen_subcommand_from a" -f -r --keep-order -a "(__fish_complete_add)" -d Add
complete -c flake-edit -n "__fish_seen_subcommand_from add" -f -r --keep-order -a "(__fish_complete_add)" -d Add
set -x ignore_space false
Binary file added assets/tape/output/pin_input.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
71 changes: 71 additions & 0 deletions assets/tape/pin_input.tape
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
# VHS documentation
#
# Output:
# Output <path>.gif Create a GIF output at the given <path>
# Output <path>.mp4 Create an MP4 output at the given <path>
# Output <path>.webm Create a WebM output at the given <path>
#
# Require:
# Require <string> Ensure a program is on the $PATH to proceed
#
# Settings:
# Set FontSize <number> Set the font size of the terminal
# Set FontFamily <string> Set the font family of the terminal
# Set Height <number> Set the height of the terminal
# Set Width <number> Set the width of the terminal
# Set LetterSpacing <float> Set the font letter spacing (tracking)
# Set LineHeight <float> Set the font line height
# Set LoopOffset <float>% Set the starting frame offset for the GIF loop
# Set Theme <json|string> Set the theme of the terminal
# Set Padding <number> Set the padding of the terminal
# Set Framerate <number> Set the framerate of the recording
# Set PlaybackSpeed <float> Set the playback speed of the recording
# Set MarginFill <file|#000000> Set the file or color the margin will be filled with.
# Set Margin <number> Set the size of the margin. Has no effect if MarginFill isn't set.
# Set BorderRadius <number> Set terminal border radius, in pixels.
# Set WindowBar <string> Set window bar type. (one of: Rings, RingsRight, Colorful, ColorfulRight)
# Set WindowBarSize <number> Set window bar size, in pixels. Default is 40.
# Set TypingSpeed <time> Set the typing speed of the terminal. Default is 50ms.
#
# Sleep:
# Sleep <time> Sleep for a set amount of <time> in seconds
#
# Type:
# Type[@<time>] "<characters>" Type <characters> into the terminal with a
# <time> delay between each character
#
# Keys:
# Backspace[@<time>] [number] Press the Backspace key
# Down[@<time>] [number] Press the Down key
# Enter[@<time>] [number] Press the Enter key
# Space[@<time>] [number] Press the Space key
# Tab[@<time>] [number] Press the Tab key
# Left[@<time>] [number] Press the Left Arrow key
# Right[@<time>] [number] Press the Right Arrow key
# Up[@<time>] [number] Press the Up Arrow key
# Down[@<time>] [number] Press the Down Arrow key
# PageUp[@<time>] [number] Press the Page Up key
# PageDown[@<time>] [number] Press the Page Down key
# Ctrl+<key> Press the Control key + <key> (e.g. Ctrl+C)
#
# Display:
# Hide Hide the subsequent commands from the output
# Show Show the subsequent commands in the output

Output output/pin_input.gif

Require echo

Set Shell "fish"
Set FontSize 32
Set Width 1200
Set Height 600

Hide
#Type "echo 'Welcome to VHS!'" Sleep 500ms Enter
Type "set -x fish_function_path ''" Enter
Type "clear" Enter
Show

Type "flake-edit --diff pin rust-overlay" Sleep 1000ms Enter
Sleep 5s
11 changes: 11 additions & 0 deletions src/bin/flake-edit/cli.rs
Original file line number Diff line number Diff line change
@@ -49,6 +49,9 @@ impl CliArgs {
pub(crate) fn update(&self) -> bool {
matches!(self.subcommand, Command::Update { .. })
}
pub(crate) fn pin(&self) -> bool {
matches!(self.subcommand, Command::Pin { .. })
}

pub fn flake(&self) -> Option<&String> {
self.flake.as_ref()
@@ -93,6 +96,14 @@ pub(crate) enum Command {
/// Update inputs to their latest specified release.
#[clap(alias = "u")]
Update {},
/// Pin inputs to their current or a specified rev.
#[clap(alias = "p")]
Pin {
/// The id of an input attribute.
id: String,
/// Optionally specify a rev for the inputs attribute.
rev: Option<String>,
},
#[clap(hide = true)]
#[command(name = "completion")]
/// Meant for shell completions.
39 changes: 35 additions & 4 deletions src/bin/flake-edit/main.rs
Original file line number Diff line number Diff line change
@@ -12,6 +12,7 @@ use flake_edit::change::Change;
use flake_edit::diff::Diff;
use flake_edit::edit;
use flake_edit::input::Follows;
use flake_edit::lock::FlakeLock;
use flake_edit::update::Updater;
use nix_uri::urls::UrlWrapper;
use nix_uri::{FlakeRef, NixUriResult};
@@ -92,8 +93,6 @@ fn main() -> eyre::Result<()> {
};
}
}
cli::Command::Update { .. } => {}
cli::Command::List { .. } => {}
cli::Command::Completion { inputs: _, mode } => match mode {
cli::CompletionMode::None => todo!(),
cli::CompletionMode::Add => {
@@ -108,6 +107,7 @@ fn main() -> eyre::Result<()> {
std::process::exit(0);
}
},
Command::Pin { .. } | Command::Update { .. } | Command::List { .. } => {}
}

if let Ok(Some(resulting_change)) = editor.apply_change(change.clone()) {
@@ -144,7 +144,7 @@ fn main() -> eyre::Result<()> {
} else if args.apply() {
app.root.apply(&resulting_change)?;
}
} else if !args.list() && !args.update() {
} else if !args.list() && !args.update() && !args.pin() {
if change.is_remove() {
return Err(eyre::eyre!(
"The input with id: {} could not be removed.",
@@ -232,7 +232,7 @@ fn main() -> eyre::Result<()> {
buf.push_str(input.id());
}
let mut updater = Updater::new(app.root().text().clone(), inputs.clone());
updater.update();
updater.update_all_inputs_to_latest_semver();
let change = updater.get_changes();
if args.diff() {
let old = text.clone();
@@ -244,5 +244,36 @@ fn main() -> eyre::Result<()> {
app.root.apply(&change)?;
}
}
if let Command::Pin { id, rev } = args.subcommand() {
let lock = FlakeLock::from_default_path()?;

let target_rev = if let Some(rev) = rev {
rev.to_string()
} else {
lock.get_rev_by_id(id)?
};

let inputs = editor.list();
let mut buf = String::new();
for input in inputs.values() {
if !buf.is_empty() {
buf.push('\n');
}
buf.push_str(input.id());
}
let mut updater = Updater::new(app.root().text().clone(), inputs.clone());

updater.pin_input_to_ref(id, &target_rev);
let change = updater.get_changes();

if args.diff() {
let old = text.clone();
let new = change;
let diff = Diff::new(&old, &new);
diff.compare();
} else if args.apply() {
app.root.apply(&change)?;
}
}
Ok(())
}
4 changes: 4 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -7,6 +7,10 @@ pub enum FlakeEditError {
Io(#[from] std::io::Error),
#[error("The flake should be a root.")]
NotARoot,
#[error("There is an error in the Lockfile: {0}")]
LockError(String),
#[error("Deserialization Error: {0}")]
Serde(#[from] serde_json::Error),
// Reqwest Error
// #[error("Incorrect Channel")]
// IncorrectChannel(String),
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -5,6 +5,7 @@ pub mod diff;
pub mod edit;
pub mod error;
pub mod input;
pub mod lock;
pub mod state;
pub mod update;
pub mod walk;
Loading

0 comments on commit 4c4a4fc

Please sign in to comment.