@@ -768,23 +768,67 @@ func readOrganizationsFromCSV(filePath string) ([]string, error) {
768768func getOrganizations (enterprise , orgListPath string ) ([]string , error ) {
769769 if orgListPath != "" {
770770 pterm .Info .Printf ("Reading organizations from CSV file: %s\n " , orgListPath )
771- orgs , err := readOrganizationsFromCSV (orgListPath )
771+ csvOrgs , err := readOrganizationsFromCSV (orgListPath )
772772 if err != nil {
773773 return nil , err
774774 }
775- if len (orgs ) == 0 {
775+ if len (csvOrgs ) == 0 {
776776 return nil , fmt .Errorf ("no valid organizations found in CSV file" )
777777 }
778- pterm .Success .Printf ("Found %d organizations in CSV file\n " , len (orgs ))
779-
780- // Show the list of organizations that will be targeted
781- pterm .Info .Println ("Organizations to be targeted:" )
782- for _ , org := range orgs {
783- pterm .Printf (" - %s\n " , org )
778+ pterm .Success .Printf ("Found %d organizations in CSV file\n " , len (csvOrgs ))
779+
780+ // Fetch all organizations from enterprise to validate against
781+ pterm .Info .Println ("Fetching organizations from enterprise to validate CSV list..." )
782+ enterpriseOrgs , err := fetchOrganizations (enterprise )
783+ if err != nil {
784+ return nil , fmt .Errorf ("failed to fetch enterprise organizations for validation: %w" , err )
785+ }
786+ pterm .Success .Printf ("Found %d organizations in enterprise '%s'\n " , len (enterpriseOrgs ), enterprise )
787+
788+ // Create a map for faster lookup
789+ enterpriseOrgMap := make (map [string ]bool )
790+ for _ , org := range enterpriseOrgs {
791+ enterpriseOrgMap [org ] = true
792+ }
793+
794+ // Validate CSV organizations against enterprise organizations
795+ var validOrgs []string
796+ var invalidOrgs []string
797+
798+ for _ , org := range csvOrgs {
799+ if enterpriseOrgMap [org ] {
800+ validOrgs = append (validOrgs , org )
801+ } else {
802+ invalidOrgs = append (invalidOrgs , org )
803+ }
804+ }
805+
806+ // Warn about invalid organizations
807+ if len (invalidOrgs ) > 0 {
808+ pterm .Warning .Printf ("Found %d organizations in CSV that do not exist in enterprise '%s':\n " , len (invalidOrgs ), enterprise )
809+ for _ , org := range invalidOrgs {
810+ pterm .Printf (" - %s (not found in enterprise)\n " , pterm .Red (org ))
811+ }
812+ pterm .Println ()
813+ }
814+
815+ // Check if we have any valid organizations left
816+ if len (validOrgs ) == 0 {
817+ return nil , fmt .Errorf ("no valid organizations found in CSV file that exist in enterprise '%s'" , enterprise )
818+ }
819+
820+ if len (invalidOrgs ) > 0 {
821+ pterm .Info .Printf ("Proceeding with %d valid organizations (skipping %d invalid)\n " , len (validOrgs ), len (invalidOrgs ))
822+ }
823+
824+ // Show the list of valid organizations that will be targeted
825+ pterm .Info .Println ("Valid organizations to be targeted:" )
826+ for _ , org := range validOrgs {
827+ pterm .Printf (" - %s\n " , pterm .Green (org ))
784828 }
785829 pterm .Println ()
786-
787- return orgs , nil
830+
831+ return validOrgs , nil
788832 }
789833
790834 // Use existing enterprise API fetching
0 commit comments