<commit_before>package cmd

import (
	"fmt"
	"os"

	"github.com/howeyc/gopass"
	"github.com/spf13/cobra"

	"github.com/jpki/myna/libmyna"
)

var pinCmd = &cobra.Command{
	Use:   "pin",
	Short: "PIN関連操作",
	Long: `PIN関連操作
`,
}

var pinStatusCmd = &cobra.Command{
	Use:   "status",
	Short: "PINステータス",
	Long:  `PINステータスを確認します`,
	Run:   pinStatus,
}

func pinStatus(cmd *cobra.Command, args []string) {
	status, err := libmyna.GetPinStatus(&ctx)
	if err != nil {
		fmt.Fprintf(os.Stderr, "エラー: %s\n", err)
		os.Exit(1)
	}

	fmt.Printf("券面事項PIN(A):\tのこり%2d回\n",
		status["card_info_pin_a"])
	fmt.Printf("券面事項PIN(B):\tのこり%2d回\n",
		status["card_info_pin_b"])
	fmt.Printf("入力補助PIN:\tのこり%2d回\n",
		status["card_input_helper_pin"])
	fmt.Printf("入力補助PIN(A):\tのこり%2d回\n",
		status["card_input_helper_pin_a"])
	fmt.Printf("入力補助PIN(B):\tのこり%2d回\n",
		status["card_input_helper_pin_b"])
	fmt.Printf("JPKI認証用PIN:\tのこり%2d回\n", status["jpki_auth"])
	fmt.Printf("JPKI署名用PIN:\tのこり%2d回\n", status["jpki_sign"])
	/*
		fmt.Printf("謎のPIN1:\tのこり%d回\n", status["unknown1"])
		fmt.Printf("謎のPIN2:\tのこり%d回\n", status["unknown2"])
	*/
}

var pinChangeCmd = &cobra.Command{
	Use:   "change",
	Short: "各種PINを変更",
	Long:  `各種PINを変更します`,
}

var pinChangeCardCmd = &cobra.Command{
	Use:   "card",
	Short: "券面入力補助用PINを変更",
	Long:  `券面入力補助用PINを変更します`,
	Run:   pinChangeCard,
}

func pinChangeCard(cmd *cobra.Command, args []string) {
	pinName := "券面入力補助用PIN(4桁)"
	pin, _ := cmd.Flags().GetString("pin")
	if pin == "" {
		fmt.Printf("現在の%s: ", pinName)
		input, err := gopass.GetPasswdMasked()
		if err != nil {
			return
		}
		pin = string(input)
	}
	newpin, _ := cmd.Flags().GetString("newpin")
	if newpin == "" {
		fmt.Printf("新しい%s: ", pinName)
		input, err := gopass.GetPasswdMasked()
		if err != nil {
			return
		}
		newpin = string(input)
	}
	err := libmyna.ChangeCardInputHelperPin(&ctx, pin, newpin)
	if err != nil {
		fmt.Fprintf(os.Stderr, "エラー: %s", err)
		return
	}
	fmt.Printf("%sを変更しました", pinName)
}

var pinChangeJPKIAuthCmd = &cobra.Command{
	Use:   "auth",
	Short: "JPKI認証用PINを変更",
	Long:  `JPKI認証用PINを変更します`,
	Run:   pinChangeJPKIAuth,
}

func pinChangeJPKIAuth(cmd *cobra.Command, args []string) {
	pinName := "JPKI認証用パスワード"
	pin, _ := cmd.Flags().GetString("pin")
	if pin == "" {
		fmt.Printf("現在の%s: ", pinName)
		input, err := gopass.GetPasswdMasked()
		if err != nil {
			return
		}
		pin = string(input)
	}
	newpin, _ := cmd.Flags().GetString("newpin")
	if newpin == "" {
		fmt.Printf("新しい%s: ", pinName)
		input, err := gopass.GetPasswdMasked()
		if err != nil {
			return
		}
		newpin = string(input)
	}
	err := libmyna.ChangeJPKIAuthPin(&ctx, pin, newpin)
	if err != nil {
		fmt.Fprintf(os.Stderr, "エラー: %s", err)
		return
	}
	fmt.Printf("%sを変更しました", pinName)
}

var pinChangeJPKISignCmd = &cobra.Command{
	Use:   "sign",
	Short: "JPKI署名用パスワードを変更",
	Long:  `JPKI署名用パスワードを変更します`,
	Run:   pinChangeJPKISign,
}

func pinChangeJPKISign(cmd *cobra.Command, args []string) {
	pinName := "JPKI署名用パスワード"
	pin, _ := cmd.Flags().GetString("pin")
	if pin == "" {
		fmt.Printf("現在の%s: ", pinName)
		input, err := gopass.GetPasswdMasked()
		if err != nil {
			return
		}
		pin = string(input)
	}
	newpin, _ := cmd.Flags().GetString("newpin")
	if newpin == "" {
		fmt.Printf("新しい%s: ", pinName)
		input, err := gopass.GetPasswdMasked()
		if err != nil {
			return
		}
		newpin = string(input)
	}
	err := libmyna.ChangeJPKISignPin(&ctx, pin, newpin)
	if err != nil {
		fmt.Fprintf(os.Stderr, "エラー: %s", err)
		return
	}
	fmt.Printf("%sを変更しました", pinName)
}

