<commit_before>package org

import (
	"errors"
	"strings"

	"github.com/appcelerator/amp/api/rpc/account"
	"github.com/appcelerator/amp/cli"
	"github.com/spf13/cobra"
	"golang.org/x/net/context"
	"google.golang.org/grpc"
)

// NewOrgRemoveCommand returns a new instance of the remove organization command.
func NewOrgRemoveCommand(c cli.Interface) *cobra.Command {
	return &cobra.Command{
		Use:     "rm ORGANIZATION(S)",
		Short:   "Remove organization",
		Aliases: []string{"remove"},
		PreRunE: cli.AtLeastArgs(1),
		RunE: func(cmd *cobra.Command, args []string) error {
			return removeOrg(c, args)
		},
	}
}

func removeOrg(c cli.Interface, args []string) error {
	var errs []string
	conn := c.ClientConn()
	client := account.NewAccountClient(conn)
	for _, org := range args {
		request := &account.DeleteOrganizationRequest{
			Name: org,
		}
		if _, err := client.DeleteOrganization(context.Background(), request); err != nil {
			errs = append(errs, grpc.ErrorDesc(err))
			continue
		}
		c.Console().Println(org)
	}
	if len(errs) > 0 {
		return errors.New(strings.Join(errs, "\n"))
	}
	return nil
}<commit_msg>org rm will switch user back to logged in account (#1420)<commit_after>package org

import (
	"errors"
	"fmt"
	"strings"

	"github.com/appcelerator/amp/api/rpc/account"
	"github.com/appcelerator/amp/api/auth"
	"github.com/appcelerator/amp/cli"
	"github.com/dgrijalva/jwt-go"
	"github.com/spf13/cobra"
	"golang.org/x/net/context"
	"google.golang.org/grpc"
	"google.golang.org/grpc/metadata"
)

// NewOrgRemoveCommand returns a new instance of the remove organization command.
func NewOrgRemoveCommand(c cli.Interface) *cobra.Command {
	return &cobra.Command{
		Use:     "rm ORGANIZATION(S)",
		Short:   "Remove organization",
		Aliases: []string{"remove"},
		PreRunE: cli.AtLeastArgs(1),
		RunE: func(cmd *cobra.Command, args []string) error {
			return removeOrg(c, args)
		},
	}
}

func removeOrg(c cli.Interface, args []string) error {
	var errs []string
	conn := c.ClientConn()
	client := account.NewAccountClient(conn)

	for _, org := range args {
		requestDelete := &account.DeleteOrganizationRequest{
			Name: org,
		}
		if _, err := client.DeleteOrganization(context.Background(), requestDelete); err != nil {
			errs = append(errs, grpc.ErrorDesc(err))
			continue
		}
		c.Console().Println(org)
		// Check if user is logged in on behalf of an org
		token, err := cli.ReadToken(c.Server())
		if err != nil {
			return err
		}
		pToken, _ := jwt.ParseWithClaims(token, &auth.AuthClaims{}, func(t *jwt.Token) (interface{}, error) {
			return []byte{}, nil
		})
		if claims, ok := pToken.Claims.(*auth.AuthClaims); ok {
			if claims.ActiveOrganization == org {
				// Switch back to the users account
				client := account.NewAccountClient(conn)
				requestSwitch := &account.SwitchRequest{
					Account: claims.AccountName,
				}
				headers := metadata.MD{}
				_, err := client.Switch(context.Background(), requestSwitch, grpc.Header(&headers))
				if err != nil {
					return fmt.Errorf("%s", grpc.ErrorDesc(err))
				}
				if err := cli.SaveToken(headers, c.Server()); err != nil {
					return fmt.Errorf("%s", grpc.ErrorDesc(err))
				}
				c.Console().Printf("Switched back to account: %s\n", claims.AccountName)
			}
		}
	}
	if len(errs) > 0 {
		return errors.New(strings.Join(errs, "\n"))
	}
	return nil
}
{'fraction_non_alphanumeric': 0.1506890353505093, 'fraction_numerical': 0.0035949670461354103, 'mean_word_length': 3.888726207906296, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '2611219', 'n_tokens_mistral': 1318, 'n_tokens_neox': 1187, 'n_words': 260}