Skip to content

Commit

Permalink
Move extraction function to eject subcommand
Browse files Browse the repository at this point in the history
  • Loading branch information
cedws committed Apr 8, 2023
1 parent 36e82dd commit 4b34936
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 23 deletions.
40 changes: 40 additions & 0 deletions cmd/eject.go
Original file line number Diff line number Diff line change
@@ -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)
}
8 changes: 7 additions & 1 deletion cmd/inject.go
Original file line number Diff line number Diff line change
Expand Up @@ -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})
},
}

Expand Down
29 changes: 7 additions & 22 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,37 +1,22 @@
package cmd

import (
"encoding/json"
"fmt"
"os"

"github.com/cedws/pubkey-extract/keyring"
"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() {
Expand Down

0 comments on commit 4b34936

Please sign in to comment.