-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathmain.go
167 lines (147 loc) · 4.33 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package main
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"os"
"slices"
"strings"
"time"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/x/term"
"github.com/magodo/pipeform/internal/log"
"github.com/magodo/pipeform/internal/plainui"
"github.com/magodo/pipeform/internal/reader"
"github.com/magodo/pipeform/internal/ui"
"github.com/urfave/cli/v3"
)
type FlagSet struct {
LogLevel string
LogPath string
TeePath string
TimeCsv string
PlainUI bool
}
var fset FlagSet
func main() {
cmd := &cli.Command{
Name: "pipeform",
Usage: "Terraform UI by running like: `terraform ... -json | pipeform`",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "log-level",
Usage: "The log level",
Sources: cli.EnvVars("PF_LOG"),
Value: string(log.LevelDebug),
Destination: &fset.LogLevel,
Validator: func(input string) error {
if !slices.Contains(log.PossibleLevels(), log.Level(strings.ToLower(input))) {
return fmt.Errorf("invalid log level: %s", input)
}
return nil
},
},
&cli.StringFlag{
Name: "log-path",
Usage: "The log path",
Sources: cli.EnvVars("PF_LOG_PATH"),
Destination: &fset.LogPath,
},
&cli.StringFlag{
Name: "tee",
Usage: `Equivalent to "terraform ... -json | tee <value> | pipeform"`,
Sources: cli.EnvVars("PF_TEE"),
Destination: &fset.TeePath,
},
&cli.StringFlag{
Name: "time-csv",
Usage: "The csv file that records the timing of each operation of each resource",
Sources: cli.EnvVars("PF_TIME_CSV"),
Destination: &fset.TimeCsv,
},
&cli.BoolFlag{
Name: "plain-ui",
Usage: "Simply print each log line by line, that expect to use in systems only support plain output",
Sources: cli.EnvVars("PF_PLAIN_UI"),
Destination: &fset.PlainUI,
},
},
Before: func(ctx context.Context, c *cli.Command) (context.Context, error) {
// If this program starts in standalone, its stdin is the same as the terminal.
// bubbletea will change the terminal into raw mode and read ansi events from it,
// which conflicts with the stdin reading for terraform JSON streams.
// In this case, user's input (e.g. ctrl-c keypress) will most likely be accidently read by
// the stream reader, instead of the ansi read loop (by bubbletea), causing a lost of event.
if term.IsTerminal(os.Stdin.Fd()) {
return ctx, errors.New("Must be followed by a pipe")
}
return ctx, nil
},
Action: func(context.Context, *cli.Command) error {
startTime := time.Now()
logger, err := log.NewLogger(log.Level(fset.LogLevel), fset.LogPath)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
defer logger.Close()
teeWriter := io.Discard
if path := fset.TeePath; path != "" {
f, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0600)
if err != nil {
return fmt.Errorf("open for tee: %v", err)
}
teeWriter = f
defer f.Close()
}
reader := reader.NewReader(os.Stdin, teeWriter)
type Model interface {
ToCsv() []byte
IsEOF() bool
}
var model Model
if fset.PlainUI {
m := plainui.NewRuntimeModel(logger, reader, os.Stdout, startTime)
if err := m.Run(); err != nil {
return fmt.Errorf("Error running program: %v\n", err)
}
model = m
} else {
m := ui.NewRuntimeModel(logger, reader, startTime)
tm, err := tea.NewProgram(m, tea.WithInputTTY(), tea.WithAltScreen()).Run()
if err != nil {
return fmt.Errorf("Error running program: %v\n", err)
}
m = tm.(ui.UIModel)
// Print diags
for _, diag := range m.Diags() {
if b, err := json.MarshalIndent(diag, "", " "); err == nil {
fmt.Fprintln(os.Stderr, string(b))
}
}
model = m
}
if path := fset.TimeCsv; path != "" {
f, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600)
if err != nil {
return fmt.Errorf("open time csv file: %v", err)
}
defer f.Close()
if _, err := f.Write(model.ToCsv()); err != nil {
fmt.Fprintf(os.Stderr, "writing time csv file: %v", err)
}
}
if !model.IsEOF() {
fmt.Fprintln(os.Stderr, "Interrupted!")
os.Exit(1)
}
return nil
},
}
if err := cmd.Run(context.Background(), os.Args); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}