Sam
81da49093d
All checks were successful
continuous-integration/drone/push Build is passing
Instead of copying the opts struct every time it gets passed around, it should be created once and passed through reference. This should reduce memory utilization, unfortunately I cannot test it since this program runs so fast pprof won't report anything useful. I think I found all of them 🙂 Co-authored-by: Sam Therapy <sam@samtherapy.net> Reviewed-on: #132 Reviewed-by: grumbulon <grumbulon@grumbulon.xyz>
70 lines
1.4 KiB
Go
70 lines
1.4 KiB
Go
// SPDX-License-Identifier: BSD-3-Clause
|
|
//go:build !js
|
|
|
|
package main
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"os"
|
|
"strings"
|
|
|
|
cli "git.froth.zone/sam/awl/cmd"
|
|
"git.froth.zone/sam/awl/pkg/query"
|
|
"git.froth.zone/sam/awl/pkg/util"
|
|
)
|
|
|
|
var version = "DEV"
|
|
|
|
func main() {
|
|
if opts, code, err := run(os.Args); err != nil {
|
|
// TODO: Make not ew
|
|
if errors.Is(err, cli.ErrNotError) || strings.Contains(err.Error(), "help requested") {
|
|
os.Exit(0)
|
|
} else {
|
|
opts.Logger.Error(err)
|
|
os.Exit(code)
|
|
}
|
|
}
|
|
}
|
|
|
|
func run(args []string) (opts *util.Options, code int, err error) {
|
|
opts, err = cli.ParseCLI(args, version)
|
|
if err != nil {
|
|
return opts, 1, fmt.Errorf("parse: %w", err)
|
|
}
|
|
|
|
var resp util.Response
|
|
|
|
// Retry queries if a query fails
|
|
for i := 0; i <= opts.Request.Retries; i++ {
|
|
resp, err = query.CreateQuery(opts)
|
|
if err == nil {
|
|
break
|
|
} else if i != opts.Request.Retries {
|
|
opts.Logger.Warn("Retrying request, error:", err)
|
|
}
|
|
}
|
|
|
|
// Query failed, make it fail
|
|
if err != nil {
|
|
return opts, 9, fmt.Errorf("query: %w", err)
|
|
}
|
|
|
|
var str string
|
|
if opts.JSON || opts.XML || opts.YAML {
|
|
str, err = query.PrintSpecial(resp, opts)
|
|
if err != nil {
|
|
return opts, 10, fmt.Errorf("format print: %w", err)
|
|
}
|
|
} else {
|
|
str, err = query.ToString(resp, opts)
|
|
if err != nil {
|
|
return opts, 15, fmt.Errorf("standard print: %w", err)
|
|
}
|
|
}
|
|
|
|
fmt.Println(str)
|
|
|
|
return opts, 0, nil
|
|
}
|