From ec0e00c3b15af08ad52bec35c22ebd9d4c83fb56 Mon Sep 17 00:00:00 2001 From: walkowif <59475134+walkowif@users.noreply.github.com> Date: Tue, 7 Nov 2023 12:28:07 +0100 Subject: [PATCH] UX improvements (#6) --- README.md | 20 +++++++++++++++++--- cmd/root.go | 18 +++++++++++++++--- cmd/utils.go | 37 +++++++++++++++++++++++++++++++------ 3 files changed, 63 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 27113e3..c3cf117 100644 --- a/README.md +++ b/README.md @@ -25,6 +25,7 @@ locksmith --logLevel debug --exampleParameter 'exampleValue' ``` Real-life example with multiple input packages and repositories. +Please see below for [an example](#configuration-file) how to set package and repository lists more easily in a configuration file. ```bash locksmith --inputPackageList https://raw.githubusercontent.com/insightsengineering/formatters/main/DESCRIPTION,https://raw.githubusercontent.com/insightsengineering/rtables/main/DESCRIPTION,https://raw.githubusercontent.com/insightsengineering/scda/main/DESCRIPTION,https://raw.githubusercontent.com/insightsengineering/scda.2022/main/DESCRIPTION,https://raw.githubusercontent.com/insightsengineering/nestcolor/main/DESCRIPTION,https://raw.githubusercontent.com/insightsengineering/tern/main/DESCRIPTION,https://raw.githubusercontent.com/insightsengineering/rlistings/main/DESCRIPTION --inputRepositoryList BioC=https://bioconductor.org/packages/release/bioc,CRAN=https://cran.rstudio.com/ @@ -40,7 +41,8 @@ By default `locksmith` will save the resulting output file to `renv.lock`. ## Configuration file If you'd like to set the above options in a configuration file, by default `locksmith` checks `~/.locksmith`, `~/.locksmith.yaml` and `~/.locksmith.yml` files. -If this file exists, `locksmith` uses options defined there, unless they are overridden by command line flags. + +If any of these files exist, `locksmith` will use options defined there, unless they are overridden by command line flags or environment variables. You can also specify custom path to configuration file with `--config .yml` command line flag. When using custom configuration file, if you specify command line flags, the latter will still take precedence. @@ -48,10 +50,22 @@ When using custom configuration file, if you specify command line flags, the lat Example contents of configuration file: ```yaml -logLevel: trace -exampleParameter: exampleValue +logLevel: debug +inputPackages: + - https://raw.githubusercontent.com/insightsengineering/formatters/main/DESCRIPTION + - https://raw.githubusercontent.com/insightsengineering/rtables/main/DESCRIPTION + - https://raw.githubusercontent.com/insightsengineering/scda/main/DESCRIPTION + - https://raw.githubusercontent.com/insightsengineering/scda.2022/main/DESCRIPTION +inputRepositories: + - Bioconductor.BioCsoft=https://bioconductor.org/packages/release/bioc/ + - CRAN=https://cran.rstudio.com/ ``` +The example above shows an alternative way of providing input packages, and input repositories, +as opposed to `inputPackageList` and `inputRepositoryList` CLI flags/YAML keys. + +Additionally, `inputPackageList`/`inputRepositoryList` CLI flags take precendence over `inputPackages`/`inputRepositories` YAML keys. + ## Environment variables `locksmith` reads environment variables with `LOCKSMITH_` prefix and tries to match them with CLI flags. diff --git a/cmd/root.go b/cmd/root.go index 70a2185..3e42837 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -28,12 +28,19 @@ import ( var cfgFile string var logLevel string -var inputPackageList string -var inputRepositoryList string var gitHubToken string var gitLabToken string var outputRenvLock string +// In case the lists are provided as arrays in YAML configuration file: +var inputPackages []string +var inputRepositories []string + +// In case the lists are provided as strings of comma-separated values +// via CLI flag or in an environment variable: +var inputPackageList string +var inputRepositoryList string + var log = logrus.New() func setLogLevel() { @@ -81,7 +88,9 @@ in an renv.lock-compatible file.`, fmt.Println("config =", cfgFile) fmt.Println("inputPackageList =", inputPackageList) fmt.Println("inputRepositoryList =", inputRepositoryList) - fmt.Println("outputRenvLock = ", outputRenvLock) + fmt.Println("inputPackages =", inputPackages) + fmt.Println("inputRepositories =", inputRepositories) + fmt.Println("outputRenvLock =", outputRenvLock) packageDescriptionList, repositoryList, repositoryMap := ParseInput() inputDescriptionFiles := DownloadDescriptionFiles(packageDescriptionList, DownloadTextFile) @@ -173,4 +182,7 @@ func initializeConfig() { checkError(err) } } + // Check if a YAML list of input packages or input repositories has been provided in the configuration file. + inputPackages = viper.GetStringSlice("inputPackages") + inputRepositories = viper.GetStringSlice("inputRepositories") } diff --git a/cmd/utils.go b/cmd/utils.go index 2d92c9b..dbbd521 100644 --- a/cmd/utils.go +++ b/cmd/utils.go @@ -47,14 +47,39 @@ func stringInSlice(a string, list []string) bool { // list of package repository URLs (in descending priority order), and a map from package // repository alias (name) to the package repository URL. func ParseInput() ([]string, []string, map[string]string) { - if len(inputPackageList) < 1 { - log.Fatal("No packages specified. Please use the --inputPackageList flag.") + if len(inputPackageList) < 1 && len(inputPackages) == 0 { + log.Fatal( + "No packages specified. Please use the --inputPackageList flag ", + "or supply the list under inputPackages in YAML config.", + ) } - if len(inputRepositoryList) < 1 { - log.Fatal("No package repositories specified. Please use the --inputRepositoryList flag.") + if len(inputRepositoryList) < 1 && len(inputRepositories) == 0 { + log.Fatal( + "No package repositories specified. Please use the --inputRepositoryList flag ", + "or supply the list under inputRepositories in YAML config.", + ) } - packageList := strings.Split(inputPackageList, ",") - repositoryList := strings.Split(inputRepositoryList, ",") + var packageList []string + if len(inputPackageList) > 0 { + log.Debug( + "--inputPackageList CLI flag or LOCKSMITH_INPUTPACKAGELIST environment variable ", + "has been set and is taking precedence over inputPackages key from YAML config.", + ) + packageList = strings.Split(inputPackageList, ",") + } else { + packageList = inputPackages + } + var repositoryList []string + if len(inputRepositoryList) > 0 { + log.Debug( + "--inputRepositoryList CLI flag or LOCKSMITH_INPUTREPOSITORYLIST environment variable ", + "has been set and is taking precedence over inputRepositories key from YAML config.", + ) + repositoryList = strings.Split(inputRepositoryList, ",") + } else { + repositoryList = inputRepositories + } + outputRepositoryMap := make(map[string]string) var outputRepositoryList []string for _, r := range repositoryList {