Skip to content

Commit

Permalink
⭐️ support custom provider path
Browse files Browse the repository at this point in the history
  • Loading branch information
chris-rock committed Jan 15, 2024
1 parent 4e5cde9 commit 49802ab
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 13 deletions.
12 changes: 6 additions & 6 deletions .github/workflows/pr-test-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -84,23 +84,23 @@ jobs:
- name: 'Set up gcloud CLI'
uses: 'google-github-actions/setup-gcloud@v2'


- name: Set provider env
run: echo "PROVIDER_PATH=${PWD}/.providers" >> $GITHUB_ENV
- name: Display Provider PAth
run: echo $PROVIDER_PATH
- name: Ensure no providers are installed
run: |
cnquery providers list
rm -rf ~/.config/cnquery/providers
rm -rf /opt/mondoo/providers/os
cnquery providers list
- name: Test cnquery
run: make test/go/plain-ci

- name: Test Providers
run: make providers/test

- uses: actions/upload-artifact@v4 # upload test results
if: success() || failure() # run this step even if previous step failed
with:
with:
name: test-results
path: report.xml

Expand Down
9 changes: 7 additions & 2 deletions apps/cnquery/cmd/providers.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,11 +160,16 @@ func printProviders(p []*providers.Provider) {
}

printProviderPath("builtin", paths["builtin"], false)
printProviderPath(providers.HomePath, paths[providers.HomePath], true)
printProviderPath(providers.SystemPath, paths[providers.SystemPath], true)
if providers.CustomProviderPath == "" {
printProviderPath(providers.HomePath, paths[providers.HomePath], true)
printProviderPath(providers.SystemPath, paths[providers.SystemPath], true)
} else {
printProviderPath(providers.CustomProviderPath, paths[providers.CustomProviderPath], true)
}
delete(paths, "builtin")
delete(paths, providers.HomePath)
delete(paths, providers.SystemPath)
delete(paths, providers.CustomProviderPath)

keys := sortx.Keys(paths)
for _, path := range keys {
Expand Down
25 changes: 20 additions & 5 deletions providers/providers.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ import (
)

var (
SystemPath string
HomePath string
SystemPath string
HomePath string
CustomProviderPath string
// this is the default path for providers, it's either system or home path, if the user is root the system path is used
DefaultPath string
// CachedProviders contains all providers that have been loaded the last time
Expand All @@ -49,6 +50,10 @@ func init() {
HomePath, _ = config.HomePath("providers")
DefaultPath = HomePath
}
CustomProviderPath = os.Getenv("PROVIDERS_PATH")
if CustomProviderPath != "" {
DefaultPath = CustomProviderPath
}

LastProviderInstall = time.Now().Unix()
}
Expand Down Expand Up @@ -186,7 +191,7 @@ func ListAll() ([]*Provider, error) {
CachedProviders = all

// This really shouldn't happen, but just in case it does...
if SystemPath == "" && HomePath == "" {
if SystemPath == "" && HomePath == "" && CustomProviderPath == "" {
log.Warn().Msg("can't find any paths for providers, none are configured")
return nil, nil
}
Expand All @@ -204,15 +209,25 @@ func ListAll() ([]*Provider, error) {
msg.Msg("can't find any paths for providers, none are configured")
}

if sysOk {
// when the user provides a custom provider path, we always load it and we ignore the system and home path
// we do not check for its existence, and instead create it on the fly when needed
if CustomProviderPath != "" {
cur, err := findProviders(CustomProviderPath)
if err != nil {
log.Warn().Str("path", CustomProviderPath).Err(err).Msg("failed to get providers from custom provider path")
}
all = append(all, cur...)
}

if sysOk && CustomProviderPath == "" {
cur, err := findProviders(SystemPath)
if err != nil {
log.Warn().Str("path", SystemPath).Err(err).Msg("failed to get providers from system path")
}
all = append(all, cur...)
}

if homeOk {
if homeOk && CustomProviderPath == "" {
cur, err := findProviders(HomePath)
if err != nil {
log.Warn().Str("path", HomePath).Err(err).Msg("failed to get providers from home path")
Expand Down

0 comments on commit 49802ab

Please sign in to comment.