-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
51 lines (46 loc) · 1.17 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package main
import (
"bufio"
"fmt"
"io"
"os"
"strings"
"github.com/fastfadingviolets/qwertyflip/flip"
)
func getTransform(flipper *flip.Flipper, fromFile string) (flip.Transform, error) {
cmdFile, err := os.Open(fromFile)
if err != nil {
return nil, fmt.Errorf("Unable to open command file %s: %v", fromFile, err)
}
defer cmdFile.Close()
bytes, err := io.ReadAll(cmdFile)
if err != nil {
return nil, fmt.Errorf("Unable to read command file %s: %v", fromFile, err)
}
cmdString := strings.TrimSpace(string(bytes))
return flipper.ParseCommand(cmdString)
}
func main() {
if len(os.Args) != 3 {
fmt.Fprintf(os.Stderr, "Usage: %s <command file> <input file>\n", os.Args[0])
os.Exit(1)
}
commandFile, inputFile := os.Args[1], os.Args[2]
flipper := flip.NewFlipper()
transform, err := getTransform(flipper, commandFile)
if err != nil {
fmt.Fprint(os.Stderr, err)
os.Exit(1)
}
file, err := os.Open(inputFile)
if err != nil {
fmt.Fprintf(os.Stderr, "Unable to open input file %s: %v", inputFile, err)
os.Exit(1)
}
defer file.Close()
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := transform.Apply(scanner.Text())
fmt.Println(line)
}
}