-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtsh.go
More file actions
98 lines (83 loc) · 2.26 KB
/
tsh.go
File metadata and controls
98 lines (83 loc) · 2.26 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package main
import (
"fmt"
"os"
"path/filepath"
"strings"
"github.com/monstermichl/typeshell/converters/bash"
"github.com/monstermichl/typeshell/converters/batch"
"github.com/monstermichl/typeshell/transpiler"
)
const (
typeBatch string = "batch"
typeBash string = "bash"
)
var convMapping = map[string]transpiler.Converter{
typeBatch: batch.New(),
typeBash: bash.New(),
}
type options struct {
in string
out string
converters []transpiler.Converter
}
func parseOptions() options {
args := os.Args
options := options{}
types := []string{}
for k := range convMapping {
types = append(types, k)
}
for i := 1; i < (len(args) - 1); i += 2 {
cSwitch := args[i]
cValue := args[i+1]
switch cSwitch {
case "-i", "--in":
// Make sure input file exists.
if stat, err := os.Stat(cValue); err != nil {
panic(fmt.Errorf("input file %s doesn't exist", cValue))
} else if stat.IsDir() {
panic(fmt.Errorf("input %s is not a file", cValue))
}
options.in = cValue
case "-o", "--out":
// Make sure output path exists.
if stat, err := os.Stat(cValue); err != nil {
panic(fmt.Errorf("output path %s doesn't exist", cValue))
} else if !stat.IsDir() {
panic(fmt.Errorf("output path %s is not a directory", cValue))
}
options.out = cValue
case "-t", "--type":
conv, ok := convMapping[cValue]
if !ok {
panic(fmt.Errorf("unknown converter type %s. Allowed types are %s", cValue, strings.Join(types, ", ")))
}
options.converters = append(options.converters, conv)
default:
panic(fmt.Errorf("unknown option %s", cSwitch))
}
}
if len(options.in) == 0 {
panic("no input file provided (-i/--in)")
} else if len(options.out) == 0 {
panic("no output directory provided (-o/--out)")
} else if len(options.converters) == 0 {
panic("no output type provided (-t/--type)")
}
return options
}
func main() {
options := parseOptions()
t := transpiler.New()
for _, conv := range options.converters {
in := options.in
dump, err := t.Transpile(in, conv)
if err != nil {
panic(err)
}
file := filepath.Base(in)
file = file[0 : len(file)-len(filepath.Ext(in))] // Remove extension.
os.WriteFile(filepath.Join(options.out, fmt.Sprintf("%s.%s", file, conv.Extension())), []byte(dump), 0777)
}
}