<commit_before>package main

import (
	"context"
	"fmt"

	"github.com/urfave/cli"

	"github.com/manifoldco/go-manifold"
	"github.com/manifoldco/manifold-cli/clients"
	"github.com/manifoldco/manifold-cli/config"
	"github.com/manifoldco/manifold-cli/errs"
	"github.com/manifoldco/manifold-cli/generated/marketplace/client"
	resClient "github.com/manifoldco/manifold-cli/generated/marketplace/client/resource"
	"github.com/manifoldco/manifold-cli/generated/marketplace/models"
	"github.com/manifoldco/manifold-cli/prompts"
)

func init() {
	appCmd := cli.Command{
		Name:  "app",
		Usage: "Manages an App in Manifold",
		Subcommands: []cli.Command{
			{
				Name:      "add",
				ArgsUsage: "[label]",
				Usage:     "Add a resource to an app in Manifold.",
				Flags: []cli.Flag{
					appFlag(),
				},
				Action: chain(ensureSession, loadDirPrefs, appAddCmd),
			},
			{
				Name:      "delete",
				ArgsUsage: "[label]",
				Usage:     "Removes a resource from an app in Manifold.",
				Flags: []cli.Flag{
					appFlag(),
				},
				Action: chain(ensureSession, deleteAppCmd),
			},
		},
	}

	cmds = append(cmds, appCmd)
}

func appAddCmd(cliCtx *cli.Context) error {
	ctx := context.Background()

	appName := cliCtx.String("app")
	if appName != "" {
		n := manifold.Name(appName)
		if err := n.Validate(nil); err != nil {
			return errs.NewUsageExitError(cliCtx, errs.ErrInvalidAppName)
		}
	}

	cfg, err := config.Load()
	if err != nil {
		return cli.NewExitError(fmt.Sprintf("Could not load configuration: %s", err), -1)
	}

	marketplaceClient, err := clients.NewMarketplace(cfg)
	if err != nil {
		return cli.NewExitError(fmt.Sprintf("Failed to create Marketplace client: %s", err), -1)
	}

	resource, res, err := getResource(ctx, cliCtx, cfg, marketplaceClient)
	if err != nil {
		return cli.NewExitError(err, -1)
	}

	if appName == "" {
		apps := fetchUniqueAppNames(res)
		_, appName, err = prompts.SelectCreateAppName(apps, appName, false)
		if err != nil {
			return prompts.HandleSelectError(err, "Could not select app")
		}
	}

	err = updateResourceApp(ctx, cfg, resource, marketplaceClient, appName)
	if err != nil {
		return cli.NewExitError(fmt.Sprintf("Failed to add app to resource: %s", err), -1)
	}

	fmt.Printf("Your resource \"%s\" has been added to the app \"%s\"\n", resource.Body.Label, appName)

	return nil
}

func deleteAppCmd(cliCtx *cli.Context) error {
	ctx := context.Background()

	cfg, err := config.Load()
	if err != nil {
		return cli.NewExitError(fmt.Sprintf("Could not load configuration: %s", err), -1)
	}

	marketplaceClient, err := clients.NewMarketplace(cfg)
	if err != nil {
		return cli.NewExitError(fmt.Sprintf("Failed to create Marketplace client: %s", err), -1)
	}

	resource, _, err := getResource(ctx, cliCtx, cfg, marketplaceClient)
	if err != nil {
		return cli.NewExitError(err, -1)
	}

	err = updateResourceApp(ctx, cfg, resource, marketplaceClient, "")
	if err != nil {
		return cli.NewExitError(
			fmt.Sprintf("Failed to remove app from resource \"%s\": %s", resource.Body.Label, err), -1)
	}

	fmt.Printf("Your resource \"%s\" has been removed from the app \"%s\"\n", resource.Body.Label,
		resource.Body.AppName)

	return nil
}

func getResource(ctx context.Context, cliCtx *cli.Context, cfg *config.Config,
	marketplaceClient *client.Marketplace,
) (*models.Resource, []*models.Resource, error) {
	args := cliCtx.Args()

	resourceLabel := ""
	if len(args) > 1 {
		return nil, nil, errs.NewUsageExitError(cliCtx, errs.ErrTooManyArgs)
	}
	if len(args) > 0 {
		resourceLabel = args[0]
		l := manifold.Label(resourceLabel)
		if err := l.Validate(nil); err != nil {
			return nil, nil, errs.NewUsageExitError(cliCtx, errs.ErrInvalidResourceName)
		}
	}

	res, err := clients.FetchResources(ctx, marketplaceClient)
	if err != nil {
		return nil, nil, cli.NewExitError(
			fmt.Sprintf("Failed to fetch the list of provisioned resources: %s", err), -1)
	}

	res, err = filterResourcesWithApp(res)
	if err != nil {
		return nil, nil, err
	}

	resourceIdx, _, err := prompts.SelectResource(res, resourceLabel)
	if err != nil {
		return nil, nil, prompts.HandleSelectError(err, "Could not select Resource")
	}

	return res[resourceIdx], res, nil
}<commit_msg>Don't filter resources with apps for add<commit_after>package main

