Skip to content

Commit

Permalink
fix: Use log library
Browse files Browse the repository at this point in the history
  • Loading branch information
adityachoudhari26 committed Nov 25, 2024
1 parent 688ad08 commit cebc06b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package jobtoresource
import (
"fmt"

"github.com/charmbracelet/log"

"github.com/MakeNowJust/heredoc/v2"
"github.com/ctrlplanedev/cli/internal/api"
"github.com/ctrlplanedev/cli/internal/cliutil"
Expand All @@ -25,11 +27,13 @@ func NewCreateRelationshipCmd() *cobra.Command {
`),
PreRunE: func(cmd *cobra.Command, args []string) error {
if jobId == "" {
return fmt.Errorf("job-id is required")
log.Error("job is required")
return fmt.Errorf("job is required")
}

if resourceIdentifier == "" {
return fmt.Errorf("resource-identifier is required")
log.Error("resource is required")
return fmt.Errorf("resource is required")
}

return nil
Expand All @@ -40,20 +44,23 @@ func NewCreateRelationshipCmd() *cobra.Command {

client, err := api.NewAPIKeyClientWithResponses(apiURL, apiKey)
if err != nil {
log.Error("failed to create relationship API client", "error", err)
return fmt.Errorf("failed to create relationship API client: %w", err)
}

jobIdUUID, err := uuid.Parse(jobId)
if err != nil {
return fmt.Errorf("failed to parse job-id: %w", err)
log.Error("failed to parse job id", "error", err)
return fmt.Errorf("failed to parse job id: %w", err)
}

resp, err := client.CreateJobToResourceRelationship(cmd.Context(), api.CreateJobToResourceRelationshipJSONRequestBody{
JobId: jobIdUUID,
ResourceIdentifier: resourceIdentifier,
})
if err != nil {
return fmt.Errorf("failed to create job-to-resource relationship: %w", err)
log.Error("failed to create job to resource relationship", "error", err)
return fmt.Errorf("failed to create job to resource relationship: %w", err)
}

return cliutil.HandleOutput(cmd, resp)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"

"github.com/MakeNowJust/heredoc/v2"
"github.com/charmbracelet/log"
"github.com/ctrlplanedev/cli/internal/api"
"github.com/ctrlplanedev/cli/internal/cliutil"
"github.com/google/uuid"
Expand All @@ -27,26 +28,32 @@ func NewCreateRelationshipCmd() *cobra.Command {
`),
PreRunE: func(cmd *cobra.Command, args []string) error {
if workspaceId == "" {
log.Error("workspace is required")
return fmt.Errorf("workspace-id is required")
}

if fromIdentifier == "" {
log.Error("from is required")
return fmt.Errorf("from is required")
}

if toIdentifier == "" {
log.Error("to is required")
return fmt.Errorf("to is required")
}

if relationshipType == "" {
log.Error("type is required")
return fmt.Errorf("type is required")
}

if relationshipType != "associated_with" && relationshipType != "depends_on" {
log.Error("type must be either 'associated_with' or 'depends_on', got %s", relationshipType)
return fmt.Errorf("type must be either 'associated_with' or 'depends_on', got %s", relationshipType)
}

if fromIdentifier == toIdentifier {
log.Error("from and to cannot be the same")
return fmt.Errorf("from and to cannot be the same")
}

Expand All @@ -58,12 +65,14 @@ func NewCreateRelationshipCmd() *cobra.Command {

client, err := api.NewAPIKeyClientWithResponses(apiURL, apiKey)
if err != nil {
log.Error("failed to create relationship API client", "error", err)
return fmt.Errorf("failed to create relationship API client: %w", err)
}

workspaceIdUUID, err := uuid.Parse(workspaceId)
if err != nil {
return fmt.Errorf("failed to parse workspace-id: %w", err)
log.Error("failed to parse workspace id", "error", err)
return fmt.Errorf("failed to parse workspace id: %w", err)
}

resp, err := client.CreateResourceToResourceRelationship(cmd.Context(), api.CreateResourceToResourceRelationshipJSONRequestBody{
Expand All @@ -73,7 +82,8 @@ func NewCreateRelationshipCmd() *cobra.Command {
Type: relationshipType,
})
if err != nil {
return fmt.Errorf("failed to create resource-to-resource relationship: %w", err)
log.Error("failed to create resource to resource relationship", "error", err)
return fmt.Errorf("failed to create resource to resource relationship: %w", err)
}

return cliutil.HandleOutput(cmd, resp)
Expand Down

0 comments on commit cebc06b

Please sign in to comment.