From 4b34936252e87bf94ccea99168270031bcf2a7a2 Mon Sep 17 00:00:00 2001 From: Connor Edwards <38229097+cedws@users.noreply.github.com> Date: Sat, 8 Apr 2023 19:25:20 +0100 Subject: [PATCH] Move extraction function to eject subcommand --- cmd/eject.go | 40 ++++++++++++++++++++++++++++++++++++++++ cmd/inject.go | 8 +++++++- cmd/root.go | 29 +++++++---------------------- 3 files changed, 54 insertions(+), 23 deletions(-) create mode 100644 cmd/eject.go diff --git a/cmd/eject.go b/cmd/eject.go new file mode 100644 index 0000000..753785f --- /dev/null +++ b/cmd/eject.go @@ -0,0 +1,40 @@ +package cmd + +import ( + "encoding/json" + "fmt" + "os" + + "github.com/cedws/pubkey-extract/keyring" + "github.com/spf13/cobra" +) + +var ejectCmd = &cobra.Command{ + Use: "eject", + Run: func(cmd *cobra.Command, args []string) { + gameData, err := os.ReadFile(binPath) + if err != nil { + fmt.Fprintln(os.Stderr, err) + os.Exit(1) + } + kr, err := keyring.Extract(gameData) + if err != nil { + fmt.Fprintln(os.Stderr, err) + os.Exit(1) + } + + raw, err := kr.MarshalBinary() + if err != nil { + fmt.Fprintln(os.Stderr, err) + os.Exit(1) + } + + enc := json.NewEncoder(os.Stdout) + enc.SetIndent("", " ") + enc.Encode(output{raw, kr}) + }, +} + +func init() { + rootCmd.AddCommand(ejectCmd) +} diff --git a/cmd/inject.go b/cmd/inject.go index 95e824e..b9e52ce 100644 --- a/cmd/inject.go +++ b/cmd/inject.go @@ -37,9 +37,15 @@ var injectCmd = &cobra.Command{ os.Exit(1) } + raw, err := kr.MarshalBinary() + if err != nil { + fmt.Fprintln(os.Stderr, err) + os.Exit(1) + } + enc := json.NewEncoder(os.Stdout) enc.SetIndent("", " ") - enc.Encode(kr) + enc.Encode(output{raw, kr}) }, } diff --git a/cmd/root.go b/cmd/root.go index aa47a40..5131769 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -1,7 +1,6 @@ package cmd import ( - "encoding/json" "fmt" "os" @@ -9,29 +8,15 @@ import ( "github.com/spf13/cobra" ) -var ( - binPath string - pretty bool -) +type output struct { + Raw []byte `json:"raw"` + Decoded *keyring.Keyring `json:"decoded"` +} -var rootCmd = &cobra.Command{ - Use: "pubkey-extract", - Run: func(cmd *cobra.Command, args []string) { - gameData, err := os.ReadFile(binPath) - if err != nil { - fmt.Fprintln(os.Stderr, err) - os.Exit(1) - } - kr, err := keyring.Extract(gameData) - if err != nil { - fmt.Fprintln(os.Stderr, err) - os.Exit(1) - } +var binPath string - enc := json.NewEncoder(os.Stdout) - enc.SetIndent("", " ") - enc.Encode(kr) - }, +var rootCmd = &cobra.Command{ + Use: "ki-keyring", } func init() {