forked from misaka00251/go2spec
Merge pull request 'Fix output format' (#3) from HeliC829/go2spec:master into master
Reviewed-on: misaka00251/go2spec#3
This commit is contained in:
@@ -173,7 +173,7 @@ func pkgVersionFromGit(gitdir string, u *upstream, preferredRev string, forcePre
|
|||||||
u.version = fmt.Sprintf("%s.%s+git%s", u.version, dateStr, lastCommitHash)
|
u.version = fmt.Sprintf("%s.%s+git%s", u.version, dateStr, lastCommitHash)
|
||||||
} else {
|
} else {
|
||||||
// without tag: 0.git20250101.96ee002 96ee0021ea0fb9174681b8004d8deba3c499d7f5
|
// without tag: 0.git20250101.96ee002 96ee0021ea0fb9174681b8004d8deba3c499d7f5
|
||||||
u.version = fmt.Sprintf("0.git%s.%s\n%%define commit_id %s", dateStr, lastCommitHash, fullCommitHash)
|
u.version = fmt.Sprintf("0+git%s.%s\n%%define commit_id %s", dateStr, lastCommitHash, fullCommitHash)
|
||||||
}
|
}
|
||||||
return u.version, nil
|
return u.version, nil
|
||||||
}
|
}
|
||||||
|
|||||||
39
main.go
39
main.go
@@ -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
48
pack.go
@@ -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
|
||||||
|
|||||||
35
spec.go
35
spec.go
@@ -58,17 +58,40 @@ func writeSpec(dir, gopkg, openRuyiSrc, openRuyiLib, openRuyiProgram, version st
|
|||||||
// Macros
|
// Macros
|
||||||
fmt.Fprintf(f, "%%define _name %s\n", upstreamName)
|
fmt.Fprintf(f, "%%define _name %s\n", upstreamName)
|
||||||
fmt.Fprintf(f, "%%define go_import_path %s\n", gopkg)
|
fmt.Fprintf(f, "%%define go_import_path %s\n", gopkg)
|
||||||
|
|
||||||
|
// If pkgVersionFromGit embedded a commit_id define in u.version, extract and write it here
|
||||||
|
commitRe := regexp.MustCompile(`%define\s+commit_id\s+([0-9a-fA-F]+)`)
|
||||||
|
if m := commitRe.FindStringSubmatch(u.version); m != nil {
|
||||||
|
fmt.Fprintf(f, "%%define commit_id %s\n", m[1])
|
||||||
|
// Remove the embedded %define line from version so it doesn't get written to Version: field
|
||||||
|
version = commitRe.ReplaceAllString(version, "")
|
||||||
|
version = strings.TrimSpace(version)
|
||||||
|
}
|
||||||
fmt.Fprintf(f, "\n")
|
fmt.Fprintf(f, "\n")
|
||||||
|
|
||||||
// Header
|
// Header
|
||||||
fmt.Fprintf(f, "Name: %s\n", openRuyiSrc)
|
fmt.Fprintf(f, "Name: %s\n", openRuyiSrc)
|
||||||
|
|
||||||
|
// Some times typeLibrary is treat as typeProgram,
|
||||||
|
// So we add an additional Name line, and keep one of those mannually
|
||||||
|
switch pkgType {
|
||||||
|
case typeProgram:
|
||||||
|
fmt.Fprintf(f, "Name: %s\n", openRuyiLib)
|
||||||
|
}
|
||||||
|
|
||||||
fmt.Fprintf(f, "Version: %s\n", version)
|
fmt.Fprintf(f, "Version: %s\n", version)
|
||||||
fmt.Fprintf(f, "Release: %%autorelease\n")
|
fmt.Fprintf(f, "Release: %%autorelease\n")
|
||||||
fmt.Fprintf(f, "Summary: %s\n", description)
|
fmt.Fprintf(f, "Summary: %s\n", description)
|
||||||
fmt.Fprintf(f, "License: %s\n", license)
|
fmt.Fprintf(f, "License: %s\n", license)
|
||||||
fmt.Fprintf(f, "URL: https://github.com/%s/%s\n", owner, repo)
|
fmt.Fprintf(f, "URL: https://github.com/%s/%s\n", owner, repo)
|
||||||
fmt.Fprintf(f, "#!RemoteAsset\n")
|
fmt.Fprintf(f, "#!RemoteAsset\n")
|
||||||
fmt.Fprintf(f, "Source0: https://github.com/%s/%s/archive/v%%{version}.tar.gz#/%%{_name}-%%{version}.tar.gz\n", owner, repo)
|
// If the computed version text contains a commit_id definition (see pkgVersionFromGit),
|
||||||
|
// use the commit_id tarball instead of v%{version}.tar.gz
|
||||||
|
if strings.Contains(u.version, "commit_id") {
|
||||||
|
fmt.Fprintf(f, "Source0: https://github.com/%s/%s/archive/%%{commit_id}.tar.gz#/%%{_name}-%%{version}.tar.gz\n", owner, repo)
|
||||||
|
} else {
|
||||||
|
fmt.Fprintf(f, "Source0: https://github.com/%s/%s/archive/v%%{version}.tar.gz#/%%{_name}-%%{version}.tar.gz\n", owner, repo)
|
||||||
|
}
|
||||||
|
|
||||||
switch pkgType {
|
switch pkgType {
|
||||||
case typeLibrary:
|
case typeLibrary:
|
||||||
@@ -125,6 +148,7 @@ func writeSpec(dir, gopkg, openRuyiSrc, openRuyiLib, openRuyiProgram, version st
|
|||||||
func writeRPMLibraryPackage(f *os.File, gopkg, openRuyiLib, longdesc string, deps []string) {
|
func writeRPMLibraryPackage(f *os.File, gopkg, openRuyiLib, longdesc string, deps []string) {
|
||||||
fmt.Fprintf(f, "\n")
|
fmt.Fprintf(f, "\n")
|
||||||
fmt.Fprintf(f, "Provides: go(%s) = %%{version}\n", gopkg)
|
fmt.Fprintf(f, "Provides: go(%s) = %%{version}\n", gopkg)
|
||||||
|
fmt.Fprintf(f, "\n")
|
||||||
// 库包的运行时依赖
|
// 库包的运行时依赖
|
||||||
if len(deps) > 0 {
|
if len(deps) > 0 {
|
||||||
for _, dep := range deps {
|
for _, dep := range deps {
|
||||||
@@ -141,10 +165,13 @@ func writeRPMLibrarySubpackage(f *os.File, gopkg, openRuyiLib, openRuyiSrc, long
|
|||||||
fmt.Fprintf(f, "Summary: Development files of %s\n", filepath.Base(gopkg))
|
fmt.Fprintf(f, "Summary: Development files of %s\n", filepath.Base(gopkg))
|
||||||
fmt.Fprintf(f, "Provides: go(%s) = %%{version}\n", gopkg)
|
fmt.Fprintf(f, "Provides: go(%s) = %%{version}\n", gopkg)
|
||||||
fmt.Fprintf(f, "BuildArch: noarch\n")
|
fmt.Fprintf(f, "BuildArch: noarch\n")
|
||||||
for _, dep := range deps {
|
if len(deps) > 0 {
|
||||||
fmt.Fprintf(f, "Requires: %s\n", dep)
|
for _, dep := range deps {
|
||||||
|
fmt.Fprintf(f, "Requires: %s\n", dep)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
fmt.Fprintf(f, "\n")
|
||||||
}
|
}
|
||||||
fmt.Fprintf(f, "\n")
|
|
||||||
fmt.Fprintf(f, "%%description -n %s\n", openRuyiLib)
|
fmt.Fprintf(f, "%%description -n %s\n", openRuyiLib)
|
||||||
fmt.Fprintf(f, "%s\n", longdesc)
|
fmt.Fprintf(f, "%s\n", longdesc)
|
||||||
fmt.Fprintf(f, "\n")
|
fmt.Fprintf(f, "\n")
|
||||||
|
|||||||
Reference in New Issue
Block a user