Added -f flag to allow reading lambda expressions from files. Changes: - Add FileSource type to read from file paths. - Add -f flag to command-line parser. - Implement validation to prevent conflicting -f and positional arguments.
42 lines
759 B
Go
42 lines
759 B
Go
package config
|
|
|
|
import (
|
|
"io"
|
|
"os"
|
|
)
|
|
|
|
// A method of extracting input from the user.
|
|
type Source interface {
|
|
// Fetch data from this source.
|
|
Extract() (string, error)
|
|
}
|
|
|
|
// A source defined by a string.
|
|
type StringSource struct{ Data string }
|
|
|
|
func (s StringSource) Extract() (string, error) { return s.Data, nil }
|
|
|
|
// A source pulling from standard input.
|
|
type StdinSource struct{}
|
|
|
|
func (s StdinSource) Extract() (string, error) {
|
|
data, err := io.ReadAll(os.Stdin)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
return string(data), nil
|
|
}
|
|
|
|
// A source reading from a file.
|
|
type FileSource struct{ Path string }
|
|
|
|
func (s FileSource) Extract() (string, error) {
|
|
data, err := os.ReadFile(s.Path)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
return string(data), nil
|
|
}
|