Skip to content

Commit

Permalink
fix: lint issues
Browse files Browse the repository at this point in the history
Signed-off-by: Carlos Alexandro Becker <[email protected]>
  • Loading branch information
caarlos0 committed Sep 6, 2024
1 parent 25fa085 commit b5adfc9
Show file tree
Hide file tree
Showing 13 changed files with 62 additions and 54 deletions.
2 changes: 1 addition & 1 deletion ansi.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"

"github.com/beevik/etree"
"github.com/charmbracelet/x/exp/term/ansi"
"github.com/charmbracelet/x/ansi"
"github.com/mattn/go-runewidth"
)

Expand Down
11 changes: 5 additions & 6 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ func expandPadding(p []float64, scale float64) []float64 {
return []float64{p[top] * scale, p[right] * scale, p[bottom] * scale, p[left] * scale}
default:
return []float64{0, 0, 0, 0}

}
}

Expand All @@ -102,7 +101,7 @@ const (
var userConfigPath = filepath.Join(xdg.ConfigHome, "freeze", "user.json")

func loadUserConfig() (fs.File, error) {
return os.Open(userConfigPath)
return os.Open(userConfigPath) //nolint: wrapcheck
}

func saveUserConfig(config Config) error {
Expand All @@ -112,19 +111,19 @@ func saveUserConfig(config Config) error {

err := os.MkdirAll(filepath.Dir(userConfigPath), os.ModePerm)
if err != nil {
return err
return err //nolint: wrapcheck
}
f, err := os.Create(userConfigPath)
if err != nil {
return err
return err //nolint: wrapcheck
}
b, err := json.Marshal(config)
if err != nil {
return err
return err //nolint: wrapcheck
}
_, err = f.Write(b)

printFilenameOutput(userConfigPath)

return err
return err //nolint: wrapcheck
}
1 change: 0 additions & 1 deletion cut_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,4 @@ func TestCut(t *testing.T) {
t.Errorf("cut(%s, %v)", test.input, test.lines)
}
}

}
6 changes: 4 additions & 2 deletions error.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ import (
"github.com/charmbracelet/lipgloss"
)

var errorHeader = lipgloss.NewStyle().Foreground(lipgloss.Color("#F1F1F1")).Background(lipgloss.Color("#FF5F87")).Bold(true).Padding(0, 1).Margin(1).MarginLeft(2).SetString("ERROR")
var errorDetails = lipgloss.NewStyle().Foreground(lipgloss.Color("#757575")).Margin(0, 0, 1, 2)
var (
errorHeader = lipgloss.NewStyle().Foreground(lipgloss.Color("#F1F1F1")).Background(lipgloss.Color("#FF5F87")).Bold(true).Padding(0, 1).Margin(1).MarginLeft(2).SetString("ERROR")
errorDetails = lipgloss.NewStyle().Foreground(lipgloss.Color("#757575")).Margin(0, 0, 1, 2)
)

