Skip to content

Commit b96bf69

Browse files
committed
fix: make shebang executor actually execute all shebangs
1 parent b3802b4 commit b96bf69

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

pkg/executors/shebang_executor.go

+19-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,25 @@ func (n ShebangExecutor) Execute(file *github.File, gist *github.Gist, ctx conte
3535
return err
3636
}
3737

38-
cmd := exec.Command("sh", "-c", content)
38+
dfile, err := os.CreateTemp("", "ghs")
39+
if err != nil {
40+
return err
41+
}
42+
defer os.Remove(dfile.Name())
43+
44+
// write content to file and make executable
45+
_, err = dfile.Write([]byte(content))
46+
if err != nil {
47+
return err
48+
}
49+
50+
err = dfile.Chmod(0777)
51+
if err != nil {
52+
return err
53+
}
54+
55+
// run file as executable
56+
cmd := exec.Command(dfile.Name())
3957
cmd.Stdout = os.Stdout
4058
cmd.Stderr = os.Stderr
4159
cmd.Stdin = os.Stdin

pkg/gist.go

+11-3
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,14 @@ import (
55
"context"
66
"encoding/json"
77
"errors"
8+
"fmt"
89
"io"
910
"net/http"
1011
"os/exec"
12+
"strings"
1113

1214
"github.com/charmbracelet/log"
15+
"github.com/cli/go-gh/v2"
1316
"github.com/flexwie/ghs/pkg/executors"
1417
"github.com/flexwie/ghs/pkg/github"
1518
)
@@ -19,7 +22,7 @@ func SearchForGistFile(url string, gistName string, ctx context.Context) (*githu
1922

2023
req, err := http.NewRequest("GET", url, nil)
2124
if token != "" {
22-
req.Header.Add("Authorization", token)
25+
req.Header.Add("Authorization", fmt.Sprintf("Bearer %s", token))
2326
}
2427

2528
resp, err := http.DefaultClient.Do(req)
@@ -47,6 +50,13 @@ func SearchForGistFile(url string, gistName string, ctx context.Context) (*githu
4750
}
4851

4952
func getGithubToken() string {
53+
x, _, err := gh.Exec("auth", "token")
54+
if err != nil {
55+
log.Warn("unauthorized")
56+
}
57+
58+
return strings.TrimSuffix(x.String(), "\n")
59+
5060
cmd := exec.Command("which", "gh")
5161
if err := cmd.Run(); err != nil {
5262
log.Warn("GitHub cli is not installed. Your private gists will not be found.")
@@ -70,8 +80,6 @@ func getGithubToken() string {
7080

7181
func GetExecutor(file *github.File, ctx context.Context) (executors.Executor, error) {
7282
for _, e := range executors.ExecutorPipeline {
73-
file := ctx.Value("file").(*github.File)
74-
7583
if e.Match(file) {
7684
return e, nil
7785
}

0 commit comments

Comments
 (0)