Skip to content
Snippets Groups Projects
Select Git revision
  • ef8dfc787980e8d43c199a8aec92bbff796b4b12
  • main default protected
  • 3.1.7
  • 3.1.6
  • 3.1.5
  • 3.1.4
  • 3.1.3
  • 3.1.2
  • 3.1.1
  • 3.1.0
  • 2.14.0
  • 2.13.5
  • 2.13.4
  • 2.13.3
  • 2.13.2
  • 2.13.0
  • 2.12.1
  • 2.12.0
  • 2.11.0
  • 2.10.1
  • 2.10.0
  • 2.9.1
22 results

index.js

Blame
  • 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