forked from misaka00251/go2spec
Compare commits
2 Commits
c4cf02616a
...
69f90f015e
| Author | SHA1 | Date | |
|---|---|---|---|
| 69f90f015e | |||
| c3045d0cfe |
39
main.go
39
main.go
@@ -1,6 +1,7 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"os"
|
||||
|
||||
"github.com/google/go-github/v60/github"
|
||||
@@ -11,6 +12,17 @@ var (
|
||||
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() {
|
||||
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() {
|
||||
transport := github.BasicAuthTransport{
|
||||
Username: os.Getenv("GITHUB_USERNAME"),
|
||||
Password: os.Getenv("GITHUB_PASSWORD"),
|
||||
OTP: os.Getenv("GITHUB_OTP"),
|
||||
Transport: httpcache.NewMemoryCacheTransport(),
|
||||
token := os.Getenv("GITHUB_TOKEN")
|
||||
|
||||
var client *http.Client
|
||||
if token != "" {
|
||||
// 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:]
|
||||
|
||||
|
||||
48
pack.go
48
pack.go
@@ -417,8 +417,14 @@ func createDirectory(openRuyiSrc string) (string, error) {
|
||||
return "", fmt.Errorf("get cwd: %w", err)
|
||||
}
|
||||
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
|
||||
}
|
||||
@@ -663,9 +669,6 @@ func mainPack(args []string, usage func()) {
|
||||
|
||||
if pkgType != typeGuess {
|
||||
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 {
|
||||
@@ -679,23 +682,7 @@ func mainPack(args []string, usage func()) {
|
||||
gopkg, strings.ToLower(gopkg))
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
// NOTE: directory existence is checked after determining final openRuyiSrc
|
||||
|
||||
// Create a tarball of the upstream source
|
||||
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)
|
||||
log.Printf("Moving tempfile to %q\n", orig)
|
||||
// We need to copy the file, merely renaming is not enough since the file
|
||||
|
||||
Reference in New Issue
Block a user