Skip to content

Commit 029cdc8

Browse files
authored
fix: usage of --output and --save (#56)
1 parent 0fbd351 commit 029cdc8

File tree

1 file changed

+74
-35
lines changed

1 file changed

+74
-35
lines changed

main.go

Lines changed: 74 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,6 @@ func run(cmd *cobra.Command, args []string) error {
182182
if err := utils.EnsureConfigDirectories(); err != nil {
183183
return fmt.Errorf("failed to ensure config directories: %w", err)
184184
}
185-
186185
// If no arguments are provided, use the current directory
187186
if len(args) == 0 {
188187
currentDir, err := os.Getwd()
@@ -355,7 +354,6 @@ func run(cmd *cobra.Command, args []string) error {
355354
"excluded": excludedInfo,
356355
}
357356

358-
359357
if err := spinner.Finish(); err != nil {
360358
return fmt.Errorf("failed to finish spinner: %w", err)
361359
}
@@ -369,13 +367,9 @@ func run(cmd *cobra.Command, args []string) error {
369367
// Check if save is set in config or flag
370368
autoSave, _ := cmd.Flags().GetBool("save")
371369
if cfg.AutoSave || autoSave {
372-
currentDir, err := os.Getwd()
373-
if err != nil {
374-
return fmt.Errorf("failed to get current directory: %w", err)
375-
}
376-
377-
if err := autoSaveOutput(rendered, currentDir); err != nil {
378-
utils.PrintColouredMessage("❌", fmt.Sprintf("Error saving file: %v", err), color.FgRed)
370+
// Pass the output flag value to autoSaveOutput
371+
if err := autoSaveOutput(rendered, output, args[0]); err != nil { // Assuming args[0] is a representative source path
372+
utils.PrintColouredMessage("❌", fmt.Sprintf("Error auto-saving file: %v", err), color.FgRed)
379373
}
380374
}
381375

@@ -394,7 +388,13 @@ func run(cmd *cobra.Command, args []string) error {
394388
utils.PrintColouredMessage("❌", fmt.Sprintf("LLM output error: %v", err), color.FgRed)
395389
}
396390
} else {
397-
if err := handleOutput(rendered, tokens, encoding, noClipboard, output, jsonOutput, report || verbose, allFiles); err != nil {
391+
// If both --save and --output are used, we don't want handleOutput to write the file
392+
// as autoSaveOutput will handle it
393+
outputForHandleOutput := output
394+
if (cfg.AutoSave || autoSave) && output != "" {
395+
outputForHandleOutput = ""
396+
}
397+
if err := handleOutput(rendered, tokens, encoding, noClipboard, outputForHandleOutput, jsonOutput, report || verbose, allFiles); err != nil {
398398
return fmt.Errorf("failed to handle output: %w", err)
399399
}
400400
}
@@ -469,24 +469,34 @@ func handleOutput(rendered string, countTokens bool, encoding string, noClipboar
469469
}
470470
fmt.Println(string(jsonBytes))
471471
} else {
472+
outputWritten := false
473+
if output != "" {
474+
err := utils.WriteToFile(output, rendered)
475+
if err != nil {
476+
// Report the error but continue to potentially copy to clipboard or print
477+
utils.PrintColouredMessage("❌", fmt.Sprintf("Failed to write to file %s: %v", output, err), color.FgRed)
478+
} else {
479+
utils.AddMessage("✅", fmt.Sprintf("Written to file: %s", output), color.FgGreen, 20)
480+
outputWritten = true
481+
}
482+
}
483+
484+
clipboardCopied := false
472485
if !noClipboard {
473486
err := utils.CopyToClipboard(rendered)
474487
if err == nil {
475488
utils.AddMessage("✅", "Copied to clipboard successfully.", color.FgGreen, 5)
476-
return nil
489+
clipboardCopied = true
490+
} else {
491+
// Only show clipboard error if we didn't write to a file
492+
if !outputWritten {
493+
utils.PrintColouredMessage("⚠️", fmt.Sprintf("Failed to copy to clipboard: %v.", err), color.FgYellow)
494+
}
477495
}
478-
// If clipboard copy failed, fall back to console output
479-
utils.PrintColouredMessage("i", fmt.Sprintf("Failed to copy to clipboard: %v. Falling back to console output.", err), color.FgYellow)
480496
}
481497

482-
if output != "" {
483-
err := utils.WriteToFile(output, rendered)
484-
if err != nil {
485-
return fmt.Errorf("failed to write to file: %w", err)
486-
}
487-
utils.AddMessage("✅", fmt.Sprintf("Written to file: %s", output), color.FgGreen, 20)
488-
} else {
489-
// If no output file is specified, print to console
498+
// If neither output file was written nor clipboard was copied, print to console
499+
if !outputWritten && !clipboardCopied {
490500
fmt.Print(rendered)
491501
}
492502
}
@@ -727,26 +737,55 @@ func performVRAMEstimation(content string) error {
727737
return nil
728738
}
729739

730-
func autoSaveOutput(content string, sourcePath string) error {
731-
homeDir, err := os.UserHomeDir()
732-
if err != nil {
733-
return fmt.Errorf("failed to get user home directory: %w", err)
740+
// autoSaveOutput saves the content based on the combination of --save and --output flags:
741+
// - If only --save is used, save to ~/ingest/<dirname>.md
742+
// - If --save and --output ./somefile.md, only save to ./somefile.md
743+
// - If --save and --output somefile.md, save to ~/ingest/somefile.md
744+
func autoSaveOutput(content string, outputPath string, sourcePath string) error {
745+
var finalPath string
746+
747+
if outputPath != "" {
748+
if strings.HasPrefix(outputPath, "./") || strings.HasPrefix(outputPath, "../") || filepath.IsAbs(outputPath) {
749+
// Case: --output starts with ./ or ../ or is absolute path
750+
// Save only to the specified output path
751+
absOutputPath, err := filepath.Abs(outputPath)
752+
if err != nil {
753+
return fmt.Errorf("failed to get absolute path for output file %s: %w", outputPath, err)
754+
}
755+
finalPath = absOutputPath
756+
} else {
757+
// Case: --output is just a filename
758+
// Save to ~/ingest/ with the specified filename
759+
homeDir, err := os.UserHomeDir()
760+
if err != nil {
761+
return fmt.Errorf("failed to get user home directory: %w", err)
762+
}
763+
ingestDir := filepath.Join(homeDir, "ingest")
764+
finalPath = filepath.Join(ingestDir, outputPath)
765+
}
766+
} else {
767+
// Default --save behavior
768+
homeDir, err := os.UserHomeDir()
769+
if err != nil {
770+
return fmt.Errorf("failed to get user home directory: %w", err)
771+
}
772+
ingestDir := filepath.Join(homeDir, "ingest")
773+
fileName := filepath.Base(sourcePath) + ".md"
774+
finalPath = filepath.Join(ingestDir, fileName)
734775
}
735776

736-
ingestDir := filepath.Join(homeDir, "ingest")
737-
if err := os.MkdirAll(ingestDir, 0700); err != nil {
738-
return fmt.Errorf("failed to create ingest directory: %w", err)
777+
// Ensure the directory for the final path exists
778+
finalDir := filepath.Dir(finalPath)
779+
if err := os.MkdirAll(finalDir, 0700); err != nil {
780+
return fmt.Errorf("failed to create directory %s for auto-save file: %w", finalDir, err)
739781
}
740782

741-
fileName := filepath.Base(sourcePath) + ".md"
742-
filePath := filepath.Join(ingestDir, fileName)
743-
744-
if err := os.WriteFile(filePath, []byte(content), 0600); err != nil {
745-
return fmt.Errorf("failed to write file: %w", err)
783+
// Write the file using os.WriteFile
784+
if err := os.WriteFile(finalPath, []byte(content), 0600); err != nil {
785+
return fmt.Errorf("failed to write auto-save file to %s: %w", finalPath, err)
746786
}
747787

748-
utils.AddMessage("✅", fmt.Sprintf("Automatically saved to %s", filePath), color.FgGreen, 10)
749-
788+
utils.AddMessage("💾", fmt.Sprintf("Auto-saved to: %s", finalPath), color.FgMagenta, 15) // Changed icon and message slightly
750789
return nil
751790
}
752791

0 commit comments

Comments
 (0)