<commit_before>package client

import (
	"errors"
	"strings"

	"github.com/keybase/cli"
	"github.com/keybase/client/go/libcmdline"
	"github.com/keybase/client/go/libkb"
	keybase1 "github.com/keybase/client/go/protocol/keybase1"
	"github.com/keybase/go-framed-msgpack-rpc/rpc"
	"golang.org/x/net/context"
)

type CmdTeamEdit struct {
	libkb.Contextified
	Team      keybase1.TeamName
	PrintOnly bool
	Settings  keybase1.TeamSettings
}

func newCmdTeamEdit(cl *libcmdline.CommandLine, g *libkb.GlobalContext) cli.Command {
	return cli.Command{
		Name:         "edit",
		ArgumentHelp: "<team name>",
		Usage:        "Edit team settings.",
		Action: func(c *cli.Context) {
			cmd := NewCmdTeamEditRunner(g)
			cl.ChooseCommand(cmd, "edit", c)
		},
		Flags: []cli.Flag{
			cli.StringFlag{
				Name:  "open",
				Usage: "Change team to be open or closed",
			},
			cli.StringFlag{
				Name:  "join-as",
				Usage: "team role (writer, reader) [required when changing team to open]",
			},
		},
	}
}

func NewCmdTeamEditRunner(g *libkb.GlobalContext) *CmdTeamEdit {
	return &CmdTeamEdit{Contextified: libkb.NewContextified(g)}
}

func (c *CmdTeamEdit) ParseArgv(ctx *cli.Context) error {
	var err error
	c.Team, err = ParseOneTeamNameK1(ctx)
	if err != nil {
		return err
	}

	if ctx.NumFlags() == 0 {
		// Just a team name and no other flags - print current team settings.
		c.PrintOnly = true
		return nil
	}

	sopen := ctx.String("open")
	switch sopen {
	case "true":
		c.Settings.Open = true
	case "false":
		c.Settings.Open = false
	default:
		return errors.New("invalid --open flag, please use true or false")
	}

	if c.Settings.Open {
		srole := ctx.String("join-as")
		if srole == "" {
			return errors.New("team role required via --join-as flag")
		}

		role, ok := keybase1.TeamRoleMap[strings.ToUpper(srole)]
		if !ok {
			return errors.New("invalid team role, please use writer, or reader")
		}

		switch role {
		case keybase1.TeamRole_READER, keybase1.TeamRole_WRITER:
			break
		default:
			return errors.New("invalid team role, please use writer, or reader")
		}

		c.Settings.JoinAs = role
	}

	return nil
}

func (c *CmdTeamEdit) applySettings(cli keybase1.TeamsClient) error {
	dui := c.G().UI.GetTerminalUI()

	details, err := cli.TeamGet(context.Background(), keybase1.TeamGetArg{Name: c.Team.String(), ForceRepoll: false})
	if err != nil {
		return err
	}

	if !details.Settings.Open && !c.Settings.Open {
		dui.Printf("The team is already closed.\n")
		return nil
	}

	if details.Settings.Open && c.Settings.Open && details.Settings.JoinAs == c.Settings.JoinAs {
		dui.Printf("The team is already open with default role: %s\n", strings.ToLower(details.Settings.JoinAs.String()))
		return nil
	}

	arg := keybase1.TeamSetSettingsArg{
		Name:     c.Team.String(),
		Settings: c.Settings,
	}

	err = cli.TeamSetSettings(context.Background(), arg)
	if err != nil {
		return err
	}

	dui.Printf("Team settings were changed.\n")
	return nil
}

