Skip to content

Commit

Permalink
enhancement: make it a real library and drop srpm mode
Browse files Browse the repository at this point in the history
  • Loading branch information
mstg committed Sep 3, 2021
1 parent 7d84156 commit 23bea74
Show file tree
Hide file tree
Showing 11 changed files with 371 additions and 475 deletions.
128 changes: 22 additions & 106 deletions cmd/srpmproc/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,8 @@
package main

import (
"fmt"
"github.com/rocky-linux/srpmproc/pkg/srpmproc"
"log"
"os"
"os/user"
"path/filepath"
"strings"

"github.com/go-git/go-billy/v5"
"github.com/go-git/go-billy/v5/memfs"
"github.com/go-git/go-billy/v5/osfs"
"github.com/go-git/go-git/v5/plumbing/transport/ssh"
"github.com/rocky-linux/srpmproc/pkg/blob"
"github.com/rocky-linux/srpmproc/pkg/blob/file"
"github.com/rocky-linux/srpmproc/pkg/blob/gcs"
"github.com/rocky-linux/srpmproc/pkg/blob/s3"
"github.com/rocky-linux/srpmproc/pkg/data"

"github.com/spf13/cobra"
)
Expand All @@ -62,7 +47,6 @@ var (
noStorageDownload bool
noStorageUpload bool
manualCommits string
upstreamPrefixHttps string
moduleFallbackStream string
allowStreamBranches bool
)
Expand All @@ -73,104 +57,37 @@ var root = &cobra.Command{
}

