main.go 4.32 KB
Newer Older
Mustafa Gezen's avatar
Mustafa Gezen committed
1
2
3
package main

import (
4
5
6
7
8
	"fmt"
	"github.com/go-git/go-git/v5/plumbing/transport/ssh"
	"github.com/mstg/srpmproc/internal/blob"
	"github.com/mstg/srpmproc/internal/blob/gcs"
	"github.com/mstg/srpmproc/internal/blob/s3"
9
	"log"
10
11
	"os/user"
	"path/filepath"
12
	"strings"
Mustafa Gezen's avatar
Mustafa Gezen committed
13

14
15
	"github.com/mstg/srpmproc/internal"
	"github.com/spf13/cobra"
Mustafa Gezen's avatar
Mustafa Gezen committed
16
17
18
)

var (
19
20
21
22
23
24
25
26
27
28
29
30
31
32
	sourceRpm          string
	sshKeyLocation     string
	sshUser            string
	upstreamPrefix     string
	version            int
	storageAddr        string
	gitCommitterName   string
	gitCommitterEmail  string
	modulePrefix       string
	rpmPrefix          string
	importBranchPrefix string
	branchPrefix       string
	noDupMode          bool
	moduleMode         bool
Mustafa Gezen's avatar
Mustafa Gezen committed
33
34
35
)

var root = &cobra.Command{
36
37
	Use: "srpmproc",
	Run: mn,
Mustafa Gezen's avatar
Mustafa Gezen committed
38
39
40
}

func mn(_ *cobra.Command, _ []string) {
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
	switch version {
	case 8:
		break
	default:
		log.Fatalf("unsupported upstream version %d", version)
	}

	var importer internal.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 {
		log.Fatalf("invalid blob storage")
57
58
59
60
61
	}

	sourceRpmLocation := ""
	if strings.HasPrefix(sourceRpm, "file://") {
		sourceRpmLocation = strings.TrimPrefix(sourceRpm, "file://")
62
		importer = &internal.SrpmMode{}
63
	} else {
Mustafa Gezen's avatar
Mustafa Gezen committed
64
65
66
67
68
		if moduleMode {
			sourceRpmLocation = fmt.Sprintf("%s/%s", modulePrefix, sourceRpm)
		} else {
			sourceRpmLocation = fmt.Sprintf("%s/%s", rpmPrefix, sourceRpm)
		}
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
		importer = &internal.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")
	}
	// create ssh key authenticator
	authenticator, err := ssh.NewPublicKeysFromFile(sshUser, lastKeyLocation, "")
	if err != nil {
		log.Fatalf("could not get git authenticator: %v", err)
84
85
86
	}

	internal.ProcessRPM(&internal.ProcessData{
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
		Importer:           importer,
		RpmLocation:        sourceRpmLocation,
		UpstreamPrefix:     upstreamPrefix,
		SshKeyLocation:     sshKeyLocation,
		SshUser:            sshUser,
		Version:            version,
		BlobStorage:        blobStorage,
		GitCommitterName:   gitCommitterName,
		GitCommitterEmail:  gitCommitterEmail,
		ModulePrefix:       modulePrefix,
		ImportBranchPrefix: importBranchPrefix,
		BranchPrefix:       branchPrefix,
		Authenticator:      authenticator,
		NoDupMode:          noDupMode,
		ModuleMode:         moduleMode,
102
	})
Mustafa Gezen's avatar
Mustafa Gezen committed
103
104
105
}

func main() {
106
107
108
109
	root.Flags().StringVar(&sourceRpm, "source-rpm", "", "Location of RPM to process")
	_ = root.MarkFlagRequired("source-rpm")
	root.Flags().StringVar(&upstreamPrefix, "upstream-prefix", "", "Upstream git repository prefix")
	_ = root.MarkFlagRequired("upstream-prefix")
110
111
112
113
	root.Flags().IntVar(&version, "version", 0, "Upstream version")
	_ = root.MarkFlagRequired("version")
	root.Flags().StringVar(&storageAddr, "storage-addr", "", "Bucket to use as blob storage")
	_ = root.MarkFlagRequired("storage-addr")
114

115
116
	root.Flags().StringVar(&sshKeyLocation, "ssh-key-location", "", "Location of the SSH key to use to authenticate against upstream")
	root.Flags().StringVar(&sshUser, "ssh-user", "git", "SSH User")
117
118
	root.Flags().StringVar(&gitCommitterName, "git-committer-name", "rockyautomation", "Name of committer")
	root.Flags().StringVar(&gitCommitterEmail, "git-committer-email", "rockyautomation@rockylinux.org", "Email of committer")
119
120
	root.Flags().StringVar(&modulePrefix, "module-prefix", "https://git.centos.org/modules", "Where to retrieve modules if exists. Only used when source-rpm is a git repo")
	root.Flags().StringVar(&rpmPrefix, "rpm-prefix", "https://git.centos.org/rpms", "Where to retrieve SRPM content. Only used when source-rpm is not a local file")
121
122
	root.Flags().StringVar(&importBranchPrefix, "import-branch-prefix", "c", "Import branch prefix")
	root.Flags().StringVar(&branchPrefix, "branch-prefix", "r", "Branch prefix (replaces import-branch-prefix)")
123
	root.Flags().BoolVar(&noDupMode, "no-dup-mode", false, "If enabled, skips already imported tags")
Mustafa Gezen's avatar
Mustafa Gezen committed
124
	root.Flags().BoolVar(&moduleMode, "module-mode", false, "If enabled, imports a module instead of a package")
125
126
127
128

	if err := root.Execute(); err != nil {
		log.Fatal(err)
	}
Mustafa Gezen's avatar
Mustafa Gezen committed
129
}