Skip to content

Commit

Permalink
move cmd package
Browse files Browse the repository at this point in the history
  • Loading branch information
mvrahden committed Sep 11, 2024
1 parent c3079b3 commit 50c9db3
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 13 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ dep:
go mod verify

build: dep
go build -o bin/shamir cli/main.go
go build -a -ldflags="-w -s" -o ./bin/shamir ./cmd/shamir/.

.PHONY: build

Expand Down
7 changes: 0 additions & 7 deletions cli/main.go

This file was deleted.

2 changes: 1 addition & 1 deletion cli/cmd/combine.go → cmd/shamir/combine.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cmd
package main

import (
"bufio"
Expand Down
6 changes: 5 additions & 1 deletion cli/cmd/root.go → cmd/shamir/shamir.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cmd
package main

import (
"fmt"
Expand All @@ -13,6 +13,10 @@ var RootCmd = &cobra.Command{
Use: "shamir",
}

func main() {
Execute()
}

func Execute() {
if err := RootCmd.Execute(); err != nil {
fmt.Println(err)
Expand Down
45 changes: 45 additions & 0 deletions cmd/shamir/shamir_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package main_test

import (
"os"
"testing"

main "github.com/mvrahden/go-shamir/cmd/shamir"
)

func setStdinAndCleanup(t *testing.T) {
t.Helper()
newStdin, err := os.CreateTemp("", "input-1")
if err != nil {
t.Fatalf("failed writing to stdin: %s", err)
}
newStderr, err := os.CreateTemp("", "output-1")
if err != nil {
t.Fatalf("failed writing to stdin: %s", err)
}
oldStdin := os.Stdin
oldStderr := os.Stderr
t.Cleanup(func() {
os.Stdin = oldStdin
os.Stderr = oldStderr
os.Remove(newStdin.Name())
os.Remove(newStderr.Name())
})
os.Stdin = newStdin
os.Stderr = newStderr
}

func Test(t *testing.T) {
os.Args = []string{"cmd", "split", "-p", "4", "-t", "2"}
setStdinAndCleanup(t)

_, err := os.Stdin.Write([]byte(`very very secret`))
if err != nil {
t.Fatalf("failed writing to stdin: %s", err)
}
_, err = os.Stdin.Seek(0, 0)
if err != nil {
t.Fatalf("failed resetting pointer of stdin: %s", err)
}
main.Execute()
}
6 changes: 3 additions & 3 deletions cli/cmd/split.go → cmd/shamir/split.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package cmd
package main

import (
"fmt"
"io/ioutil"
"io"
"os"

"github.com/hashicorp/vault/shamir"
Expand All @@ -28,7 +28,7 @@ func init() {
}

func runSplit(cmd *cobra.Command, args []string) {
secretBuf, err := ioutil.ReadAll(os.Stdin)
secretBuf, err := io.ReadAll(os.Stdin)
if err != nil {
fmt.Fprintf(os.Stderr, "Failed to read stdin: %v\n", err)
os.Exit(1)
Expand Down

0 comments on commit 50c9db3

Please sign in to comment.