main.go 3.96 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
	sourceRpm         string
	sshKeyLocation    string
	sshUser           string
	upstreamPrefix    string
23
24
	version           int
	storageAddr       string
25
26
	gitCommitterName  string
	gitCommitterEmail string
27
28
	modulePrefix      string
	rpmPrefix         string
29
	noDupMode         bool
Mustafa Gezen's avatar
Mustafa Gezen committed
30
	moduleMode        bool
Mustafa Gezen's avatar
Mustafa Gezen committed
31
32
33
)

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

func mn(_ *cobra.Command, _ []string) {
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
	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")
55
56
57
58
59
	}

	sourceRpmLocation := ""
	if strings.HasPrefix(sourceRpm, "file://") {
		sourceRpmLocation = strings.TrimPrefix(sourceRpm, "file://")
60
		importer = &internal.SrpmMode{}
61
	} else {
Mustafa Gezen's avatar
Mustafa Gezen committed
62
63
64
65
66
		if moduleMode {
			sourceRpmLocation = fmt.Sprintf("%s/%s", modulePrefix, sourceRpm)
		} else {
			sourceRpmLocation = fmt.Sprintf("%s/%s", rpmPrefix, sourceRpm)
		}
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
		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)
82
83
84
	}

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

func main() {
102
103
104
105
	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")
106
107
108
109
	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")
110

111
112
	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")
113
114
115
116
	root.Flags().StringVar(&gitCommitterName, "git-committer-name", "distrobuild-bot", "Name of committer")
	root.Flags().StringVar(&gitCommitterEmail, "git-committer-email", "mustafa+distrobuild@bycrates.com", "Email of committer")
	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")
117
	root.Flags().BoolVar(&noDupMode, "no-dup-mode", false, "If enabled, skips already imported tags")
Mustafa Gezen's avatar
Mustafa Gezen committed
118
	root.Flags().BoolVar(&moduleMode, "module-mode", false, "If enabled, imports a module instead of a package")
119
120
121
122

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