Skip to content

Conversation

@dikshagupta1
Copy link
Contributor

Summary

This PR implements support for configuring HAB_AUTH_TOKEN and HAB_REFRESH_CHANNEL in the CLI configuration file (~/.hab/etc/cli.toml) as requested in CHEF-25540. Users can now set these values in the config file as fallbacks when environment variables are not set, while maintaining the proper precedence chain.

Changes Made

  • Added refresh_channel field to CliConfig struct with proper TOML serialization/deserialization
  • Implemented utility functions following existing auth_token patterns for configuration precedence
  • Updated hab pkg build command to use config file fallback while maintaining CLI and env var precedence
  • Added comprehensive unit tests covering CLI arg precedence, environment variable fallback, and config file integration
  • Updated error messages to mention config file as an option alongside CLI args and environment variables
  • Updated environment variables documentation to explain config file alternatives

Configuration Precedence

The implementation maintains the correct precedence chain:

  1. CLI arguments (--auth, --refresh-channel)
  2. Environment variables (HAB_AUTH_TOKEN, HAB_REFRESH_CHANNEL)
  3. Configuration file (~/.hab/etc/cli.toml)
  4. Default values

Testing

  • Unit tests added for CliConfig struct refresh_channel field
  • Unit tests added for utility functions covering all precedence scenarios
  • Integration tests verified configuration file parsing works correctly
  • All existing tests continue to pass
  • Code coverage maintained >80%

Example Configuration

Users can now add the following to ~/.hab/etc/cli.toml:

auth_token = "your_auth_token_here"
refresh_channel = "stable"

Jira Link

CHEF-25540

@dikshagupta1 dikshagupta1 requested review from a team as code owners October 13, 2025 07:13
@netlify
Copy link

netlify bot commented Oct 13, 2025

👷 Deploy Preview for chef-habitat processing.

Name Link
🔨 Latest commit ee85dc2
🔍 Latest deploy log https://app.netlify.com/projects/chef-habitat/deploys/693744bf87184100073082a7

@github-actions github-actions bot added the Documentation Flags an issue / PR for attention by the technical documentation team label Oct 13, 2025
@dikshagupta1 dikshagupta1 removed the Documentation Flags an issue / PR for attention by the technical documentation team label Oct 13, 2025
@dikshagupta1 dikshagupta1 reopened this Oct 13, 2025
@github-actions github-actions bot added the Documentation Flags an issue / PR for attention by the technical documentation team label Oct 13, 2025
@sonarqube-for-infrastructure-prod

Copy link
Contributor

@mwrock mwrock left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We will also want to set the refresh channel via the config for the studio commands as well just like we do with origin, token and url.

We should add a prompt to hab setup for configuring the refresh channel. This is how the other config values can be initially set.

@dikshagupta1 dikshagupta1 removed the Documentation Flags an issue / PR for attention by the technical documentation team label Dec 2, 2025
@dikshagupta1 dikshagupta1 marked this pull request as draft December 3, 2025 07:12
Copilot AI review requested due to automatic review settings December 3, 2025 14:51
@github-actions github-actions bot added the Documentation Flags an issue / PR for attention by the technical documentation team label Dec 3, 2025
@dikshagupta1
Copy link
Contributor Author

We will also want to set the refresh channel via the config for the studio commands as well just like we do with origin, token and url.

We should add a prompt to hab setup for configuring the refresh channel. This is how the other config values can be initially set.

Hi @mwrock ,
Added the requested implementation in commit 9980b27

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR implements support for configuring HAB_AUTH_TOKEN and HAB_REFRESH_CHANNEL in the CLI configuration file (~/.hab/etc/cli.toml) as fallbacks when environment variables are not set, maintaining proper precedence: CLI arguments > environment variables > config file > defaults.

Key Changes:

  • Added refresh_channel field to CliConfig struct with TOML serialization support
  • Implemented utility functions for configuration precedence handling
  • Updated error messages to reference the config file option

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
components/hab/src/lib.rs Added REFRESH_CHANNEL_ENVVAR constant
components/hab/src/command/studio/enter.rs Integrated refresh channel config with studio environment setup
components/hab/src/command/cli/setup.rs Added interactive setup for default refresh channel configuration
components/hab/src/cli_v4/utils.rs Implemented precedence handling utilities and comprehensive unit tests
components/hab/src/cli_v4/pkg/build.rs Updated build command to use config file fallback with proper precedence
components/docs-chef-io/content/habitat/environment_variables.md Updated documentation to reference config file options
components/common/src/cli_config.rs Added refresh_channel field and unit tests for serialization

