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

Commit

Permalink
refactor write to writeStringln
Browse files Browse the repository at this point in the history
  • Loading branch information
tedteng committed Jul 1, 2021
1 parent 82f980a commit afa4a7e
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 43 deletions.
33 changes: 17 additions & 16 deletions pkg/cmd/history_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
4 changes: 2 additions & 2 deletions pkg/cmd/target.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/target_history.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/cmd/target_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
3 changes: 1 addition & 2 deletions pkg/cmd/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
26 changes: 6 additions & 20 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 afa4a7e

Please sign in to comment.