<commit_before>package cmd

//#include <stdio.h>
//#include <stdlib.h>
//#include <unistd.h>
//#include "pass.h"
import "C"

import (
	"bytes"
	"encoding/json"
	"errors"
	"fmt"
	"io"
	"io/ioutil"
	"net/http"
	"os"
	"os/user"
	"strings"
	"unsafe"
)

type User struct{}

func getPassword(fd uintptr) string {
	cPasswd := C.GetPassword(C.int(fd))
	defer C.free(unsafe.Pointer(cPasswd))
	return C.GoString(cPasswd)
}

func (c *User) Info() *Info {
	return &Info{
		Name:  "user",
		Usage: "user (create) [args]",
		Desc:  "manage users.",
	}
}

func (c *User) Subcommands() map[string]interface{} {
	return map[string]interface{}{
		"create": &UserCreate{},
	}
}

type UserCreate struct{}

func (c *UserCreate) Info() *Info {
	return &Info{
		Name:  "create",
		Usage: "user create username password",
		Desc:  "creates user.",
	}
}

func (c *UserCreate) Run(context *Context, client Doer) error {
	email := context.Args[0]
	io.WriteString(context.Stdout, "Password: ")
	password := getPassword(os.Stdin.Fd())
	io.WriteString(context.Stdout, "\n")
	if password == "" {
		msg := "You must provide the password!\n"
		io.WriteString(context.Stdout, msg)
		return errors.New(msg)
	}
	b := bytes.NewBufferString(`{"email":"` + email + `", "password":"` + password + `"}`)
	request, err := http.NewRequest("POST", GetUrl("/users"), b)
	if err != nil {
		return err
	}
	_, err = client.Do(request)
	if err != nil {
		return err
	}
	io.WriteString(context.Stdout, fmt.Sprintf(`User "%s" created with success!`+"\n", email))
	return nil
}

type Login struct{}

func (c *Login) Run(context *Context, client Doer) error {
	email := context.Args[0]
	io.WriteString(context.Stdout, "Password: ")
	password := getPassword(os.Stdin.Fd())
	io.WriteString(context.Stdout, "\n")
	if password == "" {
		msg := "You must provide the password!\n"
		io.WriteString(context.Stdout, msg)
		return errors.New(msg)
	}
	b := bytes.NewBufferString(`{"password":"` + password + `"}`)<commit_msg>cmd: some refactoring<commit_after>package cmd

//#include <stdio.h>
//#include <stdlib.h>
//#include <unistd.h>
//#include "pass.h"
import "C"

import (
	"bytes"
	"encoding/json"
	"errors"
	"fmt"
	"io"
	"io/ioutil"
	"net/http"
	"os"
	"os/user"
	"strings"
	"unsafe"
)

type User struct{}

func getPassword(fd uintptr) string {
	cPasswd := C.GetPassword(C.int(fd))
	defer C.free(unsafe.Pointer(cPasswd))
	return C.GoString(cPasswd)
}

func readPassword(out io.Writer, password *string, ) error {
	io.WriteString(out, "Password: ")
	*password = getPassword(os.Stdin.Fd())
	io.WriteString(out, "\n")
	if *password == "" {
		msg := "You must provide the password!\n"
		io.WriteString(out, msg)
		return errors.New(msg)
	}
	return nil
}

func (c *User) Info() *Info {
	return &Info{
		Name:  "user",
		Usage: "user (create) [args]",
		Desc:  "manage users.",
	}
}

func (c *User) Subcommands() map[string]interface{} {
	return map[string]interface{}{
		"create": &UserCreate{},
	}
}

type UserCreate struct{}

func (c *UserCreate) Info() *Info {
	return &Info{
		Name:  "create",
		Usage: "user create username password",
		Desc:  "creates user.",
	}
}

func (c *UserCreate) Run(context *Context, client Doer) error {
	var password string
	email := context.Args[0]
	err := readPassword(context.Stdout, &password)
	if err != nil {
		return err
	}
	b := bytes.NewBufferString(`{"email":"` + email + `", "password":"` + password + `"}`)
	request, err := http.NewRequest("POST", GetUrl("/users"), b)
	if err != nil {
		return err
	}
	_, err = client.Do(request)
	if err != nil {
		return err
	}
	io.WriteString(context.Stdout, fmt.Sprintf(`User "%s" created with success!`+"\n", email))
	return nil
}

type Login struct{}

func (c *Login) Run(context *Context, client Doer) error {
	var password string
	email := context.Args[0]
	err := readPassword(context.Stdout, &password)
	if err != nil {
		return err
	}
	b := bytes.NewBufferString(`{"password":"` + password + `"}`)
{'fraction_non_alphanumeric': 0.18369426751592358, 'fraction_numerical': 0.001019108280254777, 'mean_word_length': 4.227696404793608, 'pattern_counts': {'":': 8, '<': 9, '<?xml version=': 0, '>': 9, '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': '1586743', 'n_tokens_mistral': 1520, 'n_tokens_neox': 1474, 'n_words': 364}