Skip to content

Commit 1237019

Browse files
author
Matt Morrison
committed
Refactor file IO into pkg. No functional changes.
1 parent e80b5ae commit 1237019

File tree

5 files changed

+57
-57
lines changed

5 files changed

+57
-57
lines changed

input_test.go

-47
This file was deleted.

main.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"github.com/hashicorp/hcl/hcl/printer"
66
"github.com/hashicorp/hcl2/hclwrite"
7+
"github.com/sl1pm4t/k2tf/pkg/file_io"
78
"github.com/sl1pm4t/k2tf/pkg/tfkschema"
89
flag "github.com/spf13/pflag"
910
"os"
@@ -57,11 +58,11 @@ func main() {
5758
Str("builddate", date).
5859
Msg("starting k2tf")
5960

60-
objs := readInput()
61+
objs := file_io.ReadInput(input)
6162

6263
log.Debug().Msgf("read %d objects from input", len(objs))
6364

64-
w, closer := setupOutput()
65+
w, closer := file_io.SetupOutput(output, overwriteExisting)
6566
defer closer()
6667

6768
for i, obj := range objs {

input.go pkg/file_io/input.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package main
1+
package file_io
22

33
import (
44
"bufio"
@@ -15,14 +15,14 @@ import (
1515
"k8s.io/apimachinery/pkg/runtime"
1616
)
1717

18-
func readInput() []runtime.Object {
18+
func ReadInput(input string) []runtime.Object {
1919
if input == "-" || input == "" {
20-
return readStdinInput()
20+
return readStdinInput(input)
2121
}
22-
return readFilesInput()
22+
return readFilesInput(input)
2323
}
2424

25-
func readStdinInput() []runtime.Object {
25+
func readStdinInput(input string) []runtime.Object {
2626
var objs []runtime.Object
2727

2828
info, err := os.Stdin.Stat()
@@ -63,7 +63,7 @@ func readStdinInput() []runtime.Object {
6363
return objs
6464
}
6565

66-
func readFilesInput() []runtime.Object {
66+
func readFilesInput(input string) []runtime.Object {
6767
var objs []runtime.Object
6868

6969
if _, err := os.Stat(input); os.IsNotExist(err) {

pkg/file_io/input_test.go

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package file_io
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func Test_readFilesInput(t *testing.T) {
8+
tests := []struct {
9+
name string
10+
input string
11+
wantObjCount int
12+
}{
13+
{
14+
"../../test-fixtures/service.yaml",
15+
"../../test-fixtures/service.yaml",
16+
1,
17+
},
18+
{
19+
"../../test-fixtures",
20+
"../../test-fixtures",
21+
25,
22+
},
23+
{
24+
"../../test-fixtures/",
25+
"../../test-fixtures/",
26+
25,
27+
},
28+
{
29+
"../../test-fixtures/nested/server-clusterrole.yaml",
30+
"../../test-fixtures/nested/server-clusterrole.yaml",
31+
1,
32+
},
33+
{
34+
"../../test-fixtures/nested/",
35+
"../../test-fixtures/nested/",
36+
4,
37+
},
38+
}
39+
for _, tt := range tests {
40+
t.Run(tt.name, func(t *testing.T) {
41+
if got := readFilesInput(tt.input); len(got) != tt.wantObjCount {
42+
t.Errorf("readFilesInput() object Count = %d, want %d", len(got), tt.wantObjCount)
43+
}
44+
})
45+
}
46+
}

output.go pkg/file_io/output.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package main
1+
package file_io
22

33
import (
44
"io"
@@ -11,7 +11,7 @@ var noOpCloser = func() {}
1111

1212
type CloseFunc func()
1313

14-
func setupOutput() (io.Writer, CloseFunc) {
14+
func SetupOutput(output string, overwriteExisting bool) (io.Writer, CloseFunc) {
1515
var closeFn CloseFunc
1616
var w io.Writer
1717

0 commit comments

Comments
 (0)