func init() {
	pinCmd.AddCommand(pinStatusCmd)
	pinCmd.AddCommand(pinChangeCmd)

	pinChangeCardCmd.Flags().String("pin", "", "現在の暗証番号(4桁)")
	pinChangeCardCmd.Flags().String("newpin", "", "新しい暗証番号(4桁)")
	pinChangeCmd.AddCommand(pinChangeCardCmd)<commit_msg>add error handling<commit_after>package cmd

import (
	"fmt"
	"os"

	"github.com/howeyc/gopass"
	"github.com/spf13/cobra"

	"github.com/jpki/myna/libmyna"
)

var pinCmd = &cobra.Command{
	Use:   "pin",
	Short: "PIN関連操作",
	Long: `PIN関連操作
`,
}

var pinStatusCmd = &cobra.Command{
	Use:   "status",
	Short: "PINステータスを表示",
	RunE:  pinStatus,
}

func pinStatus(cmd *cobra.Command, args []string) error {
	status, err := libmyna.GetPinStatus(&ctx)
	if err != nil {
		return err
	}

	fmt.Printf("券面事項PIN(A):\tのこり%2d回\n",
		status["card_info_pin_a"])
	fmt.Printf("券面事項PIN(B):\tのこり%2d回\n",
		status["card_info_pin_b"])
	fmt.Printf("入力補助PIN:\tのこり%2d回\n",
		status["card_input_helper_pin"])
	fmt.Printf("入力補助PIN(A):\tのこり%2d回\n",
		status["card_input_helper_pin_a"])
	fmt.Printf("入力補助PIN(B):\tのこり%2d回\n",
		status["card_input_helper_pin_b"])
	fmt.Printf("JPKI認証用PIN:\tのこり%2d回\n", status["jpki_auth"])
	fmt.Printf("JPKI署名用PIN:\tのこり%2d回\n", status["jpki_sign"])
	/*
		fmt.Printf("謎のPIN1:\tのこり%d回\n", status["unknown1"])
		fmt.Printf("謎のPIN2:\tのこり%d回\n", status["unknown2"])
	*/
	return nil
}

var pinChangeCmd = &cobra.Command{
	Use:   "change",
	Short: "各種PINを変更",
}

var pinChangeCardCmd = &cobra.Command{
	Use:   "card",
	Short: "券面入力補助用PINを変更",
	RunE:  pinChangeCard,
}

func pinChangeCard(cmd *cobra.Command, args []string) error {
	pinName := "券面入力補助用PIN(4桁)"
	pin, _ := cmd.Flags().GetString("pin")
	if pin == "" {
		fmt.Printf("現在の%s: ", pinName)
		input, err := gopass.GetPasswdMasked()
		if err != nil {
			return nil
		}
		pin = string(input)
	}
	newpin, _ := cmd.Flags().GetString("newpin")
	if newpin == "" {
		fmt.Printf("新しい%s: ", pinName)
		input, err := gopass.GetPasswdMasked()
		if err != nil {
			return nil
		}
		newpin = string(input)
	}
	err := libmyna.ChangeCardInputHelperPin(&ctx, pin, newpin)
	if err != nil {
		return err
	}
	fmt.Printf("%sを変更しました", pinName)
	return nil
}

var pinChangeJPKIAuthCmd = &cobra.Command{
	Use:   "auth",
	Short: "JPKI認証用PINを変更",
	Long:  `JPKI認証用PINを変更します`,
	RunE:  pinChangeJPKIAuth,
}

func pinChangeJPKIAuth(cmd *cobra.Command, args []string) error {
	pinName := "JPKI認証用パスワード"
	pin, _ := cmd.Flags().GetString("pin")
	if pin == "" {
		fmt.Printf("現在の%s: ", pinName)
		input, err := gopass.GetPasswdMasked()
		if err != nil {
			return nil
		}
		pin = string(input)
	}
	newpin, _ := cmd.Flags().GetString("newpin")
	if newpin == "" {
		fmt.Printf("新しい%s: ", pinName)
		input, err := gopass.GetPasswdMasked()
		if err != nil {
			return nil
		}
		newpin = string(input)
	}
	err := libmyna.ChangeJPKIAuthPin(&ctx, pin, newpin)
	if err != nil {
		return err
	}
	fmt.Printf("%sを変更しました", pinName)
	return nil
}

var pinChangeJPKISignCmd = &cobra.Command{
	Use:   "sign",
	Short: "JPKI署名用パスワードを変更",
	Long:  `JPKI署名用パスワードを変更します`,
	RunE:  pinChangeJPKISign,
}

func pinChangeJPKISign(cmd *cobra.Command, args []string) error {
	pinName := "JPKI署名用パスワード"
	pin, _ := cmd.Flags().GetString("pin")
	if pin == "" {
		fmt.Printf("現在の%s: ", pinName)
		input, err := gopass.GetPasswdMasked()
		if err != nil {
			return nil
		}
		pin = string(input)
	}
	newpin, _ := cmd.Flags().GetString("newpin")
	if newpin == "" {
		fmt.Printf("新しい%s: ", pinName)
		input, err := gopass.GetPasswdMasked()
		if err != nil {
			return nil
		}
		newpin = string(input)
	}
	err := libmyna.ChangeJPKISignPin(&ctx, pin, newpin)
	if err != nil {
		fmt.Fprintf(os.Stderr, "エラー: %s", err)
		return err
	}
	fmt.Printf("%sを変更しました", pinName)
	return nil
}

func init() {
	pinCmd.AddCommand(pinStatusCmd)
	pinCmd.AddCommand(pinChangeCmd)

	pinChangeCardCmd.Flags().String("pin", "", "現在の暗証番号(4桁)")
	pinChangeCardCmd.Flags().String("newpin", "", "新しい暗証番号(4桁)")
	pinChangeCmd.AddCommand(pinChangeCardCmd)
{'fraction_non_alphanumeric': 0.1824973319103522, 'fraction_numerical': 0.004402347918890074, 'mean_word_length': 4.3511777301927195, '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': '5640451', 'n_tokens_mistral': 3642, 'n_tokens_neox': 3590, 'n_words': 537}