diff --git a/pkg/cmd/history_writer.go b/pkg/cmd/history_writer.go index df98e17f..c132a6f0 100644 --- a/pkg/cmd/history_writer.go +++ b/pkg/cmd/history_writer.go @@ -16,32 +16,33 @@ package cmd import ( "encoding/json" + "errors" "os" ) -// Write writes history to given path -func (w *GardenctlHistoryWriter) Write(historyPath string, m map[string]string) error { - f, err := os.OpenFile(historyPath, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0666) - if err != nil { - return err - } +var tmp string - j, err := json.Marshal(m) +// WriteStringln writes history to given path +func (w *GardenctlHistoryWriter) WriteStringln(historyPath string, i interface{}) error { + f, err := os.OpenFile(historyPath, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0666) if err != nil { return err } - _, err = f.WriteString(string(j) + "\n") - return err -} + switch x := i.(type) { + case map[string]string: + j, err := json.Marshal(x) + if err != nil { + return err + } + tmp = string(j) -// WriteString writes history to given path -func (w *GardenctlHistoryWriter) WriteString(historyPath string, s string) error { - f, err := os.OpenFile(historyPath, os.O_WRONLY|os.O_APPEND|os.O_CREATE, 0666) - if err != nil { - return err + case string: + tmp = x + default: + return errors.New("Invalid type not supported") } - _, err = f.WriteString(s + "\n") + _, err = f.WriteString(tmp + "\n") return err } diff --git a/pkg/cmd/target.go b/pkg/cmd/target.go index 6750bfb2..b5d2a7bd 100644 --- a/pkg/cmd/target.go +++ b/pkg/cmd/target.go @@ -104,7 +104,7 @@ func NewTargetCmd(targetReader TargetReader, targetWriter TargetWriter, configRe checkError(err) } - err := historyWriter.Write(pathHistory, targetInfo) + err := historyWriter.WriteStringln(pathHistory, targetInfo) if err != nil { return err } @@ -275,7 +275,7 @@ func NewTargetCmd(targetReader TargetReader, targetWriter TargetWriter, configRe } } - err := historyWriter.Write(pathHistory, targetInfo) + err := historyWriter.WriteStringln(pathHistory, targetInfo) if err != nil { return err } diff --git a/pkg/cmd/target_history.go b/pkg/cmd/target_history.go index 86c1bf1a..1a7c265b 100644 --- a/pkg/cmd/target_history.go +++ b/pkg/cmd/target_history.go @@ -86,7 +86,7 @@ func NewHistoryCmd(targetWriter TargetWriter, historyWriter HistoryWriter) *cobr kubeconfigPathOutput(&target) - err = historyWriter.WriteString(pathHistory, items.Item) + err = historyWriter.WriteStringln(pathHistory, items.Item) if err != nil { return fmt.Errorf("error write history %s", err) } diff --git a/pkg/cmd/target_test.go b/pkg/cmd/target_test.go index b204e999..d1ab89a8 100644 --- a/pkg/cmd/target_test.go +++ b/pkg/cmd/target_test.go @@ -116,7 +116,7 @@ var _ = Describe("Target command", func() { It("targeting project with correct name", func() { targetReader.EXPECT().ReadTarget(gomock.Any()).Return(target).Times(2) - historyWriter.EXPECT().Write(gomock.Any(), gomock.Any()).Return(nil) + historyWriter.EXPECT().WriteStringln(gomock.Any(), gomock.Any()).Return(nil) target.EXPECT().Stack().Return([]cmd.TargetMeta{ { Kind: cmd.TargetKindGarden, @@ -172,7 +172,7 @@ var _ = Describe("Target command", func() { It("targeting shoot name with multiple matches across projects", func() { targetReader.EXPECT().ReadTarget(gomock.Any()).Return(target) - historyWriter.EXPECT().Write(gomock.Any(), gomock.Any()).Return(nil) + historyWriter.EXPECT().WriteStringln(gomock.Any(), gomock.Any()).Return(nil) target.EXPECT().Stack().Return([]cmd.TargetMeta{ { Kind: cmd.TargetKindGarden, diff --git a/pkg/cmd/types.go b/pkg/cmd/types.go index f27b7193..f462d93d 100644 --- a/pkg/cmd/types.go +++ b/pkg/cmd/types.go @@ -41,8 +41,7 @@ type KubeconfigWriter interface { // HistoryWriter writes history to given path. type HistoryWriter interface { - Write(path string, history map[string]string) error - WriteString(path string, history string) error + WriteStringln(path string, history interface{}) error } // GardenctlTargetReader implements TargetReader. diff --git a/pkg/mock/cmd/history_writer.go b/pkg/mock/cmd/history_writer.go index f047a6e9..0f52de4c 100644 --- a/pkg/mock/cmd/history_writer.go +++ b/pkg/mock/cmd/history_writer.go @@ -33,30 +33,16 @@ func (m *MockHistoryWriter) EXPECT() *MockHistoryWriterMockRecorder { return m.recorder } -// Write mocks base method. -func (m *MockHistoryWriter) Write(arg0 string, arg1 map[string]string) error { +// WriteStringln mocks base method. +func (m *MockHistoryWriter) WriteStringln(arg0 string, arg1 interface{}) error { m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "Write", arg0, arg1) + ret := m.ctrl.Call(m, "WriteStringln", arg0, arg1) ret0, _ := ret[0].(error) return ret0 } -// Write indicates an expected call of Write. -func (mr *MockHistoryWriterMockRecorder) Write(arg0, arg1 interface{}) *gomock.Call { +// WriteStringln indicates an expected call of WriteStringln. +func (mr *MockHistoryWriterMockRecorder) WriteStringln(arg0, arg1 interface{}) *gomock.Call { mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Write", reflect.TypeOf((*MockHistoryWriter)(nil).Write), arg0, arg1) -} - -// WriteString mocks base method. -func (m *MockHistoryWriter) WriteString(arg0, arg1 string) error { - m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "WriteString", arg0, arg1) - ret0, _ := ret[0].(error) - return ret0 -} - -// WriteString indicates an expected call of WriteString. -func (mr *MockHistoryWriterMockRecorder) WriteString(arg0, arg1 interface{}) *gomock.Call { - mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "WriteString", reflect.TypeOf((*MockHistoryWriter)(nil).WriteString), arg0, arg1) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "WriteStringln", reflect.TypeOf((*MockHistoryWriter)(nil).WriteStringln), arg0, arg1) }