-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
78 lines (62 loc) · 1.47 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
package main
import (
"encoding/json"
"flag"
"fmt"
"log/slog"
"os"
"strings"
"github.com/wim-web/tfoon/pkg/moddep"
)
func TreesPresenter() func(moddep.ModuleTreeList) (string, error) {
return func(trees moddep.ModuleTreeList) (string, error) {
b, err := json.Marshal(trees)
return string(b), err
}
}
func M2ePresenter() func(moddep.Module2EntryPoint) (string, error) {
return func(m2e moddep.Module2EntryPoint) (string, error) {
b, err := json.Marshal(m2e)
return string(b), err
}
}
func main() {
logger := slog.New(slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{
Level: slog.LevelError,
}))
// --separator -s flag
separator := flag.String("separate", ",", "default is comma")
m2e := flag.Bool("m2e", false, "module to entrypoint mode")
flag.Parse()
args := flag.Args()
if len(args) != 1 {
logger.Error("e.g.: tfoon path1,path2")
os.Exit(1)
}
paths := strings.Split(args[0], *separator)
output, err := getOutput(paths, *m2e, logger)
if err != nil {
logger.Error(err.Error())
os.Exit(1)
}
fmt.Println(output)
}
func getOutput(paths []string, m2eMode bool, logger *slog.Logger) (string, error) {
trees, err := moddep.FromPaths(paths)
if err != nil {
return "", err
}
var output string
if m2eMode {
m2e := trees.ToModule2EntryPoint()
presenter := M2ePresenter()
output, err = presenter(m2e)
} else {
presenter := TreesPresenter()
output, err = presenter(trees)
}
if err != nil {
return "", err
}
return output, nil
}