-
-
Notifications
You must be signed in to change notification settings - Fork 100
/
main.go
205 lines (189 loc) · 5.29 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package main
import (
"context"
"fmt"
"os"
"runtime/debug"
"strconv"
"time"
"net/http"
_ "net/http/pprof"
"github.com/jessevdk/go-flags"
"github.com/moncho/dry/app"
"github.com/moncho/dry/appui"
"github.com/moncho/dry/docker"
"github.com/moncho/dry/ui"
"github.com/moncho/dry/version"
log "github.com/sirupsen/logrus"
)
const (
banner = ` _
| |
__| | ____ _ _
/ _ | / ___)| | | |
( (_| || | | |_| |
\____||_| \__ |
(____/
`
cheese = "<white>made with \U0001F499 (and go) by</> <blue>moncho</>"
connecting = "\U0001f433 Trying to connect to the Docker Host \U0001f433"
)
var loadMessage = []string{docker.Whale0,
docker.Whale1,
docker.Whale2,
docker.Whale3,
docker.Whale4,
docker.Whale5,
docker.Whale6,
docker.Whale7,
docker.Whale}
// options dry's flags
type options struct {
Description bool `short:"d" long:"description" description:"Shows the description"`
MonitorMode string `short:"m" long:"monitor" description:"Starts in monitor mode, given value (if any) is the refresh rate" optional:"yes" optional-value:"500"`
// enable profiling
Profile bool `short:"p" long:"profile" description:"Enable profiling"`
Version bool `short:"v" long:"version" description:"Dry version"`
//Docker-related properties
DockerHost string `short:"H" long:"docker_host" description:"Docker Host"`
DockerCertPath string `short:"c" long:"docker_certpath" description:"Docker cert path"`
DockerTLSVerifiy string `short:"t" long:"docker_tls" description:"Docker TLS verify"`
//Whale
Whale uint `short:"w" long:"whale" description:"Show whale for w seconds"`
}
func config(opts options) (app.Config, error) {
var cfg app.Config
if opts.DockerHost == "" {
if os.Getenv("DOCKER_HOST") == "" {
log.Printf(
"No DOCKER_HOST env variable found and no Host parameter was given, connecting to %s",
docker.DefaultDockerHost)
cfg.DockerHost = docker.DefaultDockerHost
} else {
cfg.DockerHost = os.Getenv("DOCKER_HOST")
cfg.DockerTLSVerify = docker.GetBool(os.Getenv("DOCKER_TLS_VERIFY"))
cfg.DockerCertPath = os.Getenv("DOCKER_CERT_PATH")
}
} else {
cfg.DockerHost = opts.DockerHost
cfg.DockerTLSVerify = docker.GetBool(opts.DockerTLSVerifiy)
cfg.DockerCertPath = opts.DockerCertPath
}
if opts.MonitorMode != "" {
cfg.MonitorMode = true
refreshRate, err := strconv.Atoi(opts.MonitorMode)
if err != nil {
return cfg, fmt.Errorf("invalid refresh rate %s: %w", opts.MonitorMode, err)
}
cfg.MonitorRefreshRate = refreshRate
}
return cfg, nil
}
func showLoadingScreen(ctx context.Context, screen *ui.Screen, cfg app.Config) {
screen.Clear()
midscreen := screen.Dimensions().Width / 2
height := screen.Dimensions().Height
screen.RenderAtColumn(midscreen-len(connecting)/2, 1, ui.White(connecting))
screen.RenderLine(2, height-2, fmt.Sprintf("<blue>Dry Version:</> %s", ui.White(version.VERSION)))
if cfg.DockerHost != "" {
screen.RenderLine(2, height-1, fmt.Sprintf("<blue>Docker Host:</> %s", ui.White(cfg.DockerHost)))
} else {
screen.RenderLine(2, height-1, ui.White("No Docker host"))
}
//20 is a safe aproximation for the length of interpreted characters from the message
screen.RenderLine(ui.ActiveScreen.Dimensions().Width-len(cheese)+20, height-1, cheese)
screen.Flush()
go func() {
rotorPos := 0
forward := true
ticker := time.NewTicker(250 * time.Millisecond)
for {
select {
case <-ticker.C:
loadingMessage := loadMessage[rotorPos]
screen.RenderAtColumn(midscreen-19, height/2-6, ui.Cyan(loadingMessage))
screen.Flush()
if rotorPos == len(loadMessage)-1 {
forward = false
} else if rotorPos == 0 {
forward = true
}
if forward {
rotorPos++
} else {
rotorPos--
}
case <-ctx.Done():
return
}
}
}()
}
func main() {
defer func() {
if r := recover(); r != nil {
log.Printf(
"Dry panicked: %v", r)
log.Error(string(debug.Stack()))
os.Exit(1)
}
}()
// parse flags
var opts options
parser := flags.NewParser(&opts, flags.Default)
_, err := parser.Parse()
if err != nil {
flagError := err.(*flags.Error)
if flagError.Type == flags.ErrHelp {
return
}
if flagError.Type == flags.ErrUnknownFlag {
log.Print("Use --help to view all available options.")
return
}
log.Printf("Could not parse flags: %s", err)
return
}
if opts.Description {
fmt.Print(app.ShortHelp)
return
}
if opts.Version {
fmt.Printf("dry version %s, build %s", version.VERSION, version.GITCOMMIT)
return
}
// Start profiling (if required)
if opts.Profile {
go func() {
log.Fatal(http.ListenAndServe("localhost:6060", nil))
}()
}
screen, err := ui.NewScreen(appui.DryTheme)
if err != nil {
log.Printf("Dry could not start: %s", err)
return
}
cfg, err := config(opts)
if err != nil {
log.Println(err.Error())
return
}
start := time.Now()
ctx, cancel := context.WithCancel(context.Background())
showLoadingScreen(ctx, screen, cfg)
dry, err := app.NewDry(screen, cfg)
//if asked to, show whale to bablat a bit longer
if opts.Whale > 0 {
showWale, _ := time.ParseDuration(fmt.Sprintf("%ds", opts.Whale))
time.Sleep(showWale - time.Since(start))
}
//dry has loaded, stopping the loading screen
cancel()
if err != nil {
screen.Close()
log.Printf("Dry could not start: %s", err)
return
}
app.RenderLoop(dry)
screen.Close()
}