Compare commits

...

2 Commits

Author SHA1 Message Date
69f90f015e Use GITHUB_TOKEN to access GitHub and avoid access limit
Attempt to get GITHUB_TOKEN from environment variables
2026-04-14 16:16:05 +08:00
c3045d0cfe Only exit with error if folder is non-empty 2026-04-14 16:10:00 +08:00
2 changed files with 59 additions and 28 deletions

39
main.go
View File

@@ -1,6 +1,7 @@
package main package main
import ( import (
"net/http"
"os" "os"
"github.com/google/go-github/v60/github" "github.com/google/go-github/v60/github"
@@ -11,6 +12,17 @@ var (
gitHub *github.Client gitHub *github.Client
) )
// TokenTransport implements http.RoundTripper for Bearer token authentication
type TokenTransport struct {
Token string
Transport http.RoundTripper
}
func (t *TokenTransport) RoundTrip(req *http.Request) (*http.Response, error) {
req.Header.Add("Authorization", "Bearer "+t.Token)
return t.Transport.RoundTrip(req)
}
func printHelp() { func printHelp() {
helpText := `go2spec - A tool to package Go modules into RPM spec files. helpText := `go2spec - A tool to package Go modules into RPM spec files.
@@ -29,13 +41,28 @@ If there are no commands provided, the tool will default to executing the 'pack'
} }
func main() { func main() {
transport := github.BasicAuthTransport{ token := os.Getenv("GITHUB_TOKEN")
Username: os.Getenv("GITHUB_USERNAME"),
Password: os.Getenv("GITHUB_PASSWORD"), var client *http.Client
OTP: os.Getenv("GITHUB_OTP"), if token != "" {
Transport: httpcache.NewMemoryCacheTransport(), // Use token authentication for better rate limits
client = &http.Client{
Transport: &TokenTransport{
Token: token,
Transport: httpcache.NewMemoryCacheTransport(),
},
}
} else {
// Fallback to basic auth if token is not provided
transport := github.BasicAuthTransport{
Username: os.Getenv("GITHUB_USERNAME"),
Password: os.Getenv("GITHUB_PASSWORD"),
OTP: os.Getenv("GITHUB_OTP"),
Transport: httpcache.NewMemoryCacheTransport(),
}
client = transport.Client()
} }
gitHub = github.NewClient(transport.Client()) gitHub = github.NewClient(client)
args := os.Args[1:] args := os.Args[1:]

48
pack.go
View File

@@ -417,8 +417,14 @@ func createDirectory(openRuyiSrc string) (string, error) {
return "", fmt.Errorf("get cwd: %w", err) return "", fmt.Errorf("get cwd: %w", err)
} }
dir := filepath.Join(wd, openRuyiSrc) dir := filepath.Join(wd, openRuyiSrc)
if err := os.Mkdir(dir, 0755); err != nil {
return "", fmt.Errorf("mkdir: %w", err) // Try to create the directory
err = os.Mkdir(dir, 0755)
if err != nil {
// If directory already exists, that's ok (it was verified to be empty)
if !os.IsExist(err) {
return "", fmt.Errorf("mkdir: %w", err)
}
} }
return dir, nil return dir, nil
} }
@@ -663,9 +669,6 @@ func mainPack(args []string, usage func()) {
if pkgType != typeGuess { if pkgType != typeGuess {
openRuyiSrc = nameFromGopkg(gopkg, pkgType, customProgPkgName, allowUnknownHoster) openRuyiSrc = nameFromGopkg(gopkg, pkgType, customProgPkgName, allowUnknownHoster)
if _, err := os.Stat(openRuyiSrc); err == nil {
log.Fatalf("Output directory %q already exists, aborting\n", openRuyiSrc)
}
} }
if strings.ToLower(gopkg) != gopkg { if strings.ToLower(gopkg) != gopkg {
@@ -679,23 +682,7 @@ func mainPack(args []string, usage func()) {
gopkg, strings.ToLower(gopkg)) gopkg, strings.ToLower(gopkg))
} }
info, err := os.Stat(openRuyiSrc) // NOTE: directory existence is checked after determining final openRuyiSrc
if err == nil {
if !info.IsDir() {
log.Fatalf("%q exists but is not a directory\n", openRuyiSrc)
}
entries, err := os.ReadDir(openRuyiSrc)
if err != nil {
log.Fatalf("Failed to read directory %q: %v\n", openRuyiSrc, err)
}
if len(entries) != 0 {
log.Fatalf("Output directory %q exists and is non-empty, aborting\n", openRuyiSrc)
}
} else if !os.IsNotExist(err) {
log.Fatalf("Failed to stat %q: %v\n", openRuyiSrc, err)
}
// Create a tarball of the upstream source // Create a tarball of the upstream source
u, err := makeUpstreamSourceTarball(gopkg, gitRevision, forcePrerelease) u, err := makeUpstreamSourceTarball(gopkg, gitRevision, forcePrerelease)
@@ -713,6 +700,23 @@ func mainPack(args []string, usage func()) {
} }
} }
// Now that we know the final package name, check output directory
info, err := os.Stat(openRuyiSrc)
if err == nil {
if !info.IsDir() {
log.Fatalf("%q exists but is not a directory\n", openRuyiSrc)
}
entries, err := os.ReadDir(openRuyiSrc)
if err != nil {
log.Fatalf("Failed to read directory %q: %v\n", openRuyiSrc, err)
}
if len(entries) != 0 {
log.Fatalf("Output directory %q exists and is non-empty, aborting\n", openRuyiSrc)
}
} else if !os.IsNotExist(err) {
log.Fatalf("Failed to stat %q: %v\n", openRuyiSrc, err)
}
orig := fmt.Sprintf("%s_%s.orig.tar.%s", openRuyiSrc, u.version, u.compression) orig := fmt.Sprintf("%s_%s.orig.tar.%s", openRuyiSrc, u.version, u.compression)
log.Printf("Moving tempfile to %q\n", orig) log.Printf("Moving tempfile to %q\n", orig)
// We need to copy the file, merely renaming is not enough since the file // We need to copy the file, merely renaming is not enough since the file