Skip to content
This repository has been archived by the owner on Jul 25, 2022. It is now read-only.

Commit

Permalink
extend history WriteString
Browse files Browse the repository at this point in the history
  • Loading branch information
tedteng committed Jun 29, 2021
1 parent 0fac175 commit 82f980a
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 8 deletions.
15 changes: 13 additions & 2 deletions pkg/cmd/history_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,28 @@ 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
}

_, 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
}
2 changes: 1 addition & 1 deletion pkg/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 9 additions & 3 deletions pkg/cmd/target_history.go
Original file line number Diff line number Diff line change
Expand Up @@ -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\"",
Expand All @@ -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
}
Expand Down Expand Up @@ -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
},
}
Expand Down
1 change: 0 additions & 1 deletion pkg/cmd/target_history_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
var _ = Describe("History", func() {
history := History{
ConfigPath: "A",
Binary: "gardenctl",
Items: []string{"A", "B"},
}

Expand Down
1 change: 1 addition & 0 deletions pkg/cmd/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
3 changes: 2 additions & 1 deletion pkg/internal/history/history.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -126,6 +126,7 @@ func (h *History) Select() *History {
}

h.PromptItem = h.Prompt[i]
h.Item = h.Items[i]
return h
}

Expand Down
14 changes: 14 additions & 0 deletions pkg/mock/cmd/history_writer.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 82f980a

Please sign in to comment.