Skip to content
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

Replace --use-scoped-token with --use-scoped-token=always|never #154

Merged
merged 2 commits into from
Nov 6, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 31 additions & 9 deletions src/cli/cmd/apply/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,35 @@ use self::{home_manager::HomeManager, nix_darwin::NixDarwin, nixos::NixOs};

use super::{CommandExecute, FlakeHubClient};

#[derive(Copy, Clone, PartialEq, Eq, clap::ValueEnum)]
enum TokenChoice {
Always,
Never,
}

impl std::fmt::Display for TokenChoice {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(
f,
"{}",
match self {
TokenChoice::Always => "always",
TokenChoice::Never => "never",
}
)
}
}

/// Apply the configuration at the specified FlakeHub output reference to the current system
#[derive(Parser)]
pub(crate) struct ApplySubcommand {
#[clap(subcommand)]
system: System,

/// Use a scoped token generated by FlakeHub that allows substituting the given output _only_.
#[clap(long, default_value_t = true)]
use_scoped_token: bool,
/// By default, fh apply exchanges its API token for a tightly scoped token generated by FlakeHub that _only_ allows substituting the given output.
/// Pass --use-scoped-token=never to use the system's FlakeHub token, and not perform exchanging for a tightly scoped token.
#[clap(long, default_value_t = TokenChoice::Always)]
use_scoped_token: TokenChoice,

#[clap(from_global)]
api_addr: url::Url,
Expand Down Expand Up @@ -85,10 +105,12 @@ impl CommandExecute for ApplySubcommand {

tracing::info!(%output_ref, "Resolving output reference");

let resolved_path =
FlakeHubClient::resolve(self.api_addr.as_ref(), &output_ref, self.use_scoped_token)
.await?;

let resolved_path = FlakeHubClient::resolve(
self.api_addr.as_ref(),
&output_ref,
self.use_scoped_token == TokenChoice::Always,
)
.await?;
tracing::debug!(
"Successfully resolved reference {} to path {}",
&output_ref,
Expand All @@ -99,7 +121,7 @@ impl CommandExecute for ApplySubcommand {

match resolved_path.token {
Some(token) => {
if self.use_scoped_token {
if self.use_scoped_token == TokenChoice::Always {
let mut nix_args = vec![
"copy".to_string(),
"--option".to_string(),
Expand Down Expand Up @@ -168,7 +190,7 @@ impl CommandExecute for ApplySubcommand {
}
}
None => {
if self.use_scoped_token {
if self.use_scoped_token == TokenChoice::Always {
return Err(color_eyre::eyre::eyre!(
"FlakeHub did not return a restricted token!"
));
Expand Down
Loading