mirror of
https://github.com/ivabus/pop
synced 2024-12-04 22:15:08 +03:00
feat: completion + version
This commit is contained in:
parent
e445bbc866
commit
3cd260b3c8
1 changed files with 54 additions and 3 deletions
57
main.go
57
main.go
|
@ -4,6 +4,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
|
"runtime/debug"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
tea "github.com/charmbracelet/bubbletea"
|
tea "github.com/charmbracelet/bubbletea"
|
||||||
|
@ -26,9 +27,9 @@ var (
|
||||||
)
|
)
|
||||||
|
|
||||||
var rootCmd = &cobra.Command{
|
var rootCmd = &cobra.Command{
|
||||||
Use: "email",
|
Use: "pop",
|
||||||
Short: "email is a command line interface for sending emails.",
|
Short: "Send emails from your terminal",
|
||||||
Long: `email is a command line interface for sending emails.`,
|
Long: `Pop is a tool for sending emails from your terminal.`,
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
if os.Getenv(RESEND_API_KEY) == "" {
|
if os.Getenv(RESEND_API_KEY) == "" {
|
||||||
fmt.Printf("\n %s %s %s\n\n", errorHeaderStyle.String(), inlineCodeStyle.Render(RESEND_API_KEY), "environment variable is required.")
|
fmt.Printf("\n %s %s %s\n\n", errorHeaderStyle.String(), inlineCodeStyle.Render(RESEND_API_KEY), "environment variable is required.")
|
||||||
|
@ -85,7 +86,42 @@ func hasStdin() bool {
|
||||||
return err == nil && (stat.Mode()&os.ModeCharDevice) == 0
|
return err == nil && (stat.Mode()&os.ModeCharDevice) == 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var (
|
||||||
|
// Version stores the build version of VHS at the time of package through
|
||||||
|
// -ldflags.
|
||||||
|
//
|
||||||
|
// go build -ldflags "-s -w -X=main.Version=$(VERSION)"
|
||||||
|
Version string
|
||||||
|
|
||||||
|
// CommitSHA stores the git commit SHA at the time of package through -ldflags.
|
||||||
|
CommitSHA string
|
||||||
|
)
|
||||||
|
|
||||||
|
var CompletionCmd = &cobra.Command{
|
||||||
|
Use: "completion [bash|zsh|fish|powershell]",
|
||||||
|
Short: "Generate completion script",
|
||||||
|
Long: `To load completions`,
|
||||||
|
DisableFlagsInUseLine: true,
|
||||||
|
ValidArgs: []string{"bash", "zsh", "fish", "powershell"},
|
||||||
|
Args: cobra.MatchAll(cobra.ExactArgs(1), cobra.OnlyValidArgs),
|
||||||
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
|
switch args[0] {
|
||||||
|
case "bash":
|
||||||
|
return rootCmd.GenBashCompletion(os.Stdout)
|
||||||
|
case "zsh":
|
||||||
|
return rootCmd.GenZshCompletion(os.Stdout)
|
||||||
|
case "fish":
|
||||||
|
return rootCmd.GenFishCompletion(os.Stdout, true)
|
||||||
|
case "powershell":
|
||||||
|
return rootCmd.GenPowerShellCompletion(os.Stdout)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
rootCmd.AddCommand(CompletionCmd)
|
||||||
|
|
||||||
rootCmd.Flags().StringSliceVar(&to, "bcc", []string{}, "BCC recipients")
|
rootCmd.Flags().StringSliceVar(&to, "bcc", []string{}, "BCC recipients")
|
||||||
rootCmd.Flags().StringSliceVar(&to, "cc", []string{}, "CC recipients")
|
rootCmd.Flags().StringSliceVar(&to, "cc", []string{}, "CC recipients")
|
||||||
rootCmd.Flags().StringSliceVarP(&attachments, "attach", "a", []string{}, "Email's attachments")
|
rootCmd.Flags().StringSliceVarP(&attachments, "attach", "a", []string{}, "Email's attachments")
|
||||||
|
@ -97,6 +133,21 @@ func init() {
|
||||||
rootCmd.Flags().BoolVarP(&preview, "preview", "p", false, "Whether to preview the email before sending")
|
rootCmd.Flags().BoolVarP(&preview, "preview", "p", false, "Whether to preview the email before sending")
|
||||||
envSignature := os.Getenv("POP_SIGNATURE")
|
envSignature := os.Getenv("POP_SIGNATURE")
|
||||||
rootCmd.Flags().StringVarP(&signature, "signature", "x", envSignature, "Signature to display at the end of the email. "+commentStyle.Render("($"+POP_SIGNATURE+")"))
|
rootCmd.Flags().StringVarP(&signature, "signature", "x", envSignature, "Signature to display at the end of the email. "+commentStyle.Render("($"+POP_SIGNATURE+")"))
|
||||||
|
|
||||||
|
rootCmd.CompletionOptions.HiddenDefaultCmd = true
|
||||||
|
|
||||||
|
if len(CommitSHA) >= 7 { //nolint:gomnd
|
||||||
|
vt := rootCmd.VersionTemplate()
|
||||||
|
rootCmd.SetVersionTemplate(vt[:len(vt)-1] + " (" + CommitSHA[0:7] + ")\n")
|
||||||
|
}
|
||||||
|
if Version == "" {
|
||||||
|
if info, ok := debug.ReadBuildInfo(); ok && info.Main.Sum != "" {
|
||||||
|
Version = info.Main.Version
|
||||||
|
} else {
|
||||||
|
Version = "unknown (built from source)"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
rootCmd.Version = Version
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
Loading…
Reference in a new issue