feat(cli): add max history length
This commit is contained in:
parent
8ed3b0c013
commit
7d4a54b34c
2 changed files with 55 additions and 8 deletions
|
|
@ -11,7 +11,8 @@ import (
|
||||||
|
|
||||||
var historyCmd = &cobra.Command{
|
var historyCmd = &cobra.Command{
|
||||||
Use: "history",
|
Use: "history",
|
||||||
Short: "Zeigt die Befehlshistorie an",
|
Short: "show command history",
|
||||||
|
Long: "Shows history of the last 2000 commands",
|
||||||
Run: func(cmd *cobra.Command, args []string) {
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
dirname, err := os.UserConfigDir()
|
dirname, err := os.UserConfigDir()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/charmbracelet/huh"
|
"github.com/charmbracelet/huh"
|
||||||
|
|
@ -34,23 +35,68 @@ func max(a, b int) int {
|
||||||
return b
|
return b
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const (
|
||||||
|
maxHistoryLines = 2000
|
||||||
|
historyFileName = "sst_history"
|
||||||
|
)
|
||||||
|
|
||||||
|
// func LogToHistory(action, pkg, manager string) error {
|
||||||
|
// dirname, err := os.UserConfigDir()
|
||||||
|
// if err != nil {
|
||||||
|
// return fmt.Errorf("cant obtain config dir: %v", err)
|
||||||
|
// }
|
||||||
|
// historyFile := filepath.Join(dirname, "sst", "sst_history")
|
||||||
|
// f, err := os.OpenFile(historyFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
|
||||||
|
// if err != nil {
|
||||||
|
// return err
|
||||||
|
// }
|
||||||
|
// defer f.Close()
|
||||||
|
|
||||||
|
// timestamp := time.Now().Format("2006-01-02 15:04:05")
|
||||||
|
// logEntry := fmt.Sprintf("%s: action: [%s] - package: [%s] - packagemanager: [%s]\n", timestamp, action, pkg, manager)
|
||||||
|
|
||||||
|
// if _, err := f.WriteString(logEntry); err != nil {
|
||||||
|
// return err
|
||||||
|
// }
|
||||||
|
// return nil
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
|
||||||
func LogToHistory(action, pkg, manager string) error {
|
func LogToHistory(action, pkg, manager string) error {
|
||||||
dirname, err := os.UserConfigDir()
|
dirname, err := os.UserConfigDir()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("cant obtain config dir: %v", err)
|
return fmt.Errorf("cant obtain config dir: %v", err)
|
||||||
}
|
}
|
||||||
historyFile := filepath.Join(dirname, "sst", "sst_history")
|
historyFile := filepath.Join(dirname, "sst", historyFileName)
|
||||||
f, err := os.OpenFile(historyFile, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
|
|
||||||
|
lines, err := readHistoryLines(historyFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer f.Close()
|
|
||||||
|
|
||||||
timestamp := time.Now().Format("2006-01-02 15:04:05")
|
timestamp := time.Now().Format("2006-01-02 15:04:05")
|
||||||
logEntry := fmt.Sprintf("%s: action: [%s] - package: [%s] - packagemanager: [%s]\n", timestamp, action, pkg, manager)
|
newLine := fmt.Sprintf("%s: action: [%s] - package: [%s] - packagemanager: [%s]", timestamp, action, pkg, manager)
|
||||||
|
lines = append(lines, newLine)
|
||||||
|
|
||||||
if _, err := f.WriteString(logEntry); err != nil {
|
if len(lines) > maxHistoryLines {
|
||||||
return err
|
lines = lines[len(lines)-maxHistoryLines:]
|
||||||
}
|
}
|
||||||
return nil
|
|
||||||
|
return writeHistoryLines(historyFile, lines)
|
||||||
|
}
|
||||||
|
|
||||||
|
func readHistoryLines(filePath string) ([]string, error) {
|
||||||
|
content, err := os.ReadFile(filePath)
|
||||||
|
if err != nil {
|
||||||
|
if os.IsNotExist(err) {
|
||||||
|
return []string{}, nil
|
||||||
|
}
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return strings.Split(string(content), "\n"), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func writeHistoryLines(filePath string, lines []string) error {
|
||||||
|
content := strings.Join(lines, "\n")
|
||||||
|
return os.WriteFile(filePath, []byte(content), 0644)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue