Skip to content

Commit

Permalink
chore: address golangci-lint issues (#74)
Browse files Browse the repository at this point in the history
  • Loading branch information
noahgorstein authored May 11, 2024
1 parent 036457b commit 5565fd9
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 68 deletions.
2 changes: 1 addition & 1 deletion .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ linters-settings:

# https://github.com/mgechev/revive/blob/master/RULES_DESCRIPTIONS.md#cyclomatic
- name: cyclomatic
arguments: [5] # default 3
arguments: [8] # default 3

# https://github.com/mgechev/revive/blob/master/RULES_DESCRIPTIONS.md#exported
- name: exported
Expand Down
6 changes: 3 additions & 3 deletions cmd/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ import (
// It takes a byte slice 'data' and returns two boolean values indicating
// whether the data is valid JSON and valid JSON lines, along with an error
// if the data is not valid in either format.
func isValidInput(data []byte) (bool, bool, error) {
isValidJSON := utils.IsValidJSON(data)
isValidJSONLines := utils.IsValidJSONLines(data)
func isValidInput(data []byte) (isValidJSON bool, isValidJSONLines bool, err error) {
isValidJSON = utils.IsValidJSON(data)
isValidJSONLines = utils.IsValidJSONLines(data)
if !isValidJSON && !isValidJSONLines {
return false, false, errors.New("Data is not valid JSON or LDJSON")
}
Expand Down
4 changes: 2 additions & 2 deletions tui/bubbles/help/help.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ func (b *Bubble) SetState(mode state.State) {
func (b Bubble) Update(msg tea.Msg) (Bubble, tea.Cmd) {
var cmd tea.Cmd

switch msg := msg.(type) {
case tea.WindowSizeMsg:
if msg, ok := msg.(tea.WindowSizeMsg); ok {
b.SetWidth(msg.Width)
}

return b, tea.Batch(cmd)
}
116 changes: 68 additions & 48 deletions tui/bubbles/jqplayground/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,64 +31,84 @@ type copyQueryToClipboardMsg struct{}

type copyResultsToClipboardMsg struct{}

// executeQuery executes a query using the provided query input and input data,
// returning a command that produces a message containing the results of the query.
// It parses the query input, processes the input data according to whether it's in JSON
// lines format or not, and then iterates over the results of the query, formatting them
// and returning them as a message. If an error occurs during parsing, processing, or
// iterating over the results, an error message is returned instead.
func (b *Bubble) executeQuery(ctx context.Context) tea.Cmd {
return func() tea.Msg {
var results strings.Builder
query, err := gojq.Parse(b.queryinput.GetInputValue())
if err != nil {
return errorMsg{error: err}
// processQueryResults iterates through the results of a gojq query on the provided JSON object
// and appends the formatted results to the provided string builder.
func processQueryResults(ctx context.Context, results *strings.Builder, query *gojq.Query, obj any) error {
iter := query.RunWithContext(ctx, obj)
for {
v, ok := iter.Next()
if !ok {
break
}

processInput := func(data []byte) error {
var obj any
if err := json.Unmarshal(data, &obj); err != nil {
return err
}
if err, ok := v.(error); ok {
return err
}

iter := query.RunWithContext(ctx, obj)
for {
v, ok := iter.Next()
if !ok {
break
}
if err, ok := v.(error); ok {
return err
}
r, err := gojq.Marshal(v)
if err != nil {
continue
}
results.WriteString(fmt.Sprintf("%s\n", string(r)))
}
return nil
if r, err := gojq.Marshal(v); err == nil {
results.WriteString(fmt.Sprintf("%s\n", string(r)))
}
}
return nil
}

if b.isJSONLines {
scanner := bufio.NewScanner(bytes.NewReader(b.inputdata.GetInputJSON()))
for scanner.Scan() {
line := scanner.Bytes()
if err := processInput(line); err != nil {
return errorMsg{error: err}
}
}
} else {
if err := processInput(b.inputdata.GetInputJSON()); err != nil {
return errorMsg{error: err}
}
func processJSONWithQuery(ctx context.Context, results *strings.Builder, query *gojq.Query, data []byte) error {
var obj any
if err := json.Unmarshal(data, &obj); err != nil {
return err
}
err := processQueryResults(ctx, results, query, obj)
if err != nil {
return err
}

return nil
}

func processJSONLinesWithQuery(ctx context.Context, results *strings.Builder, query *gojq.Query, data []byte) error {
scanner := bufio.NewScanner(bytes.NewReader(data))
for scanner.Scan() {
line := scanner.Bytes()
if err := processJSONWithQuery(ctx, results, query, line); err != nil {
return err
}
}
return nil

}

func (b *Bubble) executeQueryOnInput(ctx context.Context) (string, error) {
var results strings.Builder
query, err := gojq.Parse(b.queryinput.GetInputValue())
if err != nil {
return "", err
}

highlightedOutput, err := utils.Prettify([]byte(results.String()), b.theme.ChromaStyle, true)
if !b.isJSONLines {
if err := processJSONWithQuery(ctx, &results, query, b.inputdata.GetInputJSON()); err != nil {
return "", err
}
} else {
if err := processJSONLinesWithQuery(ctx, &results, query, b.inputdata.GetInputJSON()); err != nil {
return "", err
}
}
return results.String(), nil

}

func (b *Bubble) executeQueryCommand(ctx context.Context) tea.Cmd {
return func() tea.Msg {
results, err := b.executeQueryOnInput(ctx)
if err != nil {
return errorMsg{error: err}
}
highlightedOutput, err := utils.Prettify([]byte(results), b.theme.ChromaStyle, true)
if err != nil {
return errorMsg{error: err}
}
return queryResultMsg{
rawResults: results.String(),
rawResults: results,
highlightedResults: highlightedOutput.String(),
}
}
Expand All @@ -115,7 +135,7 @@ func (b Bubble) copyOutputToClipboard() tea.Cmd {

func (b Bubble) writeOutputToFile() tea.Cmd {
return func() tea.Msg {
err := os.WriteFile(b.fileselector.GetInput(), []byte(b.results), 0o644)
err := os.WriteFile(b.fileselector.GetInput(), []byte(b.results), 0o600)
if err != nil {
return errorMsg{
error: err,
Expand Down
3 changes: 2 additions & 1 deletion tui/bubbles/jqplayground/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func (b *Bubble) resizeBubbles() {
b.output.SetSize(b.width/2, height)
}

//nolint:revive //will refactor later
func (b Bubble) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var (
cmd tea.Cmd
Expand Down Expand Up @@ -94,7 +95,7 @@ func (b Bubble) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
b.state = state.Running
var ctx context.Context
ctx, b.cancel = context.WithCancel(context.Background())
cmd = b.executeQuery(ctx)
cmd = b.executeQueryCommand(ctx)
cmds = append(cmds, cmd)
}
case tea.KeyCtrlS.String():
Expand Down
7 changes: 0 additions & 7 deletions tui/bubbles/output/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,13 +96,6 @@ func (b Bubble) Update(msg tea.Msg) (Bubble, tea.Cmd) {
cmds []tea.Cmd
)

switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "a":
}
}

b.viewport, cmd = b.viewport.Update(msg)
cmds = append(cmds, cmd)

Expand Down
1 change: 1 addition & 0 deletions tui/bubbles/queryinput/queryinput.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ func (b Bubble) View() string {
return b.Styles.containerStyle.Render(b.textinput.View())
}

//nolint:revive //will refactor later
func (b Bubble) Update(msg tea.Msg) (Bubble, tea.Cmd) {
if msg, ok := msg.(tea.KeyMsg); ok {
switch msg.Type {
Expand Down
12 changes: 6 additions & 6 deletions tui/theme/theme.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ func getDefaultTheme() Theme {
Inactive: GREY,
Success: GREEN,
Error: RED,
ChromaStyle: styles.ParaisoLight,
ChromaStyle: styles.Get("paraiso-light"),
}
if lipgloss.HasDarkBackground() {
theme.ChromaStyle = styles.Vim
theme.ChromaStyle = styles.Get("vim")
}
return theme
}
Expand Down Expand Up @@ -337,21 +337,21 @@ var themeMap = map[string]Theme{
Error: lipgloss.Color("#cd5555"),
ChromaStyle: styles.Get("perldoc"),
},
"paradaiso-dark": {
"paraiso-dark": {
Primary: lipgloss.Color("#48b685"),
Secondary: lipgloss.Color("#5bc4bf"),
Inactive: GREY,
Success: lipgloss.Color("#48b685"),
Error: lipgloss.Color("#ef6155"),
ChromaStyle: styles.Get("paradaiso-dark"),
ChromaStyle: styles.Get("paraiso-dark"),
},
"paradaiso-light": {
"paraiso-light": {
Primary: lipgloss.Color("#48b685"),
Secondary: lipgloss.Color("#815ba4"),
Inactive: GREY,
Success: lipgloss.Color("#48b685"),
Error: lipgloss.Color("#ef6155"),
ChromaStyle: styles.Get("paradaiso-light"),
ChromaStyle: styles.Get("paraiso-light"),
},
"pygments": {
Primary: lipgloss.Color("#008000"),
Expand Down

0 comments on commit 5565fd9

Please sign in to comment.