feat: rename config to cli
This commit is contained in:
27
internal/cli/destination.go
Normal file
27
internal/cli/destination.go
Normal file
@@ -0,0 +1,27 @@
|
||||
package cli
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
)
|
||||
|
||||
// A Destination is method of writing output to the user.
|
||||
type Destination interface {
|
||||
// Write data to this destination.
|
||||
Write(data string) error
|
||||
}
|
||||
|
||||
// An StdoutDestination writes to stdout.
|
||||
type StdoutDestination struct{}
|
||||
|
||||
func (d StdoutDestination) Write(data string) error {
|
||||
fmt.Println(data)
|
||||
return nil
|
||||
}
|
||||
|
||||
// A FileDestination writes to a file.
|
||||
type FileDestination struct{ Path string }
|
||||
|
||||
func (d FileDestination) Write(data string) error {
|
||||
return os.WriteFile(d.Path, []byte(data+"\n"), 0644)
|
||||
}
|
||||
41
internal/cli/source.go
Normal file
41
internal/cli/source.go
Normal file
@@ -0,0 +1,41 @@
|
||||
package cli
|
||||
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user