github.com/nektos/act@v0.2.83/pkg/gh/gh.go (about)

     1  package gh
     2  
     3  import (
     4  	"bufio"
     5  	"bytes"
     6  	"context"
     7  	"os/exec"
     8  )
     9  
    10  func GetToken(ctx context.Context, workingDirectory string) (string, error) {
    11  	var token string
    12  
    13  	// Locate the 'gh' executable
    14  	path, err := exec.LookPath("gh")
    15  	if err != nil {
    16  		return "", err
    17  	}
    18  
    19  	// Command setup
    20  	cmd := exec.CommandContext(ctx, path, "auth", "token")
    21  	cmd.Dir = workingDirectory
    22  
    23  	// Capture the output
    24  	var out bytes.Buffer
    25  	cmd.Stdout = &out
    26  
    27  	// Run the command
    28  	err = cmd.Run()
    29  	if err != nil {
    30  		return "", err
    31  	}
    32  
    33  	// Read the first line of the output
    34  	scanner := bufio.NewScanner(&out)
    35  	if scanner.Scan() {
    36  		token = scanner.Text()
    37  	}
    38  
    39  	return token, nil
    40  }