/
do.go
46 lines (40 loc) · 986 Bytes
/
do.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
package tool
import (
"context"
"errors"
"fmt"
"runtime"
"darkp.ink/ema"
)
// Do tool with specific arguments.
func Do(ctx context.Context, args []string) error {
if err := do(ctx, args); err != nil {
return fmt.Errorf("tool.Do: %w", err)
}
return nil
}
func do(ctx context.Context, args []string) error {
switch len(args) {
case 3:
// continue
case 2:
if args[1] == "version" {
fmt.Print("ema " + ema.Version() + " " + runtime.GOOS + " " + runtime.GOARCH)
return nil
}
fallthrough
default:
fmt.Print(Help())
return NewExitError(ExitCodeInvalidArgs, errors.New("required input and output argument"))
}
w, err := NewWorkFromOS(ctx, args[1], args[2])
if err != nil {
fmt.Print(Help())
return err
}
if worker := RegisteredWorker(w.Formats()); worker != nil {
return worker.DoWork(w)
}
inputFormat, outputFormat := w.Formats()
return NewExitError(ExitCodeInvalidFormats, fmt.Errorf("no worker for %q to %q", inputFormat, outputFormat))
}