func mn(_ *cobra.Command, _ []string) {
switch version {
case 8:
break
default:
log.Fatalf("unsupported upstream version %d", version)
}

var importer data.ImportMode
var blobStorage blob.Storage

if strings.HasPrefix(storageAddr, "gs://") {
blobStorage = gcs.New(strings.Replace(storageAddr, "gs://", "", 1))
} else if strings.HasPrefix(storageAddr, "s3://") {
blobStorage = s3.New(strings.Replace(storageAddr, "s3://", "", 1))
} else if strings.HasPrefix(storageAddr, "file://") {
blobStorage = file.New(strings.Replace(storageAddr, "file://", "", 1))
} else {
log.Fatalf("invalid blob storage")
}

sourceRpmLocation := ""
if strings.HasPrefix(sourceRpm, "file://") {
sourceRpmLocation = strings.TrimPrefix(sourceRpm, "file://")
importer = &srpmproc.SrpmMode{}
} else {
if moduleMode {
sourceRpmLocation = fmt.Sprintf("%s/%s", modulePrefix, sourceRpm)
} else {
sourceRpmLocation = fmt.Sprintf("%s/%s", rpmPrefix, sourceRpm)
}
importer = &srpmproc.GitMode{}
}

lastKeyLocation := sshKeyLocation
if lastKeyLocation == "" {
usr, err := user.Current()
if err != nil {
log.Fatalf("could not get user: %v", err)
}
lastKeyLocation = filepath.Join(usr.HomeDir, ".ssh/id_rsa")
}

var authenticator *ssh.PublicKeys

var err error
// create ssh key authenticator
authenticator, err = ssh.NewPublicKeysFromFile(sshUser, lastKeyLocation, "")
if err != nil {
log.Fatalf("could not get git authenticator: %v", err)
}

fsCreator := func(branch string) billy.Filesystem {
return memfs.New()
}

if tmpFsMode != "" {
log.Printf("using tmpfs dir: %s", tmpFsMode)
fsCreator = func(branch string) billy.Filesystem {
tmpDir := filepath.Join(tmpFsMode, branch)
err := os.MkdirAll(tmpDir, 0755)
if err != nil {
log.Fatalf("could not create tmpfs dir: %v", err)
}
return osfs.New(tmpDir)
}
}

var manualCs []string
if strings.TrimSpace(manualCommits) != "" {
manualCs = strings.Split(manualCommits, ",")
}

srpmproc.ProcessRPM(&data.ProcessData{
Importer: importer,
RpmLocation: sourceRpmLocation,
UpstreamPrefix: upstreamPrefix,
pd, err := srpmproc.NewProcessData(&srpmproc.ProcessDataRequest{
Version: version,
StorageAddr: storageAddr,
Package: sourceRpm,
ModuleMode: moduleMode,
TmpFsMode: tmpFsMode,
ModulePrefix: modulePrefix,
RpmPrefix: rpmPrefix,
SshKeyLocation: sshKeyLocation,
SshUser: sshUser,
Version: version,
BlobStorage: blobStorage,
ManualCommits: manualCommits,
UpstreamPrefix: upstreamPrefix,
GitCommitterName: gitCommitterName,
GitCommitterEmail: gitCommitterEmail,
ModulePrefix: modulePrefix,
ImportBranchPrefix: importBranchPrefix,
BranchPrefix: branchPrefix,
SingleTag: singleTag,
Authenticator: authenticator,
NoDupMode: noDupMode,
ModuleMode: moduleMode,
TmpFsMode: tmpFsMode,
NoStorageDownload: noStorageDownload,
NoStorageUpload: noStorageUpload,
ManualCommits: manualCs,
UpstreamPrefixHttps: upstreamPrefixHttps,
ModuleFallbackStream: moduleFallbackStream,
AllowStreamBranches: allowStreamBranches,
FsCreator: fsCreator,
ModuleFallbackStream: moduleFallbackStream,
NoStorageUpload: noStorageUpload,
NoStorageDownload: noStorageDownload,
SingleTag: singleTag,
})
if err != nil {
log.Fatal(err)
}

err = srpmproc.ProcessRPM(pd)
if err != nil {
log.Fatal(err)
}
}

func main() {
Expand Down Expand Up @@ -198,7 +115,6 @@ func main() {
root.Flags().BoolVar(&noStorageDownload, "no-storage-download", false, "If enabled, blobs are always downloaded from upstream")
root.Flags().BoolVar(&noStorageUpload, "no-storage-upload", false, "If enabled, blobs are not uploaded to blob storage")
root.Flags().StringVar(&manualCommits, "manual-commits", "", "Comma separated branch and commit list for packages with broken release tags (Format: BRANCH:HASH)")
root.Flags().StringVar(&upstreamPrefixHttps, "upstream-prefix-https", "", "Web version of upstream prefix. Required if module-mode")
root.Flags().StringVar(&moduleFallbackStream, "module-fallback-stream", "", "Override fallback stream. Some module packages are published as collections and mostly use the same stream name, some of them deviate from the main stream")
root.Flags().BoolVar(&allowStreamBranches, "allow-stream-branches", false, "Allow import from stream branches")

Expand Down
9 changes: 4 additions & 5 deletions pkg/data/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,22 +21,21 @@
package data

import (
"github.com/cavaliercoder/go-rpm"
"github.com/go-git/go-git/v5"
"hash"
)

type ImportMode interface {
RetrieveSource(pd *ProcessData) *ModeData
WriteSource(pd *ProcessData, md *ModeData)
PostProcess(md *ModeData)
RetrieveSource(pd *ProcessData) (*ModeData, error)
WriteSource(pd *ProcessData, md *ModeData) error
PostProcess(md *ModeData) error
ImportName(pd *ProcessData, md *ModeData) string
}

type ModeData struct {
Name string
Repo *git.Repository
Worktree *git.Worktree
RpmFile *rpm.PackageFile
FileWrites map[string][]byte
TagBranch string
PushBranch string
Expand Down
5 changes: 3 additions & 2 deletions pkg/data/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import (
"github.com/rocky-linux/srpmproc/pkg/blob"
)

type FsCreatorFunc func(branch string) (billy.Filesystem, error)

type ProcessData struct {
RpmLocation string
UpstreamPrefix string
Expand All @@ -48,8 +50,7 @@ type ProcessData struct {
NoStorageDownload bool
NoStorageUpload bool
ManualCommits []string
UpstreamPrefixHttps string
ModuleFallbackStream string
AllowStreamBranches bool
FsCreator func(branch string) billy.Filesystem
FsCreator FsCreatorFunc
}
2 changes: 1 addition & 1 deletion pkg/directives/spec_change.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ func specChange(cfg *srpmprocpb.Cfg, pd *data.ProcessData, md *data.ModeData, _
hasPatch := false

version := ""
importName := strings.Replace(pd.Importer.ImportName(pd, md), md.RpmFile.Name(), "1", 1)
importName := strings.Replace(pd.Importer.ImportName(pd, md), md.Name, "1", 1)
importNameSplit := strings.SplitN(importName, "-", 2)
if len(importNameSplit) == 2 {
versionSplit := strings.SplitN(importNameSplit[1], ".el", 2)
Expand Down
14 changes: 14 additions & 0 deletions pkg/misc/regex.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package misc

import (
"fmt"
"regexp"
)

func GetTagImportRegex(importBranchPrefix string, allowStreamBranches bool) *regexp.Regexp {
if allowStreamBranches {
return regexp.MustCompile(fmt.Sprintf("refs/tags/(imports/(%s(?:.s|.)|%s(?:|s).+)/(.*))", importBranchPrefix, importBranchPrefix))
} else {
return regexp.MustCompile(fmt.Sprintf("refs/tags/(imports/(%s.|%s.-.+)/(.*))", importBranchPrefix, importBranchPrefix))
}
}
Loading

0 comments on commit 23bea74

Please sign in to comment.