-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
80 lines (68 loc) · 1.61 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
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
package main
import (
"fmt"
"io/ioutil"
"log"
"os"
"sort"
"github.com/urfave/cli"
"moul.io/converter"
)
var (
VERSION string
GITCOMMIT string
)
func main() {
app := cli.NewApp()
app.Name = "converter"
app.Author = "Manfred Touron"
app.Email = "https://github.com/moul/converter"
if VERSION != "" || GITCOMMIT != "" {
app.Version = VERSION + " (" + GITCOMMIT + ")"
}
app.EnableBashCompletion = true
app.Commands = []cli.Command{}
filters := []string{}
for filter := range converter.Converters {
filters = append(filters, filter)
}
sort.Strings(filters)
for _, filter := range filters {
command := cli.Command{
Name: filter,
Action: Action,
BashComplete: BashComplete,
Hidden: filter[0] == '_',
// Usage: fmt.Sprintf("%s -> %s", filter.InputType, filter.OutputType),
}
app.Commands = append(app.Commands, command)
}
if err := app.Run(os.Args); err != nil {
log.Fatalf("Error: %v", err)
}
}
func BashComplete(c *cli.Context) {
for filter := range converter.Converters {
fmt.Println(filter)
}
}
func Action(c *cli.Context) error {
args := append([]string{c.Command.Name}, c.Args()...)
if len(args) == 0 {
return fmt.Errorf("you need to use at least one filter") // nolint:goerr113
}
fn, err := converter.ChainFunc(args)
if err != nil {
return fmt.Errorf("failed to create a converter: %w", err)
}
input, err := ioutil.ReadAll(os.Stdin)
if err != nil {
return fmt.Errorf("failed to read from stdin: %w", err)
}
output, err := fn(input)
if err != nil {
return fmt.Errorf("failed to convert: %w", err)
}
fmt.Printf("%v\n", output)
return nil
}