main.go 3.7 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
31
32
)

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

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

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

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

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

105
106
	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")
107
108
109
110
	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")
111
	root.Flags().BoolVar(&noDupMode, "no-dup-mode", false, "If enabled, skips already imported tags")
112
113
114
115

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