Skip to content

Commit

Permalink
Simplify by removing redundant package names from storage types
Browse files Browse the repository at this point in the history
  • Loading branch information
minamijoyo committed Mar 20, 2022
1 parent e6784c2 commit 61c37ab
Show file tree
Hide file tree
Showing 19 changed files with 173 additions and 151 deletions.
5 changes: 3 additions & 2 deletions command/history_runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/google/go-cmp/cmp/cmpopts"
"github.com/minamijoyo/tfmigrate/config"
"github.com/minamijoyo/tfmigrate/history"
"github.com/minamijoyo/tfmigrate/storage/mock"
)

func TestHistoryRunnerPlan(t *testing.T) {
Expand Down Expand Up @@ -207,7 +208,7 @@ migration "mock" "test4" {
for _, tc := range cases {
t.Run(tc.desc, func(t *testing.T) {
migrationDir := setupMigrationDir(t, tc.migrations)
storage := &history.MockStorageConfig{
storage := &mock.Config{
Data: tc.historyFile,
WriteError: false,
ReadError: false,
Expand Down Expand Up @@ -712,7 +713,7 @@ migration "mock" "test4" {
for _, tc := range cases {
t.Run(tc.desc, func(t *testing.T) {
migrationDir := setupMigrationDir(t, tc.migrations)
storage := &history.MockStorageConfig{
storage := &mock.Config{
Data: tc.historyFile,
WriteError: tc.writeError,
ReadError: tc.readError,
Expand Down
3 changes: 2 additions & 1 deletion command/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"github.com/minamijoyo/tfmigrate/config"
"github.com/minamijoyo/tfmigrate/history"
"github.com/minamijoyo/tfmigrate/storage/mock"
)

func TestListMigrations(t *testing.T) {
Expand Down Expand Up @@ -92,7 +93,7 @@ migration "mock" "test4" {
for _, tc := range cases {
t.Run(tc.desc, func(t *testing.T) {
migrationDir := setupMigrationDir(t, tc.migrations)
storage := &history.MockStorageConfig{
storage := &mock.Config{
Data: tc.historyFile,
WriteError: false,
ReadError: false,
Expand Down
3 changes: 2 additions & 1 deletion config/history_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"testing"

"github.com/minamijoyo/tfmigrate/history"
"github.com/minamijoyo/tfmigrate/storage/local"
)

func TestParseHistoryBlock(t *testing.T) {
Expand All @@ -27,7 +28,7 @@ tfmigrate {
}
`,
want: &history.Config{
Storage: &history.LocalStorageConfig{
Storage: &local.Config{
Path: "tmp/history.json",
},
},
Expand Down
27 changes: 15 additions & 12 deletions config/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ import (

"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/gohcl"
"github.com/minamijoyo/tfmigrate/history"
"github.com/minamijoyo/tfmigrate/storage"
"github.com/minamijoyo/tfmigrate/storage/local"
"github.com/minamijoyo/tfmigrate/storage/mock"
"github.com/minamijoyo/tfmigrate/storage/s3"
)

// StorageBlock represents a block for migration history data store in HCL.
Expand All @@ -22,8 +25,8 @@ type StorageBlock struct {
Remain hcl.Body `hcl:",remain"`
}

// parseStorageBlock parses a storage block and returns a history.StorageConfig.
func parseStorageBlock(b StorageBlock) (history.StorageConfig, error) {
// parseStorageBlock parses a storage block and returns a storage.Config.
func parseStorageBlock(b StorageBlock) (storage.Config, error) {
switch b.Type {
case "mock": // only for testing
return parseMockStorageBlock(b)
Expand All @@ -39,9 +42,9 @@ func parseStorageBlock(b StorageBlock) (history.StorageConfig, error) {
}
}

// parseMockStorageBlock parses a storage block for mock and returns a history.StorageConfig.
func parseMockStorageBlock(b StorageBlock) (history.StorageConfig, error) {
var config history.MockStorageConfig
// parseMockStorageBlock parses a storage block for mock and returns a storage.Config.
func parseMockStorageBlock(b StorageBlock) (storage.Config, error) {
var config mock.Config
diags := gohcl.DecodeBody(b.Remain, nil, &config)
if diags.HasErrors() {
return nil, diags
Expand All @@ -50,9 +53,9 @@ func parseMockStorageBlock(b StorageBlock) (history.StorageConfig, error) {
return &config, nil
}

// parseLocalStorageBlock parses a storage block for local and returns a history.StorageConfig.
func parseLocalStorageBlock(b StorageBlock) (history.StorageConfig, error) {
var config history.LocalStorageConfig
// parseLocalStorageBlock parses a storage block for local and returns a storage.Config.
func parseLocalStorageBlock(b StorageBlock) (storage.Config, error) {
var config local.Config
diags := gohcl.DecodeBody(b.Remain, nil, &config)
if diags.HasErrors() {
return nil, diags
Expand All @@ -61,9 +64,9 @@ func parseLocalStorageBlock(b StorageBlock) (history.StorageConfig, error) {
return &config, nil
}

// parseS3StorageBlock parses a storage block for s3 and returns a history.StorageConfig.
func parseS3StorageBlock(b StorageBlock) (history.StorageConfig, error) {
var config history.S3StorageConfig
// parseS3StorageBlock parses a storage block for s3 and returns a storage.Config.
func parseS3StorageBlock(b StorageBlock) (storage.Config, error) {
var config s3.Config
diags := gohcl.DecodeBody(b.Remain, nil, &config)
if diags.HasErrors() {
return nil, diags
Expand Down
7 changes: 4 additions & 3 deletions config/storage_local_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ import (
"reflect"
"testing"

"github.com/minamijoyo/tfmigrate/history"
"github.com/minamijoyo/tfmigrate/storage"
"github.com/minamijoyo/tfmigrate/storage/local"
)

func TestParseLocalStorageBlock(t *testing.T) {
cases := []struct {
desc string
source string
want history.StorageConfig
want storage.Config
ok bool
}{
{
Expand All @@ -25,7 +26,7 @@ tfmigrate {
}
}
`,
want: &history.LocalStorageConfig{
want: &local.Config{
Path: "tmp/history.json",
},
ok: true,
Expand Down
7 changes: 4 additions & 3 deletions config/storage_mock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ import (
"reflect"
"testing"

"github.com/minamijoyo/tfmigrate/history"
"github.com/minamijoyo/tfmigrate/storage"
"github.com/minamijoyo/tfmigrate/storage/mock"
)

func TestParseMockStorageBlock(t *testing.T) {
cases := []struct {
desc string
source string
want history.StorageConfig
want storage.Config
ok bool
}{
{
Expand All @@ -27,7 +28,7 @@ tfmigrate {
}
}
`,
want: &history.MockStorageConfig{
want: &mock.Config{
Data: "foo",
WriteError: true,
ReadError: false,
Expand Down
9 changes: 5 additions & 4 deletions config/storage_s3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ import (
"reflect"
"testing"

"github.com/minamijoyo/tfmigrate/history"
"github.com/minamijoyo/tfmigrate/storage"
"github.com/minamijoyo/tfmigrate/storage/s3"
)

func TestParseS3StorageBlock(t *testing.T) {
cases := []struct {
desc string
source string
want history.StorageConfig
want storage.Config
ok bool
}{
{
Expand All @@ -26,7 +27,7 @@ tfmigrate {
}
}
`,
want: &history.S3StorageConfig{
want: &s3.Config{
Bucket: "tfmigrate-test",
Key: "tfmigrate/history.json",
},
Expand All @@ -53,7 +54,7 @@ tfmigrate {
}
}
`,
want: &history.S3StorageConfig{
want: &s3.Config{
Bucket: "tfmigrate-test",
Key: "tfmigrate/history.json",
Region: "ap-northeast-1",
Expand Down
7 changes: 4 additions & 3 deletions config/storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ import (
"reflect"
"testing"

"github.com/minamijoyo/tfmigrate/history"
"github.com/minamijoyo/tfmigrate/storage"
"github.com/minamijoyo/tfmigrate/storage/local"
)

func TestParseStorageBlock(t *testing.T) {
cases := []struct {
desc string
source string
want history.StorageConfig
want storage.Config
ok bool
}{
{
Expand All @@ -25,7 +26,7 @@ tfmigrate {
}
}
`,
want: &history.LocalStorageConfig{
want: &local.Config{
Path: "tmp/history.json",
},
ok: true,
Expand Down
5 changes: 3 additions & 2 deletions config/tfmigrate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"testing"

"github.com/minamijoyo/tfmigrate/history"
"github.com/minamijoyo/tfmigrate/storage/local"
)

func TestParseConfigurationFile(t *testing.T) {
Expand All @@ -29,7 +30,7 @@ tfmigrate {
want: &TfmigrateConfig{
MigrationDir: "tfmigrate",
History: &history.Config{
Storage: &history.LocalStorageConfig{
Storage: &local.Config{
Path: "tmp/history.json",
},
},
Expand All @@ -50,7 +51,7 @@ tfmigrate {
want: &TfmigrateConfig{
MigrationDir: ".",
History: &history.Config{
Storage: &history.LocalStorageConfig{
Storage: &local.Config{
Path: "tmp/history.json",
},
},
Expand Down
4 changes: 3 additions & 1 deletion history/config.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package history

import "github.com/minamijoyo/tfmigrate/storage"

// Config is a set of configurations for migration history management.
type Config struct {
// MigrationDir is a path to directory where migration files are stored.
MigrationDir string
// Storage is an interface of factory method for Storage
Storage StorageConfig
Storage storage.Config
}
4 changes: 3 additions & 1 deletion history/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"sort"
"strings"
"time"

"github.com/minamijoyo/tfmigrate/storage"
)

// Controller manages a migration history.
Expand Down Expand Up @@ -81,7 +83,7 @@ func loadMigrationFileNames(dir string) ([]string, error) {

// loadHistory loads a history file from a storage.
// If a given history is not found, create a new one.
func loadHistory(ctx context.Context, c StorageConfig) (*History, error) {
func loadHistory(ctx context.Context, c storage.Config) (*History, error) {
s, err := c.NewStorage()
if err != nil {
return nil, err
Expand Down
20 changes: 11 additions & 9 deletions history/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"time"

"github.com/google/go-cmp/cmp"
"github.com/minamijoyo/tfmigrate/storage"
"github.com/minamijoyo/tfmigrate/storage/mock"
)

func TestLoadMigrationFileNames(t *testing.T) {
Expand Down Expand Up @@ -114,13 +116,13 @@ func TestLoadMigrationFileNames(t *testing.T) {
func TestLoadHistory(t *testing.T) {
cases := []struct {
desc string
config StorageConfig
config storage.Config
want *History
ok bool
}{
{
desc: "simple",
config: &MockStorageConfig{
config: &mock.Config{
Data: `{
"version": 1,
"records": {
Expand Down Expand Up @@ -157,7 +159,7 @@ func TestLoadHistory(t *testing.T) {
},
{
desc: "empty",
config: &MockStorageConfig{
config: &mock.Config{
Data: "",
WriteError: false,
ReadError: false,
Expand All @@ -167,7 +169,7 @@ func TestLoadHistory(t *testing.T) {
},
{
desc: "read error",
config: &MockStorageConfig{
config: &mock.Config{
Data: "",
WriteError: false,
ReadError: true,
Expand All @@ -177,7 +179,7 @@ func TestLoadHistory(t *testing.T) {
},
{
desc: "invalid format",
config: &MockStorageConfig{
config: &mock.Config{
Data: "foo",
WriteError: false,
ReadError: false,
Expand Down Expand Up @@ -209,14 +211,14 @@ func TestLoadHistory(t *testing.T) {
func TestControllerSave(t *testing.T) {
cases := []struct {
desc string
config *MockStorageConfig
config *mock.Config
h *History
want []byte
ok bool
}{
{
desc: "simple",
config: &MockStorageConfig{
config: &mock.Config{
Data: "",
WriteError: false,
ReadError: false,
Expand All @@ -230,7 +232,7 @@ func TestControllerSave(t *testing.T) {
},
{
desc: "write error",
config: &MockStorageConfig{
config: &mock.Config{
Data: "",
WriteError: true,
ReadError: false,
Expand Down Expand Up @@ -261,7 +263,7 @@ func TestControllerSave(t *testing.T) {
}

if tc.ok {
got := []byte(tc.config.s.data)
got := []byte(tc.config.StorageData())
if string(got) != string(tc.want) {
t.Errorf("got: %s, want: %s", string(got), string(tc.want))
}
Expand Down

0 comments on commit 61c37ab

Please sign in to comment.