Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cli test #34

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions cli/cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ func setupOutputFormat(cmd *cobra.Command) {
}

func loadConfig(cmd *cobra.Command, args []string) {
fmt.Println("Loading configuration...") // Debug line

config, err := config.LoadConfig(configFile)
if err != nil {
cliLogger.Fatal("could not load config", zap.Error(err))
Expand All @@ -112,13 +114,17 @@ func loadConfig(cmd *cobra.Command, args []string) {
}

func validateConfig(cmd *cobra.Command, args []string) {
fmt.Println("Validating configuration...") // Debug line

if cliConfig.IsEmpty() {
cliLogger.Warn("You haven't configured your CLI, some commands might fail!")
cliLogger.Warn("Run 'qt configure' to configure your CLI")
}
}

func setupLogger(cmd *cobra.Command, args []string) {
fmt.Println("Setting up logger...") // Debug line

atom := zap.NewAtomicLevel()
if verbose {
atom.SetLevel(zap.DebugLevel)
Expand Down Expand Up @@ -150,10 +156,14 @@ func setupLogger(cmd *cobra.Command, args []string) {
}

func teardownCommand(cmd *cobra.Command, args []string) {
fmt.Println("Tearing down command...") // Debug line

cliLogger.Sync()
}

func setupVersion() {
fmt.Println("Setting up version...") // Debug line

versionText, isVersionMatch = actions.GetVersion(
context.Background(),
cliConfig,
Expand All @@ -162,6 +172,8 @@ func setupVersion() {
}

func validateVersionMismatch() {
fmt.Println("Validating version mismatch...") // Debug line

if !isVersionMatch && os.Getenv("TRACETEST_DEV") == "" {
fmt.Fprintf(os.Stderr, versionText+`
✖️ Error: Version Mismatch
Expand Down
113 changes: 96 additions & 17 deletions cli/cmd/resource_apply_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"context"
"fmt"
"encoding/json"

"github.com/kubeshop/tracetest/cli/pkg/fileutil"
"github.com/kubeshop/tracetest/cli/pkg/resourcemanager"
Expand All @@ -25,6 +26,9 @@ func init() {
resourceType := resourceParams.ResourceName
ctx := context.Background()

// Debug: Print the value of the "gitrepo" flag
fmt.Println("Git Repo Flag:", applyParams.GitRepo)

resourceClient, err := resources.Get(resourceType)
if err != nil {
return "", err
Expand All @@ -34,39 +38,114 @@ func init() {
if err != nil {
return "", err
}

// Check if a definition file is provided
if applyParams.DefinitionFile != "" {
// If a file name is provided, read its contents
inputFile, err := fileutil.Read(applyParams.DefinitionFile)
if err != nil {
return "", fmt.Errorf("cannot read file %s: %w", applyParams.DefinitionFile, err)
}

inputFile, err := fileutil.Read(applyParams.DefinitionFile)
if err != nil {
return "", fmt.Errorf("cannot read file %s: %w", applyParams.DefinitionFile, err)
}
result, err := resourceClient.Apply(ctx, inputFile, resultFormat)
if err != nil {
return "", err
}

result, err := resourceClient.Apply(ctx, inputFile, resultFormat)
if err != nil {
return "", err
return result, nil
} else {
// If no file name is provided, use Git parameters as JSON body
gitParams, err := resourceClient.ApplyWithGitParameters(ctx, resultFormat)
if err != nil {
return "", err
}
// Convert the map to JSON
jsonBody, err := json.Marshal(gitParams)
if err != nil {
return "", fmt.Errorf("error creating JSON body: %w", err)
}

return string(jsonBody), nil
}

return result, nil
}, applyParams),
PostRun: teardownCommand,
}

applyCmd.Flags().StringVarP(&applyParams.DefinitionFile, "file", "f", "", "path to the definition file")
applyCmd.Flags().StringVarP(&applyParams.GitRepo, "gitrepo", "", "", "Git repository name")
applyCmd.Flags().StringVarP(&applyParams.GitUsername, "gitusername", "", "", "Git username")
applyCmd.Flags().StringVarP(&applyParams.GitToken, "gittoken", "", "", "Git token")
applyCmd.Flags().StringVarP(&applyParams.RepoName, "reponame", "", "", "Repository name")
applyCmd.Flags().StringVarP(&applyParams.Branch, "branch", "", "", "Branch name")
applyCmd.Flags().StringVarP(&applyParams.GitFile, "gitfile", "", "", "Git file name")

rootCmd.AddCommand(applyCmd)
}

type applyParameters struct {
DefinitionFile string
GitRepo string
GitUsername string
GitToken string
RepoName string
Branch string
GitFile string
}

func (p applyParameters) Validate(cmd *cobra.Command, args []string) []error {
errors := make([]error, 0)
errors := make([]error, 0)

if p.DefinitionFile == "" {
errors = append(errors, paramError{
Parameter: "file",
Message: "Definition file must be provided",
})
}
if p.DefinitionFile == "" {
gitErrors := p.validateGitParameters()
errors = append(errors, gitErrors...)
}

return errors
return errors
}

func (p applyParameters) validateGitParameters() []error {
gitErrors := make([]error, 0)

// Add specific validation checks for Git parameters
if p.GitRepo == "" {
gitErrors = append(gitErrors, paramError{
Parameter: "git-repo",
Message: "Git repository is required",
})
}
if p.GitUsername == "" {
gitErrors = append(gitErrors, paramError{
Parameter: "gitusername",
Message: "Git username is required",
})
}

if p.GitToken == "" {
gitErrors = append(gitErrors, paramError{
Parameter: "gittoken",
Message: "Git token is required",
})
}

if p.RepoName == "" {
gitErrors = append(gitErrors, paramError{
Parameter: "reponame",
Message: "Repository name is required",
})
}

if p.Branch == "" {
gitErrors = append(gitErrors, paramError{
Parameter: "branch",
Message: "Branch name is required",
})
}

if p.GitFile == "" {
gitErrors = append(gitErrors, paramError{
Parameter: "gitfile",
Message: "Git file name is required",
})
}
return gitErrors
}
47 changes: 46 additions & 1 deletion cli/cmd/resource_get_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package cmd
import (
"context"
"errors"
//"fmt"
//"encoding/json"

"github.com/kubeshop/tracetest/cli/pkg/resourcemanager"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -33,7 +35,37 @@ func init() {
if err != nil {
return "", err
}

// Check if Git parameters are provided
if getParams.GitRepo != "" || getParams.Username != "" || getParams.Token != "" || getParams.RepoName != "" || getParams.Branch != "" || getParams.GitFile != "" {
// Construct Git parameters JSON
gitParams := resourcemanager.GitParams{
GitRepo: getParams.GitRepo,
Username: getParams.Username,
Token: getParams.Token,
RepoName: getParams.RepoName,
Branch: getParams.Branch,
GitFile: getParams.GitFile,
}
// // Marshal GitParams to JSON
// jsonBody, err := json.Marshal(gitParams)
// if err != nil {
// return "", fmt.Errorf("error creating JSON body: %w", err)
// }

// Call the Get method with GitParams
result, err := resourceClient.GetWithGit(ctx, getParams.ResourceID, resultFormat, gitParams)
if errors.Is(err, resourcemanager.ErrNotFound) {
return result, nil
}
if err != nil {
return "", err
}

return result, nil
}

// Call the Get method without GitParams
result, err := resourceClient.Get(ctx, getParams.ResourceID, resultFormat)
if errors.Is(err, resourcemanager.ErrNotFound) {
return result, nil
Expand All @@ -43,16 +75,29 @@ func init() {
}

return result, nil
}, getParams),
}, getParams),
PostRun: teardownCommand,
}

getCmd.Flags().StringVar(&getParams.ResourceID, "id", "", "id of the resource to get")
getCmd.Flags().StringVar(&getParams.GitRepo, "gitrepo", "", "Git repository name")
getCmd.Flags().StringVar(&getParams.Username, "gitusername", "", "Git username")
getCmd.Flags().StringVar(&getParams.Token, "gittoken", "", "Git token")
getCmd.Flags().StringVar(&getParams.RepoName, "reponame", "", "Repository name")
getCmd.Flags().StringVar(&getParams.Branch, "branch", "", "Branch name")
getCmd.Flags().StringVar(&getParams.GitFile, "gitfile", "", "Git file name")

rootCmd.AddCommand(getCmd)
}

type resourceIDParameters struct {
ResourceID string
GitRepo string
Username string
Token string
RepoName string
Branch string
GitFile string
}

func (p resourceIDParameters) Validate(cmd *cobra.Command, args []string) []error {
Expand Down
Loading
Loading