…H_CHANNEL

- Added refresh_channel field to CliConfig struct in cli_config.rs
- Implemented utility functions for config fallback with proper precedence
- Updated hab pkg build to use config file when env vars not set
- Added comprehensive unit tests for new functionality
- Updated error messages to mention config file options
- Updated environment variables documentation with config file info
- Maintains precedence: CLI args > env vars > config file > defaults

Signed-off-by: dikshagupta1 <dikshagupta1303@gmail.com>
Signed-off-by: dikshagupta1 <dikshagupta1303@gmail.com>
Signed-off-by: dikshagupta1 <dikshagupta1303@gmail.com>
@dikshagupta1 dikshagupta1 marked this pull request as ready for review December 3, 2025 18:29
Copilot AI review requested due to automatic review settings December 3, 2025 18:29
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 8 out of 8 changed files in this pull request and generated 2 comments.

@dikshagupta1 dikshagupta1 requested a review from a team December 4, 2025 06:14
@dikshagupta1 dikshagupta1 removed the Documentation Flags an issue / PR for attention by the technical documentation team label Dec 4, 2025
bldr_auth_token_from_args_env_or_load(opt).ok()
}

pub(crate) fn refresh_channel_from_args_env_or_load(opt: Option<String>) -> Result<String, Error> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd have this name in in _or_config

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean to change it to bldr_auth_token_in _or_config??

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry I meant refresh_channel_from_args_env_or_config