func (c *CmdTeamEdit) Run() error {
	protocols := []rpc.Protocol{
		NewTeamsUIProtocol(c.G()),
	}

	if err := RegisterProtocolsWithContext(protocols, c.G()); err != nil {
		return err
	}

	cli, err := GetTeamsClient(c.G())
	if err != nil {
		return err
	}

	if !c.PrintOnly {
		if err := c.applySettings(cli); err != nil {
			return err
		}
	}

	details, err := cli.TeamGet(context.Background(), keybase1.TeamGetArg{Name: c.Team.String(), ForceRepoll: !c.PrintOnly})
	if err != nil {
		return err
	}

	dui := c.G().UI.GetTerminalUI()
	dui.Printf("Current settings of team %q:\n", c.Team.String())
	if details.Settings.Open {
		dui.Printf("  Open:\t\t\ttrue\n")
		dui.Printf("  New member role:\t%s\n", strings.ToLower(details.Settings.JoinAs.String()))
	} else {
		dui.Printf("  Open:\tfalse\n")
	}
<commit_msg>Use Bool flag, remove protocols<commit_after>package client

import (
	"errors"
	"strings"

	"github.com/keybase/cli"
	"github.com/keybase/client/go/libcmdline"
	"github.com/keybase/client/go/libkb"
	keybase1 "github.com/keybase/client/go/protocol/keybase1"
	"golang.org/x/net/context"
)

type CmdTeamEdit struct {
	libkb.Contextified
	Team      keybase1.TeamName
	PrintOnly bool
	Settings  keybase1.TeamSettings
}

func newCmdTeamEdit(cl *libcmdline.CommandLine, g *libkb.GlobalContext) cli.Command {
	return cli.Command{
		Name:         "edit",
		ArgumentHelp: "<team name>",
		Usage:        "Edit team settings.",
		Action: func(c *cli.Context) {
			cmd := NewCmdTeamEditRunner(g)
			cl.ChooseCommand(cmd, "edit", c)
		},
		Flags: []cli.Flag{
			cli.BoolFlag{
				Name:  "open",
				Usage: "Change team to be open or closed",
			},
			cli.StringFlag{
				Name:  "join-as",
				Usage: "team role (writer, reader) [required when changing team to open]",
			},
		},
	}
}

func NewCmdTeamEditRunner(g *libkb.GlobalContext) *CmdTeamEdit {
	return &CmdTeamEdit{Contextified: libkb.NewContextified(g)}
}

func (c *CmdTeamEdit) ParseArgv(ctx *cli.Context) error {
	var err error
	c.Team, err = ParseOneTeamNameK1(ctx)
	if err != nil {
		return err
	}

	if ctx.NumFlags() == 0 {
		// Just a team name and no other flags - print current team settings.
		c.PrintOnly = true
		return nil
	}

	if ctx.Generic("open") == nil {
		return errors.New("--open flag is required")
	}

	c.Settings.Open = ctx.Bool("open")
	if c.Settings.Open {
		srole := ctx.String("join-as")
		if srole == "" {
			return errors.New("team role required via --join-as flag")
		}

		role, ok := keybase1.TeamRoleMap[strings.ToUpper(srole)]
		if !ok {
			return errors.New("invalid team role, please use writer, or reader")
		}

		switch role {
		case keybase1.TeamRole_READER, keybase1.TeamRole_WRITER:
			break
		default:
			return errors.New("invalid team role, please use writer, or reader")
		}

		c.Settings.JoinAs = role
	}

	return nil
}

func (c *CmdTeamEdit) applySettings(cli keybase1.TeamsClient) error {
	dui := c.G().UI.GetTerminalUI()

	details, err := cli.TeamGet(context.Background(), keybase1.TeamGetArg{Name: c.Team.String(), ForceRepoll: false})
	if err != nil {
		return err
	}

	if !details.Settings.Open && !c.Settings.Open {
		dui.Printf("The team is already closed.\n")
		return nil
	}

	if details.Settings.Open && c.Settings.Open && details.Settings.JoinAs == c.Settings.JoinAs {
		dui.Printf("The team is already open with default role: %s\n", strings.ToLower(details.Settings.JoinAs.String()))
		return nil
	}

	arg := keybase1.TeamSetSettingsArg{
		Name:     c.Team.String(),
		Settings: c.Settings,
	}

	err = cli.TeamSetSettings(context.Background(), arg)
	if err != nil {
		return err
	}

	dui.Printf("Team settings were changed.\n")
	return nil
}

func (c *CmdTeamEdit) Run() error {
	cli, err := GetTeamsClient(c.G())
	if err != nil {
		return err
	}

	if !c.PrintOnly {
		if err := c.applySettings(cli); err != nil {
			return err
		}
	}

	details, err := cli.TeamGet(context.Background(), keybase1.TeamGetArg{Name: c.Team.String(), ForceRepoll: !c.PrintOnly})
	if err != nil {
		return err
	}

	dui := c.G().UI.GetTerminalUI()
	dui.Printf("Current settings of team %q:\n", c.Team.String())
	if details.Settings.Open {
		dui.Printf("  Open:\t\t\ttrue\n")
		dui.Printf("  New member role:\t%s\n", strings.ToLower(details.Settings.JoinAs.String()))
	} else {
		dui.Printf("  Open:\tfalse\n")
	}

{'fraction_non_alphanumeric': 0.1458505652054039, 'fraction_numerical': 0.0035842293906810036, 'mean_word_length': 3.6122059758423393, 'pattern_counts': {'":': 2, '<': 5, '<?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': '2613608', 'n_tokens_mistral': 2809, 'n_tokens_neox': 2629, 'n_words': 616}