Select Git revision
-
Sigmund, Dominik authoredSigmund, Dominik authored
utils.go 1.66 KiB
package utils
import (
"bufio"
"fmt"
"os"
"os/exec"
"os/signal"
"regexp"
"strings"
"github.com/manifoldco/promptui"
)
// MenuOption represents an option in the menu
type MenuOption struct {
Display string
Value string
}
// CheckGit checks if Git is installed and available in PATH
func CheckGit() bool {
_, err := exec.LookPath("git")
return err == nil
}
// Slugify converts a string into a slug suitable for filenames and URLs
func Slugify(name string) string {
// Convert to lowercase, remove non-alphanumeric characters, and replace spaces with hyphens
re := regexp.MustCompile("[^a-z0-9]+")
slug := re.ReplaceAllString(strings.ToLower(name), "-")
slug = strings.Trim(slug, "-")
return slug
}
// PromptIfEmpty prompts the user for input if the given variable is empty
func PromptIfEmpty(value *string, prompt string) {
if *value == "" {
reader := bufio.NewReader(os.Stdin)
fmt.Print(prompt)
input, _ := reader.ReadString('\n')
*value = strings.TrimSpace(input)
}
}
// SelectMenu displays a menu with options and returns the selected value
func SelectMenu(options []MenuOption, prompt string) (string, error) {
// Handle interrupt signal (Ctrl+C)
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt)
go func() {
<-c
fmt.Println("\nAborted by user")
os.Exit(1)
}()
promptItems := make([]string, len(options))
for i, option := range options {
promptItems[i] = option.Display
}
promptUI := promptui.Select{
Label: prompt,
Items: promptItems,
}
_, result, err := promptUI.Run()
if err != nil {
return "", err