Skip to content

Commit

Permalink
UX improvements (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
walkowif authored Nov 7, 2023
1 parent 980c6f6 commit ec0e00c
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 12 deletions.
20 changes: 17 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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/
Expand All @@ -40,18 +41,31 @@ 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 <your-configuration-file>.yml` command line flag.
When using custom configuration file, if you specify command line flags, the latter will still take precedence.

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.
Expand Down
18 changes: 15 additions & 3 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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")
}
37 changes: 31 additions & 6 deletions cmd/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit ec0e00c

Please sign in to comment.