fix: logging error distorts ui & clear stale cmds

Signed-off-by: cpendery <cpendery@vt.edu>
This commit is contained in:
cpendery
2023-09-14 19:41:49 -04:00
parent dab1b93553
commit 1f8f5f7561
2 changed files with 18 additions and 1 deletions
+10
View File
@@ -47,3 +47,13 @@ func ReadResult() string {
}
return string(content)
}
func ClearResult() {
path, err := xdg.CacheFile(cacheFilePath)
if err != nil {
slog.Error("failed to load cache file", slog.String("error", err.Error()))
}
if err := os.Truncate(path, 0); err != nil {
slog.Error("failed to clear cache file", slog.String("error", err.Error()))
}
}
+8 -1
View File
@@ -1,6 +1,8 @@
package cmd
import (
"io"
"log/slog"
"os"
tea "github.com/charmbracelet/bubbletea"
@@ -20,6 +22,7 @@ more productive
complete documentation is available at https://github.com/cpendery/clac`,
SilenceUsage: true,
Args: cobra.ArbitraryArgs,
RunE: rootExec,
}
outputCmd bool
@@ -28,7 +31,7 @@ complete documentation is available at https://github.com/cpendery/clac`,
func init() {
rootCmd.Flags().BoolVarP(&outputCmd, "output-cmd", "o", false, "outputs the last cmd from a clac execution")
rootCmd.Flags().BoolVarP(&outputCmd, "logging", "l", false, "enable logging to the `clac.log` file")
rootCmd.Flags().BoolVarP(&enableLogging, "logging", "l", false, "enable logging to the `clac.log` file")
}
func rootExec(cmd *cobra.Command, args []string) error {
@@ -37,12 +40,16 @@ func rootExec(cmd *cobra.Command, args []string) error {
_, err := os.Stdout.WriteString(result)
return err
}
autocomplete.ClearResult()
if enableLogging {
f, err := tea.LogToFile("clac.log", "debug")
if err != nil {
return err
}
defer f.Close()
} else {
slog.SetDefault(slog.New(slog.NewTextHandler(io.Discard, nil)))
}
startingInput := ""