1
1
package pkg
2
2
3
3
import (
4
+ "bytes"
4
5
"context"
5
6
"encoding/json"
6
7
"errors"
7
8
"io"
8
9
"net/http"
10
+ "os/exec"
9
11
12
+ "github.com/charmbracelet/log"
10
13
"github.com/flexwie/ghs/pkg/executors"
11
14
"github.com/flexwie/ghs/pkg/github"
12
15
)
@@ -15,7 +18,14 @@ func SearchForGist(ctx context.Context) (*github.Gist, *github.File, error) {
15
18
url := ctx .Value ("apiUrl" ).(string )
16
19
gistName := ctx .Value ("gist" ).(string )
17
20
18
- resp , err := http .Get (url )
21
+ token := getGithubToken ()
22
+
23
+ req , err := http .NewRequest ("GET" , url , nil )
24
+ if token != "" {
25
+ req .Header .Add ("Authorization" , token )
26
+ }
27
+
28
+ resp , err := http .DefaultClient .Do (req )
19
29
if err != nil {
20
30
return nil , nil , err
21
31
}
@@ -39,6 +49,28 @@ func SearchForGist(ctx context.Context) (*github.Gist, *github.File, error) {
39
49
return nil , nil , errors .New ("unable to find requested gist" )
40
50
}
41
51
52
+ func getGithubToken () string {
53
+ cmd := exec .Command ("which" , "gh" )
54
+ if err := cmd .Run (); err != nil {
55
+ log .Warn ("GitHub cli is not installed. Your private gists will not be found." )
56
+ return ""
57
+ }
58
+
59
+ cmd = exec .Command ("gh" , "auth" , "token" )
60
+ writer := new (bytes.Buffer )
61
+ cmd .Stdout = writer
62
+
63
+ errOut := new (bytes.Buffer )
64
+ cmd .Stderr = errOut
65
+
66
+ if err := cmd .Run (); err != nil {
67
+ log .Warn ("GitHub cli authentication failed. Your private gists will not be found." , "err" , errOut .String ())
68
+ return ""
69
+ }
70
+
71
+ return writer .String ()
72
+ }
73
+
42
74
func GetExecutor (ctx context.Context ) (executors.Executor , error ) {
43
75
for _ , e := range executors .ExecutorPipeline {
44
76
file := ctx .Value ("file" ).(* github.File )
0 commit comments