Skip to content

Commit

Permalink
fix error when there is no history file yet
Browse files Browse the repository at this point in the history
  • Loading branch information
danielfoehrKn committed Dec 15, 2021
1 parent 274b655 commit cf39be3
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 8 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,8 @@
/hack/switch/switcher_darwin_amd64
/hack/switch/switcher_darwin_amd64.tar.gz

/hack/switch/switcher_darwin_arm64
/hack/switch/switcher_darwin_arm64.tar.gz

/hack/switch/switcher_linux_amd64
/hack/switch/switcher_linux_amd64.tar.gz
6 changes: 6 additions & 0 deletions pkg/subcommands/history/history.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ func SwitchToHistory(stores []store.KubeconfigStore, config *types.Config, state
return err
}

historyLength := len(history)

idx, err := fuzzyfinder.Find(
history,
func(i int) string {
Expand All @@ -48,6 +50,10 @@ func SwitchToHistory(stores []store.KubeconfigStore, config *types.Config, state
return fmt.Sprintf("%d: %s", len(history)-i-1, *context)
}

if i+1 == historyLength {
return fmt.Sprintf("%d: %s (ns: %s)", 0, *context, *ns)
}

previousContext, _, err := util.ParseHistoryEntry(history[i+1])
if err != nil {
logger.Debugf("failed to parse previous namespace history entry")
Expand Down
24 changes: 16 additions & 8 deletions pkg/subcommands/history/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ func ReadHistory() ([]string, error) {
fileName := os.ExpandEnv(historyFilePath)
file, err := os.Open(fileName)
if err != nil {
if os.IsNotExist(err) {
return nil, fmt.Errorf("no history entries yet - please run `switch` first")
}
return nil, err
}
defer file.Close()
Expand All @@ -46,6 +49,13 @@ func ReadHistory() ([]string, error) {
// AppendToHistory appends the given context: namespace to the history file
func AppendToHistory(context, namespace string) error {
filepath := os.ExpandEnv(historyFilePath)
f, err := os.OpenFile(filepath,
os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return err
}
defer f.Close()

historyEntry := fmt.Sprintf("%s:: %s\n", context, namespace)

lastHistoryEntry, err := getLastLineWithSeek(filepath)
Expand All @@ -54,17 +64,10 @@ func AppendToHistory(context, namespace string) error {
}

// do not entry history entry if previous entry is identical
if strings.Contains(historyEntry, lastHistoryEntry) {
if historyEntry == lastHistoryEntry {
return nil
}

f, err := os.OpenFile(filepath,
os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return err
}
defer f.Close()

if _, err := f.WriteString(historyEntry); err != nil {
return err
}
Expand Down Expand Up @@ -99,6 +102,11 @@ func getLastLineWithSeek(filepath string) (string, error) {
var cursor int64 = 0
stat, _ := fileHandle.Stat()
filesize := stat.Size()

if filesize == 0 {
return "", nil
}

for {
cursor -= 1
if _, err := fileHandle.Seek(cursor, io.SeekEnd); err != nil {
Expand Down

0 comments on commit cf39be3

Please sign in to comment.