feat(cli): add max history length
This commit is contained in:
parent
8ed3b0c013
commit
7d4a54b34c
2 changed files with 55 additions and 8 deletions
|
|
@ -4,6 +4,7 @@ import (
|
|||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/charmbracelet/huh"
|
||||
|
|
@ -34,23 +35,68 @@ func max(a, b int) int {
|
|||
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 {
|
||||
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)
|
||||
historyFile := filepath.Join(dirname, "sst", historyFileName)
|
||||
|
||||
lines, err := readHistoryLines(historyFile)
|
||||
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)
|
||||
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 {
|
||||
return err
|
||||
if len(lines) > maxHistoryLines {
|
||||
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