Mist3rBru / go-clack

Effortlessly build beautiful command-line apps
MIT License
3 stars 0 forks source link

Spinner input #2

Open JustSteveKing opened 1 month ago

JustSteveKing commented 1 month ago

I am using the spinner atm to run a shell command, and it seems to hang. I know that the command asks for my root password to run - is there something I need to do to allow the spinner to do this?

Mist3rBru commented 1 month ago

Hey @JustSteveKing, thanks for filling this issue!

Could you please provide an snippet so I can reproduce it?

JustSteveKing commented 1 month ago

Here it is @Mist3rBru

package cmd

import (
    "fmt"
    "os"
    "os/exec"

    "github.com/Mist3rBru/go-clack/prompts"
    "github.com/spf13/cobra"
)

var rebuildCmd = &cobra.Command{
    Use:   "rebuild",
    Short: "Rebuild nix config for mac.",
    Run: func(cmd *cobra.Command, args []string) {
        prompts.Intro("Let's rebuild your nix configuration.")

        homeDir, _ := os.UserHomeDir()

        s := prompts.Spinner(prompts.SpinnerOptions{})
        s.Start("Rebuilding nix config using flake 'main'")

        command := exec.Command("darwin-rebuild", "switch", "--flake", homeDir+"/.config/nix#main")
        command.Stdin = os.Stdin
        command.Stdout = os.Stdout
        command.Stderr = os.Stderr

        if err := command.Run(); err != nil {
            s.Stop(fmt.Sprintf("Something went wrong: %v", err), 1)
        }
        s.Stop("All good", 0)

        prompts.Outro("Nix configuration rebuilt.")
    },
}

func init() {
    rootCmd.AddCommand(rebuildCmd)
}
JustSteveKing commented 1 month ago

Hey @Mist3rBru did you have any thoughts on this at all?

Mist3rBru commented 1 month ago

I looked into it, and it seems the OS treats the request for permission and the command execution as a single process, so we can't separate them in this way.

Some possible workarounds would be to request permission to switch to the root (sudo) user, then run a spinner while the command is executed, but this isn't a good user experience. Or just run it without the spinner.

Otherwise, I don't see a viable solution. I would consider this a known limitation and suggest keeping the issue open.

JustSteveKing commented 1 month ago

Thanks for looking into it @Mist3rBru