Skip to content

Commit 50f94be

Browse files
committed
refactor: pkg -> internal
1 parent d5622a9 commit 50f94be

File tree

12 files changed

+175
-112
lines changed

12 files changed

+175
-112
lines changed
File renamed without changes.

pkg/examples/generate.go renamed to internal/examples/generate.go

+4-35
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
11
package examples
22

33
import (
4-
"bytes"
5-
"errors"
64
"fmt"
7-
"io"
85
"os"
9-
"path/filepath"
106
"strings"
117

128
"github.com/FrangipaneTeam/bean/config"
139
"github.com/FrangipaneTeam/bean/internal/exlist"
10+
yml "github.com/FrangipaneTeam/bean/pkg/yaml"
1411
"github.com/FrangipaneTeam/bean/tui/pages/errorpanel"
1512
"github.com/charmbracelet/bubbles/list"
1613
tea "github.com/charmbracelet/bubbletea"
@@ -95,7 +92,7 @@ func createExampleList(dir string) ([]*exlist.Example, *errorpanel.ErrorMsg) {
9592
}
9693

9794
// get only yaml files
98-
if isYaml := isYamlFile(sf.Name()); !isYaml {
95+
if isYaml := yml.IsYamlFile(sf.Name()); !isYaml {
9996
continue
10097
}
10198

@@ -157,12 +154,6 @@ func createExampleList(dir string) ([]*exlist.Example, *errorpanel.ErrorMsg) {
157154
return exampleList, nil
158155
}
159156

160-
// isYamlFile returns true if the file is a yaml file.
161-
func isYamlFile(fileName string) bool {
162-
ext := filepath.Ext(fileName)
163-
return ext == ".yaml" || ext == ".yml"
164-
}
165-
166157
func checkForExtraFile(dir string, file string) (int, *errorpanel.ErrorMsg) {
167158
var (
168159
extraK8S *exlist.Example
@@ -173,7 +164,7 @@ func checkForExtraFile(dir string, file string) (int, *errorpanel.ErrorMsg) {
173164
fmt.Sprintf("%s/%s.extra", dir, file),
174165
)
175166
if err == nil {
176-
extraY, errSplitYaml := splitYAML(extraYFile)
167+
extraY, errSplitYaml := yml.SplitYAML(extraYFile)
177168
if errSplitYaml != nil {
178169
return extraKind, &errorpanel.ErrorMsg{
179170
Reason: "split yaml error",
@@ -204,7 +195,7 @@ func checkForSecretFile(dir string, file string) (int, *errorpanel.ErrorMsg) {
204195
fmt.Sprintf("%s/%s.secret", dir, file),
205196
)
206197
if err == nil {
207-
extraY, errSplitYaml := splitYAML(extraSFile)
198+
extraY, errSplitYaml := yml.SplitYAML(extraSFile)
208199
if errSplitYaml != nil {
209200
return extraKind, &errorpanel.ErrorMsg{
210201
Reason: "split yaml error",
@@ -225,25 +216,3 @@ func checkForSecretFile(dir string, file string) (int, *errorpanel.ErrorMsg) {
225216
}
226217
return extraKind, nil
227218
}
228-
229-
func splitYAML(resources []byte) ([][]byte, error) {
230-
dec := yaml.NewDecoder(bytes.NewReader(resources))
231-
232-
var res [][]byte
233-
for {
234-
var value interface{}
235-
err := dec.Decode(&value)
236-
if errors.Is(err, io.EOF) {
237-
break
238-
}
239-
if err != nil {
240-
return nil, err
241-
}
242-
valueBytes, err := yaml.Marshal(value)
243-
if err != nil {
244-
return nil, err
245-
}
246-
res = append(res, valueBytes)
247-
}
248-
return res, nil
249-
}

pkg/examples/watch.go renamed to internal/examples/watch.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"path/filepath"
99

1010
"github.com/FrangipaneTeam/bean/config"
11+
yml "github.com/FrangipaneTeam/bean/pkg/yaml"
1112
"github.com/FrangipaneTeam/bean/tui/pages/errorpanel"
1213
tea "github.com/charmbracelet/bubbletea"
1314
"github.com/dietsche/rfsnotify"
@@ -74,7 +75,7 @@ func watchCRDFiles(watcher *fsnotify.Watcher, done chan bool, ch chan NotifyActi
7475
Cause: errors.New("event not ok"),
7576
}
7677
}
77-
if isYamlFile(event.Name) {
78+
if yml.IsYamlFile(event.Name) {
7879
f := NotifyActivity{
7980
FileName: event.Name,
8081
}

internal/template/template.go

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package template
2+
3+
import (
4+
"html/template"
5+
"log"
6+
"os"
7+
)
8+
9+
// Process processes a template file and writes the output to a file.
10+
func Process(rawTemplate string, outputFile string, data interface{}) {
11+
var err error
12+
13+
// Read the template file
14+
t := template.Must(template.New("listTested").Parse(rawTemplate)) // .Funcs(funcMap)
15+
16+
// Parse the template
17+
t, err = t.Parse(rawTemplate)
18+
if err != nil {
19+
log.Fatal(err)
20+
}
21+
22+
// create a new file
23+
file, _ := os.Create(outputFile)
24+
defer file.Close()
25+
26+
// Execute the template
27+
err = t.ExecuteTemplate(file, "listTested", data)
28+
if err != nil {
29+
//TODO: handle error in app
30+
log.Print(err)
31+
}
32+
}

pkg/logs/logs.go

-1
This file was deleted.

pkg/yaml/yaml.go

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package yaml
2+
3+
import (
4+
"bytes"
5+
"errors"
6+
"io"
7+
"path/filepath"
8+
9+
"gopkg.in/yaml.v3"
10+
)
11+
12+
// SplitYAML splits a YAML file into multiple YAML files.
13+
func SplitYAML(resources []byte) ([][]byte, error) {
14+
dec := yaml.NewDecoder(bytes.NewReader(resources))
15+
16+
var res [][]byte
17+
for {
18+
var value interface{}
19+
err := dec.Decode(&value)
20+
if errors.Is(err, io.EOF) {
21+
break
22+
}
23+
if err != nil {
24+
return nil, err
25+
}
26+
valueBytes, err := yaml.Marshal(value)
27+
if err != nil {
28+
return nil, err
29+
}
30+
res = append(res, valueBytes)
31+
}
32+
return res, nil
33+
}
34+
35+
// IsYamlFile returns true if the file is a yaml file regardless of the extension.
36+
func IsYamlFile(fileName string) bool {
37+
ext := filepath.Ext(fileName)
38+
return ext == ".yaml" || ext == ".yml"
39+
}

tui/pages/exlist/manage.go

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package exlist
2+
3+
import (
4+
"github.com/charmbracelet/bubbles/list"
5+
tea "github.com/charmbracelet/bubbletea"
6+
)
7+
8+
// UpdateExamplesList updates the examples list.
9+
func (m *Model) UpdateExamplesList(examples map[string][]list.Item) {
10+
m.exampleList = examples
11+
}
12+
13+
func (m *Model) UpdateList(params ...string) (*Model, tea.Cmd) {
14+
var cmd tea.Cmd
15+
title := m.listName
16+
if len(params) != 0 {
17+
title = params[0]
18+
}
19+
20+
if _, ok := m.exampleList[title]; ok {
21+
i := m.exampleList[title]
22+
cmd = m.CurrentList.SetItems(i)
23+
m.listName = title
24+
}
25+
return m, cmd
26+
}
27+
28+
func (m *Model) SetRootItems() (*Model, tea.Cmd) {
29+
cmd := m.CurrentList.SetItems(m.exampleList["-"])
30+
return m, cmd
31+
}

tui/pages/exlist/new.go

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package exlist
2+
3+
import (
4+
"github.com/FrangipaneTeam/bean/internal/exlist"
5+
"github.com/FrangipaneTeam/bean/internal/keymap"
6+
"github.com/FrangipaneTeam/bean/internal/theme"
7+
"github.com/charmbracelet/bubbles/list"
8+
"github.com/charmbracelet/lipgloss"
9+
)
10+
11+
type Model struct {
12+
listName string
13+
keys *keymap.ListKeyMap
14+
15+
// list
16+
exampleList map[string][]list.Item
17+
CurrentList list.Model
18+
}
19+
20+
func New(
21+
keymap *keymap.ListKeyMap,
22+
exampleList exlist.LoadedExamples,
23+
width int,
24+
height int,
25+
) *Model {
26+
delegate := list.NewDefaultDelegate()
27+
28+
delegate.Styles.SelectedTitle = delegate.Styles.SelectedTitle.
29+
BorderForeground(theme.HighlightColour).
30+
Foreground(theme.HighlightColour).
31+
Bold(true)
32+
33+
delegate.Styles.SelectedDesc = delegate.Styles.SelectedDesc.
34+
BorderForeground(theme.HighlightColour).
35+
Foreground(theme.HighlightFeintColour)
36+
37+
delegate.Styles.DimmedDesc = delegate.Styles.DimmedDesc.
38+
Foreground(theme.FeintColour)
39+
40+
delegate.Styles.FilterMatch = lipgloss.NewStyle().
41+
Underline(true).
42+
Bold(true)
43+
44+
list := list.New(exampleList.Examples["-"],
45+
delegate,
46+
width,
47+
height,
48+
)
49+
list.Title = "Choose an example"
50+
list.DisableQuitKeybindings()
51+
list.SetShowHelp(false)
52+
list.SetStatusBarItemName("example", "examples")
53+
54+
// list.SetSize()
55+
56+
return &Model{
57+
keys: keymap,
58+
exampleList: exampleList.Examples,
59+
CurrentList: list,
60+
}
61+
}

tui/pages/header/header.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import (
77
"time"
88

99
"github.com/FrangipaneTeam/bean/config"
10+
"github.com/FrangipaneTeam/bean/internal/examples"
1011
"github.com/FrangipaneTeam/bean/internal/theme"
11-
"github.com/FrangipaneTeam/bean/pkg/examples"
1212
"github.com/FrangipaneTeam/bean/tui/pages/common"
1313
"github.com/charmbracelet/bubbles/spinner"
1414
tea "github.com/charmbracelet/bubbletea"

tui/pages/home/examples.go

-44
Original file line numberDiff line numberDiff line change
@@ -385,36 +385,9 @@ func (m model) View() string {
385385
footer.String(),
386386
))
387387

388-
// if m.width > 0 {
389-
// // physicalWidth, physicalHeight, _ := term.GetSize(int(os.Stdout.Fd()))
390-
// tui.AppStyle = tui.AppStyle.MaxWidth(m.width).MaxHeight(m.height)
391-
// }
392-
393-
// Okay, let's print it
394388
return theme.AppStyle.Render(doc.String())
395389
}
396390

397-
// func (m model) showExamples() (model, tea.Cmd) {
398-
// i := m.exampleList["-"]
399-
// cmd := m.currentList.SetItems(i)
400-
// m.currentList.Title = "Choose an example"
401-
// m.listName = "-"
402-
403-
// m.currentList.Select(0)
404-
405-
// return m, cmd
406-
// }
407-
408-
// func (m model) showYaml(title string) (model, tea.Cmd) {
409-
// var cmd tea.Cmd
410-
// if _, ok := m.exampleList[title]; ok {
411-
// i := m.exampleList[title]
412-
// cmd = m.currentList.SetItems(i)
413-
// m.listName = title
414-
// }
415-
// return m, cmd
416-
// }
417-
418391
func (m model) tickCmd() tea.Cmd {
419392
return tea.Tick(time.Second*1, func(t time.Time) tea.Msg {
420393
if !m.k8s.IsTickRunning() {
@@ -424,23 +397,6 @@ func (m model) tickCmd() tea.Cmd {
424397
})
425398
}
426399

427-
// func (m model) rootView() (model, tea.Cmd) {
428-
// var cmds []tea.Cmd
429-
// m.pages.SetViewName(pages.PRoot)
430-
// m.keys.EnableRootKeys()
431-
// cmd := m.currentList.NewStatusMessage("back to home")
432-
// cmds = append(cmds, cmd)
433-
434-
// if m.currentList.FilterState() == list.FilterApplied {
435-
// m.currentList.ResetFilter()
436-
// }
437-
438-
// m, cmd = m.showExamples()
439-
// cmds = append(cmds, cmd)
440-
441-
// return m, tea.Batch(cmds...)
442-
// }
443-
444400
func (m model) generateK8SFiles() (model, *k8s.Cmd, tea.Cmd) {
445401
if m.pages.CurrentList.SelectedItem() == nil {
446402
cmd := m.errorPanel.Init()

tui/pages/loading/loading.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ import (
55
"fmt"
66

77
"github.com/FrangipaneTeam/bean/config"
8+
"github.com/FrangipaneTeam/bean/internal/examples"
89
"github.com/FrangipaneTeam/bean/internal/exlist"
910
"github.com/FrangipaneTeam/bean/internal/theme"
10-
"github.com/FrangipaneTeam/bean/pkg/examples"
1111
"github.com/FrangipaneTeam/bean/tui/pages/errorpanel"
1212
"github.com/FrangipaneTeam/bean/tui/pages/home"
1313
"github.com/charmbracelet/bubbles/spinner"

0 commit comments

Comments
 (0)