func NewCreateGroupsCmd()

in tools/mconnect/commands/groups/groups.go [171:225]


func NewCreateGroupsCmd(factory groupCreatorFactory) *cobra.Command {
	return &cobra.Command{
		Use:   "create-groups path project region",
		Short: "Creates a group for each CAST application in Migration Center and adds the 'mconnect' label to it.",
		Long:  `Creates a group for each CAST application in Migration Center and adds the 'mconnect' label to it.`,
		Example: `	
		mconnect create-groups --path=path/to/cast/analysisResults.csv --project=my-mc-project-id --region=my-region1
		mconnect create-groups --path=path/to/cast/analysisResults.csv --project=my-mc-project-id --region=my-region1 --ignore-existing-groups=true`,
		RunE: func(cmd *cobra.Command, args []string) error {
			ctx := context.Background()

			if len(args) != 0 {
				return messages.NoArgumentsAcceptedError{Args: args}.Error()
			}

			// Remove usage printing on error once initial parameter validations are done.
			cmd.SilenceUsage = true

			fmt.Println(messages.ParsingFile{FilePath: path})
			groups, err := parser.Groups(path)
			if err != nil {
				return fmt.Errorf("failed parsing, err: %v", err)
			}
			fmt.Println(messages.GroupsApps{Applications: len(groups)})

			location := root.DefaultLocation
			if region != "" {
				location = region
			}

			fmt.Println(messages.GroupsCreation)
			opts := []option.ClientOption{option.WithUserAgent(messages.GroupsUserAgent)}
			if endpoint != "" {
				opts = append(opts, option.WithEndpoint(endpoint))
			}

			client, err := migrationcenter.NewClient(ctx, opts...)
			if err != nil {
				return err
			}
			defer client.Close()

			pal := mcutil.ProjectAndLocation{ProjectID: projectID, Location: location}
			gc := factory.build(pal, client)
			err = gc.create(ctx, groups, ignoreExistingGroups)
			if err != nil {
				return fmt.Errorf("failed creating groups, err: %v", err)
			}

			fmt.Println(messages.GroupsSuccess)
			fmt.Println(messages.GroupsNextSteps{Path: path, ProjectID: projectID, Region: location})
			return nil
		},
	}
}