<commit_before>package client

import (
	"errors"
	"fmt"
	"io"

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

type kvStoreAPIHandler struct {
	libkb.Contextified
	kvstore  keybase1.KvstoreClient
	selfTeam string
	indent   bool
}

func newKVStoreAPIHandler(g *libkb.GlobalContext, indentOutput bool) *kvStoreAPIHandler {
	return &kvStoreAPIHandler{Contextified: libkb.NewContextified(g), indent: indentOutput}
}

func (t *kvStoreAPIHandler) handle(ctx context.Context, c Call, w io.Writer) error {
	switch c.Params.Version {
	case 0, 1:
		return t.handleV1(ctx, c, w)
	default:
		return ErrInvalidVersion{version: c.Params.Version}
	}
}

const (
	getEntryMethod = "get"
	putEntryMethod = "put"
	listMethod     = "list"
	delEntryMethod = "del"
)

var validKvstoreMethodsV1 = map[string]bool{
	getEntryMethod: true,
	putEntryMethod: true,
	listMethod:     true,
	delEntryMethod: true,
}

func (t *kvStoreAPIHandler) handleV1(ctx context.Context, c Call, w io.Writer) error {
	if !validKvstoreMethodsV1[c.Method] {
		return ErrInvalidMethod{name: c.Method, version: 1}
	}

	kvstore, err := GetKVStoreClient(t.G())
	if err != nil {
		return err
	}
	t.kvstore = kvstore

	config, err := GetConfigClient(t.G())
	if err != nil {
		return err
	}
	status, err := config.GetCurrentStatus(context.Background(), 0)
	if err != nil {
		return err
	}
	username := status.User.Username
	t.selfTeam = fmt.Sprintf("%s,%s", username, username)

	switch c.Method {
	case getEntryMethod:
		return t.getEntry(ctx, c, w)
	case putEntryMethod:
		return t.putEntry(ctx, c, w)
	case listMethod:
		return t.list(ctx, c, w)
	case delEntryMethod:
		return t.deleteEntry(ctx, c, w)
	default:
		return ErrInvalidMethod{name: c.Method, version: 1}
	}
}

type getEntryOptions struct {
	Team      *string `json:"team,omitempty"`
	Namespace string  `json:"namespace"`
	EntryKey  string  `json:"entryKey"`
}

func (a *getEntryOptions) Check() error {
	if len(a.Namespace) == 0 {
		return errors.New("`namespace` field required")
	}
	if len(a.EntryKey) == 0 {<commit_msg>check nil user from status (#24327)<commit_after>package client

import (
	"errors"
	"fmt"
	"io"

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

type kvStoreAPIHandler struct {
	libkb.Contextified
	kvstore  keybase1.KvstoreClient
	selfTeam string
	indent   bool
}

func newKVStoreAPIHandler(g *libkb.GlobalContext, indentOutput bool) *kvStoreAPIHandler {
	return &kvStoreAPIHandler{Contextified: libkb.NewContextified(g), indent: indentOutput}
}

func (t *kvStoreAPIHandler) handle(ctx context.Context, c Call, w io.Writer) error {
	switch c.Params.Version {
	case 0, 1:
		return t.handleV1(ctx, c, w)
	default:
		return ErrInvalidVersion{version: c.Params.Version}
	}
}

const (
	getEntryMethod = "get"
	putEntryMethod = "put"
	listMethod     = "list"
	delEntryMethod = "del"
)

var validKvstoreMethodsV1 = map[string]bool{
	getEntryMethod: true,
	putEntryMethod: true,
	listMethod:     true,
	delEntryMethod: true,
}

func (t *kvStoreAPIHandler) handleV1(ctx context.Context, c Call, w io.Writer) error {
	if !validKvstoreMethodsV1[c.Method] {
		return ErrInvalidMethod{name: c.Method, version: 1}
	}

	kvstore, err := GetKVStoreClient(t.G())
	if err != nil {
		return err
	}
	t.kvstore = kvstore

	config, err := GetConfigClient(t.G())
	if err != nil {
		return err
	}
	status, err := config.GetCurrentStatus(context.Background(), 0)
	if err != nil {
		return err
	} else if !status.LoggedIn || status.User == nil {
		return errors.New("not logged in")
	}

	username := status.User.Username
	t.selfTeam = fmt.Sprintf("%s,%s", username, username)

	switch c.Method {
	case getEntryMethod:
		return t.getEntry(ctx, c, w)
	case putEntryMethod:
		return t.putEntry(ctx, c, w)
	case listMethod:
		return t.list(ctx, c, w)
	case delEntryMethod:
		return t.deleteEntry(ctx, c, w)
	default:
		return ErrInvalidMethod{name: c.Method, version: 1}
	}
}

type getEntryOptions struct {
	Team      *string `json:"team,omitempty"`
	Namespace string  `json:"namespace"`
	EntryKey  string  `json:"entryKey"`
}

func (a *getEntryOptions) Check() error {
	if len(a.Namespace) == 0 {
		return errors.New("`namespace` field required")
	}
	if len(a.EntryKey) == 0 {
{'fraction_non_alphanumeric': 0.12696577243293247, 'fraction_numerical': 0.007169287696577244, 'mean_word_length': 4.474683544303797, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '2992336', 'n_tokens_mistral': 1626, 'n_tokens_neox': 1566, 'n_words': 378}