Skip to content

Commit

Permalink
Merge branch 'UDENG-5696-initial-client-core-cli' into UDENG-5705-Ubu…
Browse files Browse the repository at this point in the history
…ntu-Insights-Consent-Manager
  • Loading branch information
hk21702 committed Jan 13, 2025
2 parents 534bcc3 + 57ecd05 commit 991b872
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 8 deletions.
13 changes: 13 additions & 0 deletions cmd/insights/commands/consent.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package commands

import (
"fmt"
"log/slog"
"slices"
"strings"

"github.com/spf13/cobra"
Expand All @@ -25,6 +27,17 @@ func installConsentCmd(app *App) {
Short: "Manage or get user consent state",
Long: "Manage or get user consent state for data collection and upload",
Args: cobra.ArbitraryArgs,
PreRunE: func(cmd *cobra.Command, args []string) error {
app.rootCmd.SilenceUsage = false

validConsentStates := []string{"true", "false", ""}
if !slices.Contains(validConsentStates, strings.ToLower(app.consentConfig.consentState)) {
return fmt.Errorf("consent-state must be either true, false, or not set")
}

app.rootCmd.SilenceUsage = true
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
// Set Sources to Args
app.consentConfig.sources = args
Expand Down
9 changes: 5 additions & 4 deletions internal/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package constants
import (
"log/slog"
"os"
"path/filepath"
)

const (
Expand Down Expand Up @@ -32,22 +33,22 @@ type option func(*options)

// GetDefaultConfigPath is the default path to the configuration file.
func GetDefaultConfigPath(opts ...option) string {
o := options{baseDir: os.UserCacheDir}
o := options{baseDir: os.UserConfigDir}
for _, opt := range opts {
opt(&o)
}

return getBaseDir(o.baseDir) + string(os.PathSeparator) + DefaultAppFolder
return filepath.Join(getBaseDir(o.baseDir), DefaultAppFolder)
}

// GetDefaultCachePath is the default path to the cache directory.
func GetDefaultCachePath(opts ...option) string {
o := options{baseDir: os.UserConfigDir}
o := options{baseDir: os.UserCacheDir}
for _, opt := range opts {
opt(&o)
}

return getBaseDir(o.baseDir) + string(os.PathSeparator) + DefaultAppFolder
return filepath.Join(getBaseDir(o.baseDir), DefaultAppFolder)
}

// getBaseDir is a helper function to handle the case where the baseDir function returns an error, and instead return an empty string.
Expand Down
8 changes: 4 additions & 4 deletions internal/constants/constants_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ func Test_GetDefaultConfigPath(t *testing.T) {
},
},
"os.UserConfigDir error": {
want: string(os.PathSeparator) + constants.DefaultAppFolder,
want: constants.DefaultAppFolder,
mock: func() (string, error) {
return "", fmt.Errorf("os.UserCacheDir error")
},
},
"os.UserConfigDir error 2": {
want: string(os.PathSeparator) + constants.DefaultAppFolder,
want: constants.DefaultAppFolder,
mock: func() (string, error) {
return "abc", fmt.Errorf("os.UserCacheDir error")
},
Expand Down Expand Up @@ -61,13 +61,13 @@ func Test_GetDefaultCachePath(t *testing.T) {
},
},
"os.UserCacheDir error": {
want: string(os.PathSeparator) + constants.DefaultAppFolder,
want: constants.DefaultAppFolder,
mock: func() (string, error) {
return "", fmt.Errorf("os.UserCacheDir error")
},
},
"os.UserCacheDir error with return": {
want: string(os.PathSeparator) + constants.DefaultAppFolder,
want: constants.DefaultAppFolder,
mock: func() (string, error) {
return "return", fmt.Errorf("os.UserCacheDir error")
},
Expand Down

0 comments on commit 991b872

Please sign in to comment.