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

feat: Config flag error ignored during login #251 #259

Merged
merged 11 commits into from
Nov 26, 2024

Conversation

qcserestipy
Copy link
Contributor

@qcserestipy qcserestipy commented Nov 15, 2024

Fixes #251

This pull request is currently work in progress.

Description
This pull request focuses on configuration handling and the login process.

Changes

  • config flag is now properly handled in the runLogin function. Originally the default path was always taken.
  • Added ConfigPath to loginview
  • Added ValidateConfigPath function to ensure the configuration file path ends with .yaml or .yml.
  • Existing config files only get updated if credentials do not yet exist
  • Overwrite config entry when password or more attributes have changed for same name

Open Changes

  • Adding support for environment variable of config path

Even though this PR is still WIP I would be happy to receive your feedback on any code changes.

Copy link
Collaborator

@bupd bupd left a comment

Choose a reason for hiding this comment

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

Thanks for your contribution.

Also we can store user-defined config path in data file. to persist across sessions.

  • update config default location to $XDG_CONFIG_HOME/harbor-cli/config.yaml

  • store the data file in $XDG_DATA_HOME/harbor-cli/data which is also ~/.local/share/harbor-cli/data. In the data file we can store the user defined config path.

  • With this we will be having the robust config option for the harbor-cli.

Comment on lines 85 to 97
huh.NewInput().
Title("Config File").
Value(&loginView.Config).
Description("Path to the config file").
Validate(func(str string) error {
if strings.TrimSpace(str) == "" {
return errors.New("config file cannot be empty or only spaces")
}
if isValid := utils.ValidateConfigPath(str); !isValid {
return errors.New("The config file path is not valid. It should be a valid path with a .yaml or .yml extension")
}
return nil
}),
Copy link
Collaborator

Choose a reason for hiding this comment

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

Instead of having config as an input to login command. We can have --config as global flag. with which user can specify the default config location to store the config.

@bupd
Copy link
Collaborator

bupd commented Nov 15, 2024

@qcserestipy Please sign your commits

@qcserestipy
Copy link
Contributor Author

@bupd Thank you for your feedback! Yes, you were right. The config was set as a global flag already. I removed the option from the login screen. The credential handling is now moved to the runLogin function only. I will start looking into the $XDG_CONFIG_HOME config handling you suggested.

@qcserestipy
Copy link
Contributor Author

The changes ensure that the application consistently references the active configuration, manages multiple credential sets efficiently, and maintains a reliable state across different operations.

Key Changes

  1. PersistentPreRun Function for Configuration Initialization

    • Purpose: Ensures that the configuration is initialized before any command execution.
    • Benefits: Guarantees that all commands have access to the necessary configuration data, preventing runtime errors related to uninitialized configurations.
  2. Data File Logic for Tracking Active Configuration

    • Purpose: Implements a data file (data.yaml) that keeps track of the last active configuration file (config.yaml).
    • Benefits: Allows users to switch between multiple configuration files seamlessly without manually specifying the config path each time.
  3. Global Pointers for DataFile and ConfigFile

    • Purpose: Makes HarborData and HarborConfig accessible globally through pointers.
    • Benefits: Simplifies access to configuration and data throughout the application, reducing the need for passing configuration objects between functions and packages.
  4. Harbor Client for Credential Retrieval

    • Purpose: Adjusts the Harbor client to retrieve credentials using the global configuration pointer.
    • Benefits: Ensures that all client operations consistently use the currently active credentials, enhancing security and reliability.
  5. Credential Management Enhancements in Login Command

    • Purpose: Adds functionality to the login process to update credentials in the configuration file after a successful login.
    • Benefits: Automatically manages and updates credential sets, reducing manual intervention and minimizing the risk of credential mismatches.

Showcase

bitmap

1

The datafile is created and it contains info about the config path which is in the config2.yaml file

2 and 3

I log in to the server and choose a new config file called config.yaml. The login proceeds, the file is created and the data file is updated.

4

The data file contains information about an old file config3.yaml that does not exist anymore. The cli crashes. The user is informed to create a new blank config3.yaml or change the name to a new config file where the data file gets updated.

5

The usual harbor subcommands can read the peristent config file specified in data.yaml, or use a config file that is given on the fly by the --config flag.

@qcserestipy
Copy link
Contributor Author

qcserestipy commented Nov 18, 2024

@bupd Is there a good way to test this code in unit tests?

log.Fatalf("Unable to determine user home directory: %v", err)
}

xdgConfigHome := os.Getenv("XDG_CONFIG_HOME")
Copy link
Member

Choose a reason for hiding this comment

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

does UserHomeDir() not already respect the XDG_CONFIG_HOME?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I followed the instructions in this specification. According to section 3 the xdg home should default to $HOME/.config in case the XDG environment variables are not set in the shell/env. Therefore I thought it could be helpful to check whether a user has a $HOME, since this might not be fulfilled for e.g. machine users.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

But I will double check whether the result in the expected behavior, and want to include this in some unit testing.

@Vad1mo
Copy link
Member

Vad1mo commented Nov 22, 2024

…igpath flag check to determine if given

Signed-off-by: qcserestipy <[email protected]>
…e logic that keeps track of the last active config; Made DataFile and ConfigFile Available through global pointer. Adjusted harbor client to retrieve credentials through config pointer; Added funtionality to login run to update credentials after login

Signed-off-by: qcserestipy <[email protected]>
…ionality for Config and Data Pointer

Signed-off-by: qcserestipy <[email protected]>
@Vad1mo
Copy link
Member

Vad1mo commented Nov 22, 2024

@qcserestipy can you also address the discussion points we can than move forward

@Vad1mo Vad1mo requested a review from bupd November 22, 2024 10:09
…sts; added config tests; adapted login tests to work with new config management

Signed-off-by: Patrick Eschenbach <[email protected]>
…sts; added config tests; adapted login tests to work with new config management

Signed-off-by: Patrick Eschenbach <[email protected]>
@qcserestipy
Copy link
Contributor Author

@Vad1mo Thank you for making me aware of the sign-off issue, I tried to resolve with the link you mentioned. I added some tests for the config and adapted the tests for the login subcommand.

Are there other points that I did not cover, yet?

@qcserestipy qcserestipy changed the title !WIP; feat: Config flag error ignored during login #251 #258 feat: Config flag error ignored during login #251 #258 Nov 22, 2024
@qcserestipy
Copy link
Contributor Author

I removed the WIP tag for now

@qcserestipy qcserestipy changed the title feat: Config flag error ignored during login #251 #258 feat: Config flag error ignored during login #251 Nov 22, 2024
…g pipeline about unused err in config_test.go

Signed-off-by: Patrick Eschenbach <[email protected]>
Copy link
Collaborator

@bupd bupd left a comment

Choose a reason for hiding this comment

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

LGTM! 🚀

@Vad1mo Vad1mo merged commit 0d1f474 into goharbor:main Nov 26, 2024
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Config flag error ignored during login
3 participants