replace.go 2.83 KB
Newer Older
Mustafa Gezen's avatar
Mustafa Gezen committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// Copyright (c) 2021 The Srpmproc Authors
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

Mustafa Gezen's avatar
Mustafa Gezen committed
21
22
23
package directives

import (
24
25
	"errors"
	"fmt"
26
	"git.rockylinux.org/release-engineering/public/srpmproc/internal/data"
27
	srpmprocpb "git.rockylinux.org/release-engineering/public/srpmproc/pb"
Mustafa Gezen's avatar
Mustafa Gezen committed
28
29
30
31
32
	"github.com/go-git/go-git/v5"
	"io/ioutil"
	"os"
)

33
func replace(cfg *srpmprocpb.Cfg, _ *data.ProcessData, _ *data.ModeData, patchTree *git.Worktree, pushTree *git.Worktree) error {
Mustafa Gezen's avatar
Mustafa Gezen committed
34
35
36
37
	for _, replace := range cfg.Replace {
		filePath := checkAddPrefix(replace.File)
		stat, err := pushTree.Filesystem.Stat(filePath)
		if replace.File == "" || err != nil {
38
			return errors.New(fmt.Sprintf("INVALID_FILE:%s", filePath))
Mustafa Gezen's avatar
Mustafa Gezen committed
39
40
41
42
		}

		err = pushTree.Filesystem.Remove(filePath)
		if err != nil {
43
			return errors.New(fmt.Sprintf("COULD_NOT_REMOVE_OLD_FILE:%s", filePath))
Mustafa Gezen's avatar
Mustafa Gezen committed
44
45
46
47
		}

		f, err := pushTree.Filesystem.OpenFile(filePath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, stat.Mode())
		if err != nil {
48
			return errors.New(fmt.Sprintf("COULD_NOT_OPEN_REPLACEMENT:%s", filePath))
Mustafa Gezen's avatar
Mustafa Gezen committed
49
50
51
52
53
54
		}

		switch replacing := replace.Replacing.(type) {
		case *srpmprocpb.Replace_WithFile:
			fPatch, err := patchTree.Filesystem.OpenFile(replacing.WithFile, os.O_RDONLY, 0644)
			if err != nil {
55
				return errors.New(fmt.Sprintf("COULD_NOT_OPEN_REPLACING:%s", replacing.WithFile))
Mustafa Gezen's avatar
Mustafa Gezen committed
56
57
58
59
			}

			replacingBytes, err := ioutil.ReadAll(fPatch)
			if err != nil {
60
				return errors.New(fmt.Sprintf("COULD_NOT_READ_REPLACING:%s", replacing.WithFile))
Mustafa Gezen's avatar
Mustafa Gezen committed
61
62
63
64
			}

			_, err = f.Write(replacingBytes)
			if err != nil {
65
				return errors.New(fmt.Sprintf("COULD_NOT_WRITE_REPLACING:%s", replacing.WithFile))
Mustafa Gezen's avatar
Mustafa Gezen committed
66
67
68
69
70
			}
			break
		case *srpmprocpb.Replace_WithInline:
			_, err := f.Write([]byte(replacing.WithInline))
			if err != nil {
71
				return errors.New(fmt.Sprintf("COULD_NOT_WRITE_INLINE:%s", filePath))
Mustafa Gezen's avatar
Mustafa Gezen committed
72
73
74
75
			}
			break
		}
	}
76
77

	return nil
Mustafa Gezen's avatar
Mustafa Gezen committed
78
}