feat: add output flag (#13)
## Description The lambda CLI previously only wrote output to stdout using shell redirection. This PR adds support for writing results to files using the `-o` flag. This is implemented using a new `Destination` interface that mirrors the existing `Source` pattern. Changes: - Added `Destination` interface with `StdoutDestination` and `FileDestination` implementations. - Added `-o` flag to CLI argument parser for output file specification. - Updated `Config` to use `Destination` instead of direct output handling. - Refactored main to use `Destination.Write()` for result output. - Updated Makefile targets (`run`, `profile`, `explain`) to use `-o` flag instead of shell redirection. ### Decisions The `-o` flag defaults to stdout when not specified or when set to `-`. This maintains backward compatibility while providing explicit file output capability. ## Benefits - Cleaner command-line interface without shell redirection. - Symmetric design with `Source` interface for input. - More portable across different shells and environments. - Explicit output handling improves code clarity. ## Checklist - [x] Code follows conventional commit format. - [x] Branch follows naming convention (`<type>/<description>`). - [ ] Tests pass (if applicable). - [ ] Documentation updated (if applicable). Reviewed-on: #13 Co-authored-by: M.V. Hutz <git@maximhutz.me> Co-committed-by: M.V. Hutz <git@maximhutz.me>
This commit was merged in pull request #13.
This commit is contained in:
6
Makefile
6
Makefile
@@ -20,13 +20,13 @@ build:
|
||||
chmod +x ${BINARY_NAME}
|
||||
|
||||
run: build
|
||||
./${BINARY_NAME} -f ./samples/$(TEST).txt > program.out
|
||||
./${BINARY_NAME} -f ./samples/$(TEST).txt -o program.out
|
||||
|
||||
profile: build
|
||||
./${BINARY_NAME} -p profile/cpu.prof -f ./samples/$(TEST).txt > program.out
|
||||
./${BINARY_NAME} -p profile/cpu.prof -f ./samples/$(TEST).txt -o program.out
|
||||
|
||||
explain: build
|
||||
./${BINARY_NAME} -x -p profile/cpu.prof -f ./samples/$(TEST).txt > program.out
|
||||
./${BINARY_NAME} -x -p profile/cpu.prof -f ./samples/$(TEST).txt -o program.out
|
||||
|
||||
graph:
|
||||
go tool pprof -raw -output=profile/cpu.raw profile/cpu.prof
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"git.maximhutz.com/max/lambda/internal/cli"
|
||||
@@ -72,5 +71,7 @@ func main() {
|
||||
process.Run()
|
||||
|
||||
// Return the final reduced result.
|
||||
fmt.Println(lambda.Stringify(compiled))
|
||||
result := lambda.Stringify(compiled)
|
||||
err = options.Destination.Write(result)
|
||||
cli.HandleError(err)
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ package config
|
||||
// Configuration settings for the program.
|
||||
type Config struct {
|
||||
Source Source // The source code given to the program.
|
||||
Destination Destination // The destination for the final result.
|
||||
Verbose bool // Whether or not to print debug logs.
|
||||
Explanation bool // Whether or not to print an explanation of the reduction.
|
||||
Profile string // If not nil, print a CPU profile during execution.
|
||||
|
||||
27
internal/config/destination.go
Normal file
27
internal/config/destination.go
Normal file
@@ -0,0 +1,27 @@
|
||||
package config
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
// A method of writing output to the user.
|
||||
type Destination interface {
|
||||
// Write data to this destination.
|
||||
Write(data string) error
|
||||
}
|
||||
|
||||
// A destination writing to stdout.
|
||||
type StdoutDestination struct{}
|
||||
|
||||
func (d StdoutDestination) Write(data string) error {
|
||||
fmt.Println(data)
|
||||
return nil
|
||||
}
|
||||
|
||||
// A destination writing to a file.
|
||||
type FileDestination struct{ Path string }
|
||||
|
||||
func (d FileDestination) Write(data string) error {
|
||||
return os.WriteFile(d.Path, []byte(data+"\n"), 0644)
|
||||
}
|
||||
@@ -13,6 +13,7 @@ func FromArgs() (*Config, error) {
|
||||
statistics := flag.Bool("s", false, "Statistics. If set, the process will print various statistics about the run.")
|
||||
profile := flag.String("p", "", "CPU profiling. If an output file is defined, the program will profile its execution and dump its results into it.")
|
||||
file := flag.String("f", "", "File. If set, read source from the specified file.")
|
||||
output := flag.String("o", "", "Output. If set, write result to the specified file. Use '-' for stdout (default).")
|
||||
flag.Parse()
|
||||
|
||||
// Parse source type.
|
||||
@@ -36,8 +37,17 @@ func FromArgs() (*Config, error) {
|
||||
}
|
||||
}
|
||||
|
||||
// Parse destination type.
|
||||
var destination Destination
|
||||
if *output == "" || *output == "-" {
|
||||
destination = StdoutDestination{}
|
||||
} else {
|
||||
destination = FileDestination{Path: *output}
|
||||
}
|
||||
|
||||
return &Config{
|
||||
Source: source,
|
||||
Destination: destination,
|
||||
Verbose: *verbose,
|
||||
Explanation: *explanation,
|
||||
Profile: *profile,
|
||||
|
||||
Reference in New Issue
Block a user