2025-10-09 01:01:56 +02:00
|
|
|
package cli
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"fmt"
|
|
|
|
|
|
2025-10-09 01:16:31 +02:00
|
|
|
"edp.buildth.ing/DevFW-CICD/edge-connect-client/internal/domain"
|
2025-10-09 01:01:56 +02:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
|
rootCmd.AddCommand(organizationCmd)
|
|
|
|
|
organizationCmd.AddCommand(createOrganizationCmd, showOrganizationCmd, updateOrganizationCmd, deleteOrganizationCmd)
|
|
|
|
|
|
|
|
|
|
// Flags for create/update
|
|
|
|
|
createOrganizationCmd.Flags().StringVar(&orgAddress, "address", "", "Address of the organization")
|
|
|
|
|
createOrganizationCmd.Flags().StringVar(&orgPhone, "phone", "", "Phone number of the organization")
|
|
|
|
|
|
|
|
|
|
updateOrganizationCmd.Flags().StringVar(&orgAddress, "address", "", "New address for the organization")
|
|
|
|
|
updateOrganizationCmd.Flags().StringVar(&orgPhone, "phone", "", "New phone number for the organization")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var (
|
|
|
|
|
orgAddress string
|
|
|
|
|
orgPhone string
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// organizationCmd represents the parent command for all organization-related actions.
|
|
|
|
|
var organizationCmd = &cobra.Command{
|
|
|
|
|
Use: "organization",
|
|
|
|
|
Short: "Manage organizations",
|
|
|
|
|
Long: `Create, show, update, and delete organizations in Edge Connect.`,
|
|
|
|
|
Aliases: []string{"org"},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var createOrganizationCmd = &cobra.Command{
|
|
|
|
|
Use: "create [name]",
|
|
|
|
|
Short: "Create a new organization",
|
|
|
|
|
Args: cobra.ExactArgs(1),
|
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
|
org := &domain.Organization{
|
|
|
|
|
Name: args[0],
|
|
|
|
|
Address: orgAddress,
|
|
|
|
|
Phone: orgPhone,
|
|
|
|
|
}
|
|
|
|
|
err := services.OrganizationService.Create(context.Background(), org)
|
|
|
|
|
if err != nil {
|
2025-10-09 10:54:14 +02:00
|
|
|
return fmt.Errorf("error creating organization: %w", err)
|
2025-10-09 01:01:56 +02:00
|
|
|
}
|
|
|
|
|
fmt.Printf("Organization '%s' created successfully.\n", args[0])
|
|
|
|
|
return nil
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var showOrganizationCmd = &cobra.Command{
|
|
|
|
|
Use: "show [name]",
|
|
|
|
|
Short: "Show details of an organization",
|
|
|
|
|
Args: cobra.ExactArgs(1),
|
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
|
org, err := services.OrganizationService.Get(context.Background(), args[0])
|
|
|
|
|
if err != nil {
|
2025-10-09 10:54:14 +02:00
|
|
|
return fmt.Errorf("error showing organization: %w", err)
|
2025-10-09 01:01:56 +02:00
|
|
|
}
|
|
|
|
|
fmt.Printf("Organization Details:\n")
|
|
|
|
|
fmt.Printf(" Name: %s\n", org.Name)
|
|
|
|
|
fmt.Printf(" Address: %s\n", org.Address)
|
|
|
|
|
fmt.Printf(" Phone: %s\n", org.Phone)
|
|
|
|
|
return nil
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var updateOrganizationCmd = &cobra.Command{
|
|
|
|
|
Use: "update [name]",
|
|
|
|
|
Short: "Update an organization",
|
|
|
|
|
Args: cobra.ExactArgs(1),
|
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
|
// First, get the current organization to update
|
|
|
|
|
org, err := services.OrganizationService.Get(context.Background(), args[0])
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("could not retrieve organization to update: %w", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Update fields if flags were provided
|
|
|
|
|
if cmd.Flags().Changed("address") {
|
|
|
|
|
org.Address = orgAddress
|
|
|
|
|
}
|
|
|
|
|
if cmd.Flags().Changed("phone") {
|
|
|
|
|
org.Phone = orgPhone
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = services.OrganizationService.Update(context.Background(), org)
|
|
|
|
|
if err != nil {
|
2025-10-09 10:54:14 +02:00
|
|
|
return fmt.Errorf("error updating organization: %w", err)
|
2025-10-09 01:01:56 +02:00
|
|
|
}
|
|
|
|
|
fmt.Printf("Organization '%s' updated successfully.\n", args[0])
|
|
|
|
|
return nil
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var deleteOrganizationCmd = &cobra.Command{
|
|
|
|
|
Use: "delete [name]",
|
|
|
|
|
Short: "Delete an organization",
|
|
|
|
|
Args: cobra.ExactArgs(1),
|
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
|
|
|
|
err := services.OrganizationService.Delete(context.Background(), args[0])
|
|
|
|
|
if err != nil {
|
2025-10-09 10:54:14 +02:00
|
|
|
return fmt.Errorf("error deleting organization: %w", err)
|
2025-10-09 01:01:56 +02:00
|
|
|
}
|
|
|
|
|
fmt.Printf("Organization '%s' deleted successfully.\n", args[0])
|
|
|
|
|
return nil
|
|
|
|
|
},
|
|
|
|
|
}
|