import (
	"context"
	"fmt"

	"github.com/urfave/cli"

	"github.com/manifoldco/go-manifold"
	"github.com/manifoldco/manifold-cli/clients"
	"github.com/manifoldco/manifold-cli/config"
	"github.com/manifoldco/manifold-cli/errs"
	"github.com/manifoldco/manifold-cli/generated/marketplace/client"
	resClient "github.com/manifoldco/manifold-cli/generated/marketplace/client/resource"
	"github.com/manifoldco/manifold-cli/generated/marketplace/models"
	"github.com/manifoldco/manifold-cli/prompts"
)

func init() {
	appCmd := cli.Command{
		Name:  "app",
		Usage: "Manages an App in Manifold",
		Subcommands: []cli.Command{
			{
				Name:      "add",
				ArgsUsage: "[label]",
				Usage:     "Add a resource to an app in Manifold.",
				Flags: []cli.Flag{
					appFlag(),
				},
				Action: chain(ensureSession, loadDirPrefs, appAddCmd),
			},
			{
				Name:      "delete",
				ArgsUsage: "[label]",
				Usage:     "Removes a resource from an app in Manifold.",
				Flags: []cli.Flag{
					appFlag(),
				},
				Action: chain(ensureSession, deleteAppCmd),
			},
		},
	}

	cmds = append(cmds, appCmd)
}

func appAddCmd(cliCtx *cli.Context) error {
	ctx := context.Background()

	appName := cliCtx.String("app")
	if appName != "" {
		n := manifold.Name(appName)
		if err := n.Validate(nil); err != nil {
			return errs.NewUsageExitError(cliCtx, errs.ErrInvalidAppName)
		}
	}

	cfg, err := config.Load()
	if err != nil {
		return cli.NewExitError(fmt.Sprintf("Could not load configuration: %s", err), -1)
	}

	marketplaceClient, err := clients.NewMarketplace(cfg)
	if err != nil {
		return cli.NewExitError(fmt.Sprintf("Failed to create Marketplace client: %s", err), -1)
	}

	resource, res, err := getResource(ctx, cliCtx, cfg, marketplaceClient, false)
	if err != nil {
		return cli.NewExitError(err, -1)
	}

	if appName == "" {
		apps := fetchUniqueAppNames(res)
		_, appName, err = prompts.SelectCreateAppName(apps, appName, false)
		if err != nil {
			return prompts.HandleSelectError(err, "Could not select app")
		}
	}

	err = updateResourceApp(ctx, cfg, resource, marketplaceClient, appName)
	if err != nil {
		return cli.NewExitError(fmt.Sprintf("Failed to add app to resource: %s", err), -1)
	}

	fmt.Printf("Your resource \"%s\" has been added to the app \"%s\"\n", resource.Body.Label, appName)

	return nil
}

func deleteAppCmd(cliCtx *cli.Context) error {
	ctx := context.Background()

	cfg, err := config.Load()
	if err != nil {
		return cli.NewExitError(fmt.Sprintf("Could not load configuration: %s", err), -1)
	}

	marketplaceClient, err := clients.NewMarketplace(cfg)
	if err != nil {
		return cli.NewExitError(fmt.Sprintf("Failed to create Marketplace client: %s", err), -1)
	}

	resource, _, err := getResource(ctx, cliCtx, cfg, marketplaceClient, true)
	if err != nil {
		return cli.NewExitError(err, -1)
	}

	err = updateResourceApp(ctx, cfg, resource, marketplaceClient, "")
	if err != nil {
		return cli.NewExitError(
			fmt.Sprintf("Failed to remove app from resource \"%s\": %s", resource.Body.Label, err), -1)
	}

	fmt.Printf("Your resource \"%s\" has been removed from the app \"%s\"\n", resource.Body.Label,
		resource.Body.AppName)

	return nil
}

func getResource(ctx context.Context, cliCtx *cli.Context, cfg *config.Config,
	marketplaceClient *client.Marketplace, withAppsOnly bool,
) (*models.Resource, []*models.Resource, error) {
	args := cliCtx.Args()

	resourceLabel := ""
	if len(args) > 1 {
		return nil, nil, errs.NewUsageExitError(cliCtx, errs.ErrTooManyArgs)
	}
	if len(args) > 0 {
		resourceLabel = args[0]
		l := manifold.Label(resourceLabel)
		if err := l.Validate(nil); err != nil {
			return nil, nil, errs.NewUsageExitError(cliCtx, errs.ErrInvalidResourceName)
		}
	}

	res, err := clients.FetchResources(ctx, marketplaceClient)
	if err != nil {
		return nil, nil, cli.NewExitError(
			fmt.Sprintf("Failed to fetch the list of provisioned resources: %s", err), -1)
	}

	if withAppsOnly {
		res, err = filterResourcesWithApp(res)
		if err != nil {
			return nil, nil, err
		}
	}

	resourceIdx, _, err := prompts.SelectResource(res, resourceLabel)
	if err != nil {
		return nil, nil, prompts.HandleSelectError(err, "Could not select Resource")
	}

	return res[resourceIdx], res, nil
}
{'fraction_non_alphanumeric': 0.14616923620101213, 'fraction_numerical': 0.0028245263034012004, 'mean_word_length': 3.9551020408163264, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6087072', 'n_tokens_mistral': 3240, 'n_tokens_neox': 2939, 'n_words': 726}