Skip to content

Instantly share code, notes, and snippets.

@DanStough
Created July 11, 2025 01:11
Show Gist options
  • Select an option

  • Save DanStough/bd95fe0b4ce411e6688bc5d89aba3539 to your computer and use it in GitHub Desktop.

Select an option

Save DanStough/bd95fe0b4ce411e6688bc5d89aba3539 to your computer and use it in GitHub Desktop.

Revisions

  1. DanStough created this gist Jul 11, 2025.
    38 changes: 38 additions & 0 deletions main.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,38 @@
    package main

    import (
    "context"
    "io"
    "os"

    "github.com/charmbracelet/fang"
    "github.com/spf13/cobra"
    )

    func main() {
    // NewRootCMD creates the root command for the epok CLI application.
    rootCmd := &cobra.Command{
    Use: "test",
    RunE: handler,
    }

    fang.Execute(context.Background(), rootCmd,
    fang.WithNotifySignal(os.Interrupt, os.Kill))
    }

    func handler(cmd *cobra.Command, _ []string) error {
    data := make(chan string)

    go func() {
    in := cmd.InOrStdin()
    _, _ = io.ReadAll(in) // Will block
    }()

    ctx := cmd.Context()
    select {
    case <-ctx.Done():
    return ctx.Err()
    case <-data:
    return nil
    }
    }