Skip to content
Myr

main.go

This file

Path

main.go

90 lines · 2.2 KiB · Go

Last changed by

chore: prepare v2 (#1116)Drew Smirnoffbefore adoption02d6f06

History of this file

Take it

1// Package main is Gum: a tool for glamorous shell scripts.2package main34import (5	"errors"6	"fmt"7	"os"8	"runtime/debug"910	tea "charm.land/bubbletea/v2"11	"charm.land/gum/v2/internal/exit"12	"charm.land/lipgloss/v2"13	"github.com/alecthomas/kong"14)1516const shaLen = 71718const defaultBool = "false"1920var (21	// Version contains the application version number. It's set via ldflags22	// when building.23	Version = ""2425	// CommitSHA contains the SHA of the commit that this application was built26	// against. It's set via ldflags when building.27	CommitSHA = ""28)2930var bubbleGumPink = lipgloss.NewStyle().Foreground(lipgloss.Color("212"))3132func main() {33	if Version == "" {34		if info, ok := debug.ReadBuildInfo(); ok && info.Main.Sum != "" {35			Version = info.Main.Version36		} else {37			Version = "unknown (built from source)"38		}39	}40	version := fmt.Sprintf("gum version %s", Version)41	if len(CommitSHA) >= shaLen {42		version += " (" + CommitSHA[:shaLen] + ")"43	}4445	gum := &Gum{}46	ctx := kong.Parse(47		gum,48		kong.Description(fmt.Sprintf("A tool for %s shell scripts.", bubbleGumPink.Render("glamorous"))),49		kong.UsageOnError(),50		kong.ConfigureHelp(kong.HelpOptions{51			Compact:             true,52			Summary:             false,53			NoExpandSubcommands: true,54		}),55		kong.Vars{56			"version":                 version,57			"versionNumber":           Version,58			"defaultHeight":           "0",59			"defaultWidth":            "0",60			"defaultAlign":            "left",61			"defaultBorder":           "none",62			"defaultBorderForeground": "",63			"defaultBorderBackground": "",64			"defaultBackground":       "",65			"defaultForeground":       "",66			"defaultMargin":           "0 0",67			"defaultPadding":          "0 0",68			"defaultUnderline":        defaultBool,69			"defaultBold":             defaultBool,70			"defaultFaint":            defaultBool,71			"defaultItalic":           defaultBool,72			"defaultStrikethrough":    defaultBool,73		},74	)75	if err := ctx.Run(); err != nil {76		var ex exit.ErrExit77		if errors.As(err, &ex) {78			os.Exit(int(ex))79		}80		if errors.Is(err, tea.ErrInterrupted) {81			os.Exit(exit.StatusAborted)82		}83		if errors.Is(err, tea.ErrProgramKilled) {84			fmt.Fprintln(os.Stderr, "timed out")85			os.Exit(exit.StatusTimeout)86		}87		fmt.Fprintln(os.Stderr, err)88		os.Exit(1)89	}90}