main.go 3.55 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
Mustafa Gezen's avatar
Mustafa Gezen committed
29
30
31
)

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

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

	sourceRpmLocation := ""
	if strings.HasPrefix(sourceRpm, "file://") {
		sourceRpmLocation = strings.TrimPrefix(sourceRpm, "file://")
58
		importer = &internal.SrpmMode{}
59
	} else {
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
		sourceRpmLocation = fmt.Sprintf("%s/%s", rpmPrefix, sourceRpm)
		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)
76
77
78
	}

	internal.ProcessRPM(&internal.ProcessData{
79
		Importer:          importer,
80
81
82
83
		RpmLocation:       sourceRpmLocation,
		UpstreamPrefix:    upstreamPrefix,
		SshKeyLocation:    sshKeyLocation,
		SshUser:           sshUser,
84
85
		Version:           version,
		BlobStorage:       blobStorage,
86
87
		GitCommitterName:  gitCommitterName,
		GitCommitterEmail: gitCommitterEmail,
88
89
		ModulePrefix:      modulePrefix,
		Authenticator:     authenticator,
90
	})
Mustafa Gezen's avatar
Mustafa Gezen committed
91
92
93
}

func main() {
94
95
96
97
	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")
98
99
100
101
	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")
102

103
104
	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")
105
106
107
108
	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")
109
110
111
112

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