satisfy full linter

This commit is contained in:
2026-07-07 23:45:14 +02:00
parent b8e0ee074c
commit 002a390f43
47 changed files with 1206 additions and 1171 deletions

View File

@@ -26,6 +26,7 @@ import (
"errors"
"fmt"
"io"
"log"
"log/slog"
"net/http"
"os"
@@ -92,7 +93,6 @@ func ApiRepl(conf *cfg.Config) error {
EOFPrompt: "exit",
HistorySearchFold: true,
})
if err != nil {
return fmt.Errorf("failed to initialize readline lib: %w", err)
}
@@ -112,11 +112,13 @@ func ApiRepl(conf *cfg.Config) error {
parts := strings.SplitN(strings.TrimSpace(text), " ", 3)
if len(parts) < 2 {
fmt.Println("error: you need to input a verb, uri [and post data]")
continue
}
if !slices.Contains(verbs, strings.ToLower(parts[0])) {
fmt.Println("error: verb must be one of " + strings.Join(verbs, ","))
continue
}
@@ -144,6 +146,7 @@ func ApiRepl(conf *cfg.Config) error {
pageJsonOutput(conf, raw)
}
//nolint:nilerr
return nil
}
@@ -175,7 +178,6 @@ func pageJsonOutput(conf *cfg.Config, raw []byte) {
pager.Stderr = os.Stderr
err := pager.Run()
if err != nil {
fmt.Printf("failed to execute pager '%s': %s", conf.Pager, err)
}
@@ -197,16 +199,16 @@ func CallAPI(conf *cfg.Config, verb, path, data string) ([]byte, error) {
verb = strings.ToUpper(verb)
// we're using port-forwards anyway
tr := &http.Transport{
noVerifyTransport := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}
client := &http.Client{Transport: tr}
client := &http.Client{Transport: noVerifyTransport}
if conf.DebugHTTP {
client = &http.Client{
Transport: &cfg.DebugTransport{
Transport: tr}}
Transport: noVerifyTransport}}
}
req, err := http.NewRequest(verb, conf.DefaultCluster.Uri+path, bytes.NewBuffer([]byte(data)))
@@ -215,7 +217,7 @@ func CallAPI(conf *cfg.Config, verb, path, data string) ([]byte, error) {
}
req.Header.Add("Content-Type", "application/json")
req.Header.Add("accept", "application/json")
req.Header.Add("Accept", "application/json")
// make sure we have got all we need
if err := conf.DefaultCluster.CheckAuth(); err != nil {
@@ -233,6 +235,11 @@ func CallAPI(conf *cfg.Config, verb, path, data string) ([]byte, error) {
if err != nil {
return nil, err
}
defer func() {
if err := resp.Body.Close(); err != nil {
log.Fatal(err)
}
}()
// Read and print response
body, err := io.ReadAll(resp.Body)
@@ -249,6 +256,7 @@ func prettyfiJson(conf *cfg.Config, raw []byte) (string, error) {
cmd.Stdin = bytes.NewReader(raw)
var out bytes.Buffer
cmd.Stdout = &out
err := cmd.Run()
@@ -258,7 +266,9 @@ func prettyfiJson(conf *cfg.Config, raw []byte) (string, error) {
return out.String(), nil
}
var pretty bytes.Buffer
err := json.Indent(&pretty, raw, "", "\t")
if err != nil {
return "", fmt.Errorf("json parse error: %w", err)
@@ -270,10 +280,10 @@ func prettyfiJson(conf *cfg.Config, raw []byte) (string, error) {
// interactively read arbitrary JSON data from STDIN, which is
// virtually a repl inside the primary repl
func readJSON(input string) (string, error) {
data := ""
data := strings.Builder{}
if input != "" {
data = input
data.WriteString(input)
} else {
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
@@ -283,23 +293,23 @@ func readJSON(input string) (string, error) {
break
}
data += line
data.WriteString(line)
}
}
if data == "" {
return data, nil
if data.Len() == 0 {
return "", nil
}
// validate
check := map[string]any{}
err := json.Unmarshal([]byte(data), &check)
err := json.Unmarshal([]byte(data.String()), &check)
if err != nil {
return "", fmt.Errorf("error: input data is not proper JSON: %w", err)
}
return data, nil
return data.String(), nil
}
func ApiList(conf *cfg.Config, pattern string) error {
@@ -338,6 +348,7 @@ func ApiList(conf *cfg.Config, pattern string) error {
}
table.Sort()
return table.Print()
}
@@ -345,10 +356,13 @@ func ApiList(conf *cfg.Config, pattern string) error {
func ApiPathNames() []string {
assets.LoadAssetOpenApi()
paths := []string{}
paths := make([]string, len(assets.OpenAPI.Spec().Paths.Paths))
idx := 0
for path := range assets.OpenAPI.Spec().Paths.Paths {
paths = append(paths, path)
paths[idx] = path
idx++
}
return paths
@@ -356,6 +370,7 @@ func ApiPathNames() []string {
func ApiShow(conf *cfg.Config, showpath, verb string) error {
assets.LoadAssetOpenApi()
out := printer.Builder{}
op, err := matchOperation(showpath, verb)
@@ -372,18 +387,20 @@ func ApiShow(conf *cfg.Config, showpath, verb string) error {
sample := getApiExample(op)
description := markdown.Render(cleanMarkup.ReplaceAllString(op.Op.Description, ""), width, cfg.DefaultMargin)
var bold = lipgloss.NewStyle().
Bold(true)
var paragraph = lipgloss.NewStyle().
MarginBottom(1).
MarginLeft(cfg.DefaultMargin)
var boldparagraph = lipgloss.NewStyle().
MarginBottom(1).
MarginLeft(cfg.DefaultMargin).
Bold(true)
var indentparagraph = lipgloss.NewStyle().
MarginBottom(1).
MarginLeft(2)
var (
bold = lipgloss.NewStyle().
Bold(true)
paragraph = lipgloss.NewStyle().
MarginBottom(1).
MarginLeft(cfg.DefaultMargin)
boldparagraph = lipgloss.NewStyle().
MarginBottom(1).
MarginLeft(cfg.DefaultMargin).
Bold(true)
indentparagraph = lipgloss.NewStyle().
MarginBottom(1).
MarginLeft(2)
)
out.WriteStringLine(bold.Render("ID: " + op.Op.ID))
out.WriteStringLine(paragraph.Render(fmt.Sprintf("%s %s", op.Verb, showpath)))
@@ -399,10 +416,11 @@ func ApiShow(conf *cfg.Config, showpath, verb string) error {
if len(params.Path) > 0 {
out.WriteStringLine(bold.Render("Path Parameters"))
for _, param := range params.Path {
out.WriteStringLine(boldparagraph.Render(param.Param))
if param.Description != "" {
//out.WriteStringLine(indentparagraph.Render(param.Description))
out.WriteStringLine(param.Description)
}
}
@@ -410,8 +428,10 @@ func ApiShow(conf *cfg.Config, showpath, verb string) error {
if len(params.Query) > 0 {
out.WriteStringLine(bold.Render("Query Parameters"))
for _, param := range params.Query {
out.WriteStringLine(boldparagraph.Render(param.Param))
if param.Description != "" {
out.WriteStringLine(indentparagraph.Render(param.Description))
}
@@ -493,10 +513,8 @@ func getApiExample(op *Op) string {
if hassource {
sample = strings.TrimSpace(source.(string))
}
}
}
}
}
@@ -509,11 +527,13 @@ func getApiExample(op *Op) string {
func matchOperation(showpath, verb string) (*Op, error) {
ops := []*Op{}
op := &Op{}
var found bool
for path, item := range assets.OpenAPI.Spec().Paths.Paths {
if path == showpath {
ops = findOperation(item.PathItemProps)
break
}
}
@@ -530,6 +550,7 @@ func matchOperation(showpath, verb string) (*Op, error) {
if strings.ToLower(item.Verb) == verb {
op = item
found = true
break
}
}