From 82f980a1feb6473cac0681a905570c0b0344cb6c Mon Sep 17 00:00:00 2001 From: Ted Teng Date: Tue, 29 Jun 2021 09:10:14 +0800 Subject: [PATCH] extend history WriteString --- pkg/cmd/history_writer.go | 15 +++++++++++++-- pkg/cmd/root.go | 2 +- pkg/cmd/target_history.go | 12 +++++++++--- pkg/cmd/target_history_test.go | 1 - pkg/cmd/types.go | 1 + pkg/internal/history/history.go | 3 ++- pkg/mock/cmd/history_writer.go | 14 ++++++++++++++ 7 files changed, 40 insertions(+), 8 deletions(-) diff --git a/pkg/cmd/history_writer.go b/pkg/cmd/history_writer.go index 85a7e040..df98e17f 100644 --- a/pkg/cmd/history_writer.go +++ b/pkg/cmd/history_writer.go @@ -20,13 +20,13 @@ import ( ) // Write writes history to given path -func (w *GardenctlHistoryWriter) Write(historyPath string, history map[string]string) error { +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 } - j, err := json.Marshal(history) + j, err := json.Marshal(m) if err != nil { return err } @@ -34,3 +34,14 @@ func (w *GardenctlHistoryWriter) Write(historyPath string, history map[string]st _, err = f.WriteString(string(j) + "\n") return err } + +// 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 + } + + _, err = f.WriteString(s + "\n") + return err +} diff --git a/pkg/cmd/root.go b/pkg/cmd/root.go index c8f0143c..d71f0cab 100644 --- a/pkg/cmd/root.go +++ b/pkg/cmd/root.go @@ -162,7 +162,7 @@ func init() { RootCmd.AddCommand(NewInfoCmd(targetReader, ioStreams)) RootCmd.AddCommand(NewVersionCmd(), NewUpdateCheckCmd()) RootCmd.AddCommand(NewDiagCmd(targetReader, ioStreams)) - RootCmd.AddCommand(NewHistoryCmd(targetWriter)) + RootCmd.AddCommand(NewHistoryCmd(targetWriter, historyWriter)) RootCmd.SuggestionsMinimumDistance = suggestionsMinimumDistance RootCmd.BashCompletionFunction = bashCompletionFunc diff --git a/pkg/cmd/target_history.go b/pkg/cmd/target_history.go index 166f615b..86c1bf1a 100644 --- a/pkg/cmd/target_history.go +++ b/pkg/cmd/target_history.go @@ -32,7 +32,7 @@ const ( ) //NewHistoryCmd use for list/search targting history -func NewHistoryCmd(targetWriter TargetWriter) *cobra.Command { +func NewHistoryCmd(targetWriter TargetWriter, historyWriter HistoryWriter) *cobra.Command { cmd := &cobra.Command{ Use: "history", Short: "List/Search targeting history, e.g. \"gardenctl x\"", @@ -46,9 +46,9 @@ func NewHistoryCmd(targetWriter TargetWriter) *cobra.Command { os.Exit(0) } - item := h.Load().Reverse().Select().PromptItem + items := h.Load().Reverse().Select() - m, err := toMap(item) + m, err := toMap(items.PromptItem) if err != nil { return err } @@ -85,6 +85,12 @@ func NewHistoryCmd(targetWriter TargetWriter) *cobra.Command { } kubeconfigPathOutput(&target) + + err = historyWriter.WriteString(pathHistory, items.Item) + if err != nil { + return fmt.Errorf("error write history %s", err) + } + return nil }, } diff --git a/pkg/cmd/target_history_test.go b/pkg/cmd/target_history_test.go index d03e15df..6e01ce05 100644 --- a/pkg/cmd/target_history_test.go +++ b/pkg/cmd/target_history_test.go @@ -23,7 +23,6 @@ import ( var _ = Describe("History", func() { history := History{ ConfigPath: "A", - Binary: "gardenctl", Items: []string{"A", "B"}, } diff --git a/pkg/cmd/types.go b/pkg/cmd/types.go index d3bc01ce..f27b7193 100644 --- a/pkg/cmd/types.go +++ b/pkg/cmd/types.go @@ -42,6 +42,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 } // GardenctlTargetReader implements TargetReader. diff --git a/pkg/internal/history/history.go b/pkg/internal/history/history.go index d87e32b7..7cf2157a 100644 --- a/pkg/internal/history/history.go +++ b/pkg/internal/history/history.go @@ -30,8 +30,8 @@ import ( //History contains the history path, binary name and history items. type History struct { ConfigPath string - Binary string Items []string + Item string Prompt []PromptItem PromptItem PromptItem } @@ -126,6 +126,7 @@ func (h *History) Select() *History { } h.PromptItem = h.Prompt[i] + h.Item = h.Items[i] return h } diff --git a/pkg/mock/cmd/history_writer.go b/pkg/mock/cmd/history_writer.go index a4601495..f047a6e9 100644 --- a/pkg/mock/cmd/history_writer.go +++ b/pkg/mock/cmd/history_writer.go @@ -46,3 +46,17 @@ func (mr *MockHistoryWriterMockRecorder) Write(arg0, arg1 interface{}) *gomock.C 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) +}