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 138c731 + ede5882 commit 534bcc3
Show file tree
Hide file tree
Showing 13 changed files with 41 additions and 28 deletions.
1 change: 1 addition & 0 deletions cmd/exposed-server/daemon/daemon.go
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
// Package daemon is responsible for running the exposed-server in the background.
package daemon
1 change: 1 addition & 0 deletions cmd/exposed-server/main.go
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
// Package main for the exposed-server.
package main
1 change: 1 addition & 0 deletions cmd/ingest-server/daemon/daemon.go
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
// Package daemon is responsible for running the ingest-server in the background.
package daemon
1 change: 1 addition & 0 deletions cmd/ingest-server/main.go
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
// Package main for the ingest-server.
package main
7 changes: 3 additions & 4 deletions cmd/insights/commands/collect.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ var defaultCollectConfig = collectConfig{
extraMetrics: "",
}

func installCollectCmd(app *App) error {
func installCollectCmd(app *App) {
app.collectConfig = defaultCollectConfig

collectCmd := &cobra.Command{
Expand All @@ -45,11 +45,11 @@ func installCollectCmd(app *App) error {

fileInfo, err := os.Stat(args[1])
if err != nil {
return fmt.Errorf("the second argument, SOURCE-METRICS-PATH, should be a valid JSON file. Error: %s", err.Error())
return fmt.Errorf("the second argument, source-metrics-path, should be a valid JSON file. Error: %s", err.Error())
}

if fileInfo.IsDir() {
return fmt.Errorf("the second argument, SOURCE-METRICS-PATH, should be a valid JSON file, not a directory.")
return fmt.Errorf("the second argument, source-metrics-path, should be a valid JSON file, not a directory")
}
}

Expand All @@ -73,5 +73,4 @@ func installCollectCmd(app *App) error {
collectCmd.Flags().BoolVarP(&app.collectConfig.dryRun, "dry-run", "d", false, "perform a dry-run where a report is collected, but not written to disk")

app.rootCmd.AddCommand(collectCmd)
return nil
}
10 changes: 5 additions & 5 deletions cmd/insights/commands/root.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Package commands contains the commands for the Ubuntu Insights CLI.
package commands

import (
Expand All @@ -7,8 +8,7 @@ import (
"github.com/ubuntu/ubuntu-insights/internal/constants"
)

const cmdName = "ubuntu-insights"

// App represents the application.
type App struct {
rootCmd *cobra.Command

Expand All @@ -30,11 +30,11 @@ var defaultRootConfig = rootConfig{
InsightsDir: constants.GetDefaultCachePath(),
}

// Registers commands and returns a new app
// New registers commands and returns a new App.
func New() (*App, error) {
a := App{}
a.rootCmd = &cobra.Command{
Use: "ubuntu-insights [COMMAND]",
Use: constants.CmdName + " [COMMAND]",
Short: "",
Long: "",
SilenceErrors: true,
Expand All @@ -48,7 +48,7 @@ func New() (*App, error) {
}

err := installRootCmd(&a)
err = installCollectCmd(&a)
installCollectCmd(&a)
installUploadCmd(&a)
installConsentCmd(&a)

Expand Down
3 changes: 1 addition & 2 deletions cmd/insights/commands/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ package commands

import (
"context"
"testing"

"log/slog"
"testing"

"github.com/stretchr/testify/assert"
"github.com/ubuntu/ubuntu-insights/internal/constants"
Expand Down
4 changes: 2 additions & 2 deletions cmd/insights/main.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// Main package for the insights command line tool.
package main

import (
"os"

"log/slog"
"os"

"github.com/ubuntu/ubuntu-insights/cmd/insights/commands"
"github.com/ubuntu/ubuntu-insights/internal/constants"
Expand Down
1 change: 1 addition & 0 deletions cmd/insights/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ func TestRun(t *testing.T) {

for name, tc := range tests {
t.Run(name, func(t *testing.T) {
t.Parallel()
a := testApp{
done: make(chan struct{}),
runError: tc.runError,
Expand Down
18 changes: 11 additions & 7 deletions internal/constants/constants.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
// Package constants is responsible for defining the constants used in the application.
// It also provides utility functions to get the default configuration and cache paths.
package constants

import (
"os"

"log/slog"
"os"
)

const (
// DefaultAppFolder is the name of the default root folder
// CmdName is the name of the command line tool.
CmdName = "ubuntu-insights"

// DefaultAppFolder is the name of the default root folder.
DefaultAppFolder = "ubuntu-insights"

// DefaultLogLevel is the default log level selected without any verbosity flags
// DefaultLogLevel is the default log level selected without any verbosity flags.
DefaultLogLevel = slog.LevelInfo

// DefaultConfigFileName is the default base name of the consent state files
Expand All @@ -26,7 +30,7 @@ type options struct {

type option func(*options)

// GetDefaultConfigPath is the default path to the configuration file
// GetDefaultConfigPath is the default path to the configuration file.
func GetDefaultConfigPath(opts ...option) string {
o := options{baseDir: os.UserCacheDir}
for _, opt := range opts {
Expand All @@ -36,7 +40,7 @@ func GetDefaultConfigPath(opts ...option) string {
return getBaseDir(o.baseDir) + string(os.PathSeparator) + DefaultAppFolder
}

// GetDefaultCachePath is the default path to the cache directory
// GetDefaultCachePath is the default path to the cache directory.
func GetDefaultCachePath(opts ...option) string {
o := options{baseDir: os.UserConfigDir}
for _, opt := range opts {
Expand All @@ -46,7 +50,7 @@ func GetDefaultCachePath(opts ...option) string {
return getBaseDir(o.baseDir) + string(os.PathSeparator) + DefaultAppFolder
}

// getBaseDir is a helper function to handle the case where the baseDir function returns an error, and instead return an empty string
// getBaseDir is a helper function to handle the case where the baseDir function returns an error, and instead return an empty string.
func getBaseDir(baseDirFunc func() (string, error)) string {
dir, err := baseDirFunc()
if err != nil {
Expand Down
16 changes: 9 additions & 7 deletions internal/constants/constants_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import (
"github.com/ubuntu/ubuntu-insights/internal/constants"
)

func Test_GetUserConfigDir(t *testing.T) {
//nolint:dupl //Tests for GetDefaultConfigPath is very similar to GetDefaultCachePath.
func Test_GetDefaultConfigPath(t *testing.T) {
t.Parallel()

tests := map[string]struct {
Expand All @@ -25,13 +26,13 @@ func Test_GetUserConfigDir(t *testing.T) {
"os.UserConfigDir error": {
want: string(os.PathSeparator) + constants.DefaultAppFolder,
mock: func() (string, error) {
return "", fmt.Errorf("error")
return "", fmt.Errorf("os.UserCacheDir error")
},
},
"os.UserConfigDir error 2": {
want: string(os.PathSeparator) + constants.DefaultAppFolder,
mock: func() (string, error) {
return "abc", fmt.Errorf("error")
return "abc", fmt.Errorf("os.UserCacheDir error")
},
},
}
Expand All @@ -45,7 +46,8 @@ func Test_GetUserConfigDir(t *testing.T) {
}
}

func Test_userCacheDir(t *testing.T) {
//nolint:dupl //Tests for GetDefaultConfigPath is very similar to GetDefaultCachePath.
func Test_GetDefaultCachePath(t *testing.T) {
t.Parallel()

tests := map[string]struct {
Expand All @@ -61,13 +63,13 @@ func Test_userCacheDir(t *testing.T) {
"os.UserCacheDir error": {
want: string(os.PathSeparator) + constants.DefaultAppFolder,
mock: func() (string, error) {
return "", fmt.Errorf("error")
return "", fmt.Errorf("os.UserCacheDir error")
},
},
"os.UserCacheDir error 2": {
"os.UserCacheDir error with return": {
want: string(os.PathSeparator) + constants.DefaultAppFolder,
mock: func() (string, error) {
return "abc", fmt.Errorf("error")
return "return", fmt.Errorf("os.UserCacheDir error")
},
},
}
Expand Down
2 changes: 1 addition & 1 deletion internal/constants/export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package constants

type Option = option

func WithBaseDir(baseDir func() (string, error)) option {
func WithBaseDir(baseDir func() (string, error)) func(o *options) {
return func(o *options) {
o.baseDir = baseDir
}
Expand Down
4 changes: 4 additions & 0 deletions internal/testutils/cmd.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// Package testutils provides helper functions for testing
package testutils

import (
Expand All @@ -8,6 +9,7 @@ import (
"github.com/stretchr/testify/assert"
)

// CmdTestCase is a test case for testing cobra CMD flags.
type CmdTestCase struct {
Name string
Short string
Expand All @@ -17,7 +19,9 @@ type CmdTestCase struct {
BaseCmd *cobra.Command
}

// FlagTestHelper is a helper function to test cobra CMD flags.
func FlagTestHelper(t *testing.T, testCase CmdTestCase) {
t.Helper()
var flag *pflag.Flag

if testCase.PersistentFlag {
Expand Down

0 comments on commit 534bcc3

Please sign in to comment.