-
Notifications
You must be signed in to change notification settings - Fork 0
/
domain-show.go
182 lines (161 loc) · 3.91 KB
/
domain-show.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
package dig
import (
"fmt"
"os"
"github.com/charmbracelet/bubbles/table"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/voodoologic/domania/cmd/namecheapHandler"
)
var baseStyle = lipgloss.NewStyle().
BorderStyle(lipgloss.NormalBorder()).
BorderForeground(lipgloss.Color("240"))
type tableModel struct {
digTable table.Model
nameCheap table.Model
}
func (m tableModel) Init() tea.Cmd { return nil }
func (m tableModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmd tea.Cmd
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "esc":
if m.digTable.Focused() {
m.digTable.Blur()
} else {
m.digTable.Focus()
}
case "q", "ctrl+c":
return m, tea.Quit
case "enter":
return m, nil
}
}
if len(m.digTable.Rows()) != len(m.nameCheap.Rows()) {
rows, cheap := consolidate(m)
rows, cheap = lookupOutliers(rows, cheap)
m.digTable.SetRows(rows)
m.nameCheap.SetRows(cheap)
rows, cheap = consolidate(m)
m.digTable.SetRows(rows)
m.digTable.SetHeight(len(rows))
m.nameCheap.SetRows(cheap)
m.nameCheap.SetHeight(len(cheap))
}
m.digTable, cmd = m.digTable.Update(msg)
return m, cmd
}
func (m tableModel) View() string {
pad := lipgloss.NewStyle().Padding(1)
tables := lipgloss.JoinHorizontal(
lipgloss.Top,
pad.Render(m.digTable.View()),
pad.Render(m.nameCheap.View()),
)
return baseStyle.Render(tables) + "\n"
}
func consolidate(myTableModel tableModel) ([]table.Row, []table.Row) {
digTableModel := myTableModel.digTable.Rows()
cheapsTableModel := myTableModel.nameCheap.Rows()
for _, row := range digTableModel {
cheap := returnMatchForRow(row, cheapsTableModel)
if cheap != nil {
row[0] = "Y"
cheap[0] = "Y"
} else {
row[0] = "N"
}
}
return digTableModel, cheapsTableModel
}
func returnMatchForRow(row table.Row, cheaps []table.Row) table.Row {
for _, cheap := range cheaps {
if match(row, cheap) {
return cheap
}
}
return nil
}
type Report struct {
digResults []DNSLookupResult
}
func match(row, cheap table.Row) bool {
for i := 1; len(row) > i; i++ {
if row[i] == cheap[i] {
continue
} else {
return false
}
}
return true
}
func lookupOutliers(rows, cheaps []table.Row) ([]table.Row, []table.Row) {
for _, cheap := range cheaps {
if cheap[0] == "?" {
//extract domain
newRows, err := DigDomain(cheap[2], cheap[1])
if err != nil {
}
rows = append(rows, newRows...)
}
}
return rows, cheaps
}
func DomainDetails(host string) {
// I want to call dig on this and display the information in a table
// I want to call namecheap and show the same information in a table
// namecheap.DomainsDNSGetHostsCommandResponse()
rows, err := InitDomain(host)
if err != nil {
panic(err)
}
columns := []table.Column{
{Title: "M", Width: 1},
{Title: "Type", Width: 5},
{Title: "Name", Width: 25},
{Title: "Data", Width: 33},
}
t := table.New(
table.WithColumns(columns),
table.WithRows(rows),
table.WithFocused(true),
table.WithHeight(len(rows)),
)
s := table.DefaultStyles()
s.Header = s.Header.
BorderStyle(lipgloss.NormalBorder()).
BorderForeground(lipgloss.Color("240")).
BorderBottom(true).
Bold(false)
s.Selected = s.Selected.
Foreground(lipgloss.Color("229")).
Background(lipgloss.Color("57")).
Bold(false)
t.SetStyles(s)
//
//
rows2, _ := namecheapHandler.GetDomainDetails(host)
t2 := table.New(
table.WithColumns(columns),
table.WithRows(rows2),
table.WithFocused(false),
table.WithHeight(len(rows2)),
)
s2 := table.DefaultStyles()
s2.Header = s2.Header.
BorderStyle(lipgloss.NormalBorder()).
BorderForeground(lipgloss.Color("240")).
BorderBottom(true).
Bold(false)
s2.Selected = s2.Selected.
Foreground(lipgloss.Color("229")).
Background(lipgloss.Color("57")).
Bold(false)
t2.SetStyles(s2)
m := tableModel{t, t2}
if _, err := tea.NewProgram(m).Run(); err != nil {
fmt.Println("error", err)
os.Exit(1)
}
}