func printError(title string, err error) {
fmt.Printf("%s\n", lipgloss.JoinHorizontal(lipgloss.Center, errorHeader.String(), title))
Expand Down
9 changes: 7 additions & 2 deletions font/font.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,5 +105,10 @@ var JetBrainsMonoTTF []byte
//go:embed JetBrainsMonoNL-Regular.ttf
var JetBrainsMonoNLTTF []byte

var JetBrainsMono string = base64.StdEncoding.EncodeToString(JetBrainsMonoTTF)
var JetBrainsMonoNL string = base64.StdEncoding.EncodeToString(JetBrainsMonoNLTTF)
var (
// JetBrainsMono font.
JetBrainsMono = base64.StdEncoding.EncodeToString(JetBrainsMonoTTF)

// JetBrainsMonoNL font.
JetBrainsMonoNL = base64.StdEncoding.EncodeToString(JetBrainsMonoNLTTF)
)
14 changes: 8 additions & 6 deletions freeze_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ import (

const binary = "./test/freeze-test"

var update = flag.Bool("update", false, "update golden files")
var png = flag.Bool("png", false, "update pngs")
var (
update = flag.Bool("update", false, "update golden files")
png = flag.Bool("png", false, "update pngs")
)

func TestMain(m *testing.M) {
flag.Parse()
Expand Down Expand Up @@ -264,15 +266,15 @@ func TestFreezeConfigurations(t *testing.T) {
if err != nil {
t.Fatal("unable to remove output files")
}
err = os.MkdirAll("test/output/svg", 0755)
err = os.MkdirAll("test/output/svg", 0o755)
if err != nil {
t.Fatal("unable to create output directory")
}
err = os.MkdirAll("test/golden/svg", 0755)
err = os.MkdirAll("test/golden/svg", 0o755)
if err != nil {
t.Fatal("unable to create output directory")
}
err = os.MkdirAll("test/output/png", 0755)
err = os.MkdirAll("test/output/png", 0o755)
if err != nil {
t.Fatal("unable to create output directory")
}
Expand All @@ -299,7 +301,7 @@ func TestFreezeConfigurations(t *testing.T) {
}
goldenfile := "test/golden/svg/" + tc.output + ".svg"
if *update {
if err := os.WriteFile(goldenfile, got, 0644); err != nil {
if err := os.WriteFile(goldenfile, got, 0o644); err != nil {
t.Log(err)
t.Fatal("unexpected error")
}
Expand Down
1 change: 0 additions & 1 deletion help.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ func printFlag(f *kong.Flag) {
fmt.Print(" ", dashStyle.Render(" "), " ")
fmt.Print(dashStyle.Render("--"), f.Name)
fmt.Print(strings.Repeat(" ", space-len(f.Name)))

}
help := highlighter.ReplaceAllString(f.Help, keywordStyle.Render("$1")+"\x1b[38;5;"+helpForeground+"m")
fmt.Println(helpStyle.Render(help))
Expand Down
19 changes: 10 additions & 9 deletions interactive.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,18 @@ func runForm(config *Config) (*Config, error) {

theme := huh.ThemeCharm()
theme.FieldSeparator = lipgloss.NewStyle()
theme.Blurred.TextInput.Text = theme.Blurred.TextInput.Text.Copy().Foreground(lipgloss.Color("243"))
theme.Blurred.TextInput.Text = theme.Blurred.TextInput.Text.Foreground(lipgloss.Color("243"))
theme.Blurred.BlurredButton = lipgloss.NewStyle().Foreground(lipgloss.Color("8")).PaddingRight(1)
theme.Blurred.FocusedButton = lipgloss.NewStyle().Foreground(lipgloss.Color("7")).PaddingRight(1)
theme.Focused.BlurredButton = lipgloss.NewStyle().Foreground(lipgloss.Color("8")).PaddingRight(1)
theme.Focused.FocusedButton = lipgloss.NewStyle().Foreground(lipgloss.Color("15")).PaddingRight(1)
theme.Focused.NoteTitle = theme.Focused.NoteTitle.Copy().Margin(1, 0)
theme.Blurred.NoteTitle = theme.Blurred.NoteTitle.Copy().Margin(1, 0)
theme.Blurred.Description = theme.Blurred.Description.Copy().Foreground(lipgloss.Color("0"))
theme.Focused.Description = theme.Focused.Description.Copy().Foreground(lipgloss.Color("7"))
theme.Blurred.Title = theme.Blurred.Title.Copy().Width(18).Foreground(lipgloss.Color("7"))
theme.Focused.Title = theme.Focused.Title.Copy().Width(18).Foreground(green).Bold(true)
theme.Blurred.SelectedOption = theme.Blurred.SelectedOption.Copy().Foreground(lipgloss.Color("243"))
theme.Focused.NoteTitle = theme.Focused.NoteTitle.Margin(1, 0)
theme.Blurred.NoteTitle = theme.Blurred.NoteTitle.Margin(1, 0)
theme.Blurred.Description = theme.Blurred.Description.Foreground(lipgloss.Color("0"))
theme.Focused.Description = theme.Focused.Description.Foreground(lipgloss.Color("7"))
theme.Blurred.Title = theme.Blurred.Title.Width(18).Foreground(lipgloss.Color("7"))
theme.Focused.Title = theme.Focused.Title.Width(18).Foreground(green).Bold(true)
theme.Blurred.SelectedOption = theme.Blurred.SelectedOption.Foreground(lipgloss.Color("243"))
theme.Focused.SelectedOption = lipgloss.NewStyle().Foreground(green)
theme.Focused.Base.BorderForeground(green)

Expand Down Expand Up @@ -197,7 +197,7 @@ func runForm(config *Config) (*Config, error) {
config.Shadow.Blur, _ = strconv.ParseFloat(shadowBlur, 64)
config.Shadow.X, _ = strconv.ParseFloat(shadowX, 64)
config.Shadow.Y, _ = strconv.ParseFloat(shadowY, 64)
return config, err
return config, err //nolint: wrapcheck
}

func validateMargin(s string) error {
Expand Down Expand Up @@ -256,6 +256,7 @@ func validateColor(s string) error {
}

func parsePadding(v string) []float64 {
//nolint: prealloc
var values []float64
for _, p := range strings.Fields(v) {
pi, _ := strconv.ParseFloat(p, 64) // already validated
Expand Down
6 changes: 3 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ import (
"github.com/charmbracelet/freeze/svg"
"github.com/charmbracelet/lipgloss"
"github.com/charmbracelet/log"
"github.com/charmbracelet/x/exp/term/ansi"
"github.com/charmbracelet/x/exp/term/ansi/parser"
"github.com/charmbracelet/x/ansi"
"github.com/charmbracelet/x/ansi/parser"
"github.com/mattn/go-isatty"
"github.com/muesli/reflow/wordwrap"
)
Expand Down Expand Up @@ -60,9 +60,9 @@ func main() {
printErrorFatal("Invalid Usage", err)
}

//nolint: nestif
if len(ctx.Args) > 0 {
switch ctx.Args[0] {

case "version":
if Version == "" {
if info, ok := debug.ReadBuildInfo(); ok && info.Main.Sum != "" {
Expand Down
30 changes: 15 additions & 15 deletions png.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,42 +11,42 @@ import (
"github.com/kanrichan/resvg-go"
)

func libsvgConvert(doc *etree.Document, w, h float64, output string) error {
func libsvgConvert(doc *etree.Document, _, _ float64, output string) error {
_, err := exec.LookPath("rsvg-convert")
if err != nil {
return err
return err //nolint: wrapcheck
}

svg, err := doc.WriteToBytes()
if err != nil {
return err
return err //nolint: wrapcheck
}

// rsvg-convert is installed use that to convert the SVG to PNG,
// since it is faster.
rsvgConvert := exec.Command("rsvg-convert", "-o", output)
rsvgConvert.Stdin = bytes.NewReader(svg)
err = rsvgConvert.Run()
return err
return err //nolint: wrapcheck
}

func resvgConvert(doc *etree.Document, w, h float64, output string) error {
svg, err := doc.WriteToBytes()
if err != nil {
return err
return err //nolint: wrapcheck
}

worker, err := resvg.NewDefaultWorker(context.Background())
if err != nil {
printErrorFatal("Unable to write output", err)
}
defer worker.Close()
defer worker.Close() //nolint: errcheck

fontdb, err := worker.NewFontDBDefault()
if err != nil {
printErrorFatal("Unable to write output", err)
}
defer fontdb.Close()
defer fontdb.Close() //nolint: errcheck
err = fontdb.LoadFontData(font.JetBrainsMonoTTF)
if err != nil {
printErrorFatal("Unable to load font", err)
Expand All @@ -61,7 +61,7 @@ func resvgConvert(doc *etree.Document, w, h float64, output string) error {
printError("Unable to write output", err)
os.Exit(1)
}
defer pixmap.Close()
defer pixmap.Close() //nolint: errcheck

tree, err := worker.NewTreeFromData(svg, &resvg.Options{
Dpi: 192,
Expand All @@ -75,24 +75,24 @@ func resvgConvert(doc *etree.Document, w, h float64, output string) error {
printError("Unable to write output", err)
os.Exit(1)
}
defer tree.Close()
defer tree.Close() //nolint: errcheck

err = tree.ConvertText(fontdb)
if err != nil {
return err
return err //nolint: wrapcheck
}
err = tree.Render(resvg.TransformIdentity(), pixmap)
if err != nil {
return err
return err //nolint: wrapcheck
}
png, err := pixmap.EncodePNG()
if err != nil {
return err
return err //nolint: wrapcheck
}

err = os.WriteFile(output, png, 0644)
err = os.WriteFile(output, png, 0o600)
if err != nil {
return err
return err //nolint: wrapcheck
}
return err
return err //nolint: wrapcheck
}
9 changes: 5 additions & 4 deletions pty.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
// The returned file is the pty's file descriptor and must be closed by the
// caller.
func (cfg Config) runInPty(c *exec.Cmd) (*os.File, error) {
//nolint: wrapcheck
return pty.StartWithAttrs(c, &pty.Winsize{
Cols: 80,
Rows: 10,
Expand All @@ -29,25 +30,25 @@ func (cfg Config) runInPty(c *exec.Cmd) (*os.File, error) {
func executeCommand(config Config) (string, error) {
args, err := shellwords.Parse(config.Execute)
if err != nil {
return "", err
return "", err //nolint: wrapcheck
}
ctx, cancel := context.WithTimeout(context.Background(), config.ExecuteTimeout)
defer cancel()

cmd := exec.CommandContext(ctx, args[0], args[1:]...)
cmd := exec.CommandContext(ctx, args[0], args[1:]...) //nolint: gosec
pty, err := config.runInPty(cmd)
if err != nil {
return "", err
}
defer pty.Close()
defer pty.Close() //nolint: errcheck
var out bytes.Buffer
go func() {
_, _ = io.Copy(&out, pty)
}()

err = cmd.Wait()
if err != nil {
return "", err
return "", err //nolint: wrapcheck
}
return out.String(), nil
}
4 changes: 2 additions & 2 deletions svg/svg.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func AddCornerRadius(e *etree.Element, radius float64) {
e.CreateAttr("ry", fmt.Sprintf("%.2f", radius))
}

// Move moves the given element to the (x, y) position
// Move moves the given element to the (x, y) position.
func Move(e *etree.Element, x, y float64) {
e.CreateAttr("x", fmt.Sprintf("%.2fpx", x))
e.CreateAttr("y", fmt.Sprintf("%.2fpx", y))
Expand Down Expand Up @@ -113,7 +113,7 @@ func GetDimensions(element *etree.Element) (int, int) {
}

// dimensionToInt takes a string and returns the integer value.
// e.g. "500px" -> 500
// e.g. "500px" -> 500.
func dimensionToInt(px string) int {
d := strings.TrimSuffix(px, "px")
v, _ := strconv.ParseInt(d, 10, 64)
Expand Down
4 changes: 2 additions & 2 deletions test/input/tab.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package main

// freeze/issues/50

type Config struct {
type Config struct { //nolint: revive
Telegram struct {
Token string `env:"TG_TOKEN"`
ChatID string `env:"TG_CHAT"`
Expand All @@ -20,7 +20,7 @@ type Config struct {
Debug bool
}

func Load() (*Config, error) {
func Load() (*Config, error) { //nolint: revive
var c Config
var err error
return &c, err
Expand Down

0 comments on commit b5adfc9

Please sign in to comment.