https://www.habitat.sh/docs/using-habitat/#using-channels")?;
if ask_default_refresh_channel(ui)? {
ui.br()?;
ui.para("Enter your default Habitat refresh channel (e.g., 'stable', 'unstable').")?;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Id replace unstable with base as an example

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

addressed in 49c6608

write_cli_config_refresh_channel(&refresh_channel)?;
} else {
ui.para("Alright, maybe another time. You can also set a `HAB_REFRESH_CHANNEL` \
environment variable or use the `--channel` flag when using the cli.")?;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think you mean --refresh-channel

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh yes, that was a mistake. Thankyou for noticing it.
addressed in 49c6608

ui.heading("Habitat Refresh Channel")?;
ui.para("The Habitat refresh channel determines which channel to use when refreshing \
packages during builds and other operations. Common values include 'stable' for \
stable releases and 'unstable' for latest development builds. If you set a \
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

base instead of unstable as a common example.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

addressed in 49c6608

Signed-off-by: dikshagupta1 <dikshagupta1303@gmail.com>
@github-actions github-actions bot added the Documentation Flags an issue / PR for attention by the technical documentation team label Dec 5, 2025
Signed-off-by: dikshagupta1 <dikshagupta1303@gmail.com>
Copilot AI review requested due to automatic review settings December 5, 2025 12:46
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 8 out of 8 changed files in this pull request and generated 6 comments.

Comment on lines +945 to +948
unsafe { env::remove_var("HAB_REFRESH_CHANNEL") };

// Set env var
unsafe { env::set_var("HAB_REFRESH_CHANNEL", "staging") };
Copy link

Copilot AI Dec 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The use of unsafe blocks for environment variable manipulation in tests is unnecessary. Rust's std::env::set_var and std::env::remove_var are safe functions and don't require unsafe blocks. Remove the unsafe wrappers around these function calls.

Copilot uses AI. Check for mistakes.
assert_eq!(result, Some("staging".to_string()));

// Clean up
unsafe { env::remove_var("HAB_REFRESH_CHANNEL") };
Copy link

Copilot AI Dec 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The use of unsafe blocks for environment variable manipulation in tests is unnecessary. Rust's std::env::set_var and std::env::remove_var are safe functions and don't require unsafe blocks. Remove the unsafe wrappers around these function calls.

Copilot uses AI. Check for mistakes.
#[test]
fn test_refresh_channel_fallback_to_none() {
// Clean env var to ensure fallback behavior
unsafe { env::remove_var("HAB_REFRESH_CHANNEL") };
Copy link

Copilot AI Dec 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The use of unsafe blocks for environment variable manipulation in tests is unnecessary. Rust's std::env::set_var and std::env::remove_var are safe functions and don't require unsafe blocks. Remove the unsafe wrappers around these function calls.

Copilot uses AI. Check for mistakes.
Comment on lines +969 to +972
unsafe { env::remove_var("HAB_REFRESH_CHANNEL") };

// Set env var
unsafe { env::set_var("HAB_REFRESH_CHANNEL", "env_channel") };
Copy link

Copilot AI Dec 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The use of unsafe blocks for environment variable manipulation in tests is unnecessary. Rust's std::env::set_var and std::env::remove_var are safe functions and don't require unsafe blocks. Remove the unsafe wrappers around these function calls.

Copilot uses AI. Check for mistakes.
assert_eq!(result, Some("cli_channel".to_string()));

// Clean up
unsafe { env::remove_var("HAB_REFRESH_CHANNEL") };
Copy link

Copilot AI Dec 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The use of unsafe blocks for environment variable manipulation in tests is unnecessary. Rust's std::env::set_var and std::env::remove_var are safe functions and don't require unsafe blocks. Remove the unsafe wrappers around these function calls.

Copilot uses AI. Check for mistakes.
docker: bool,

/// Channel used to retrieve plan dependencies for Chef supported origins
/// Uses value from the `HAB_REFRESH_CHANNEL` env variable or cli.toml config file if not set
Copy link

Copilot AI Dec 5, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The documentation comment should accurately describe the full precedence chain. Update to mention CLI argument takes priority: 'Uses value from --refresh-channel CLI arg, HAB_REFRESH_CHANNEL env variable, cli.toml config file, or defaults to "base" if not set'.

Suggested change
/// Uses value from the `HAB_REFRESH_CHANNEL` env variable or cli.toml config file if not set
/// Uses value from the `--refresh-channel` CLI argument, `HAB_REFRESH_CHANNEL` environment variable, `cli.toml` config file, or defaults to `"base"` if not set

Copilot uses AI. Check for mistakes.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We definitely want to indicate that base is the default. Also we need to add a period(.) to the comment on line 71 because the --help text concatenates these two comments.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed in commit ee85dc2

docker: bool,

/// Channel used to retrieve plan dependencies for Chef supported origins
/// Uses value from the `HAB_REFRESH_CHANNEL` env variable or cli.toml config file if not set
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We definitely want to indicate that base is the default. Also we need to add a period(.) to the comment on line 71 because the --help text concatenates these two comments.

ui.heading("Habitat Refresh Channel")?;
ui.para("The Habitat refresh channel determines which channel to use for core packages \
during a build. Common values include 'stable' for stable releases and 'base' \
for latest development builds. If you set a refresh channel here, it will be used \
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for latest development builds -> for latest suported packages.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed in commit ee85dc2

ui.para("If you would like to save a default refresh channel for use by the Habitat \
client, please enter your preferred channel. Otherwise, just enter No.")?;
ui.para("For more information on Habitat channels, please read the documentation at \
https://www.habitat.sh/docs/using-habitat/#using-channels")?;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should delete this line and the one above it because the link looks irrelevant. I know the v2 docs will have https://docs.chef.io/habitat/2.0-rc1/supported_packages/package_refresh_strategy/ but I don't think we want ot add an rc link here.

Also we should mention that base is the default.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addresses in commit ee85dc2

Signed-off-by: dikshagupta1 <dikshagupta1303@gmail.com>
@sonarqube-for-infrastructure-prod

Quality Gate failed Quality Gate failed

Failed conditions
1 New issue

See analysis details on SonarQube

Catch issues before they fail your Quality Gate with our IDE extension SonarQube for IDE SonarQube for IDE

@mwrock
Copy link
Contributor

mwrock commented Dec 9, 2025

This all looks good now. Just a couple CI rustfmt and sonar issues you need to address.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Documentation Flags an issue / PR for attention by the technical documentation team

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants