summaryrefslogtreecommitdiff
path: root/readControlFile.go
blob: b53a79dbce3f1fcba1980e5a30e85cdefecf9646 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package main

import (
	"bufio"
	"os"
	"strings"
	"unicode"

	"go.wit.com/lib/protobuf/gitpb"
	"go.wit.com/log"
)

func trimNonNumericPrefix(s string) string {
	// Find the index of the first character that IS a digit.
	firstDigitIndex := strings.IndexFunc(s, unicode.IsDigit)

	// If no digit is found, IndexFunc returns -1.
	// In this case, the result should be an empty string.
	if firstDigitIndex == -1 {
		return ""
	}

	// Return the substring starting from the first digit.
	return s[firstDigitIndex:]
}

// readGitConfig reads and parses the control file
func readControlFile(repo *gitpb.Repo) error {
	pairs := make(map[string]string)
	var key string

	file, err := os.Open("control")
	if err != nil {
		log.Warn("readControlFile() could not find the file")
		// return errors.New("'control': file not found")
		// if this happens, make up a fake control file
		pairs["Maintainer"] = "go-deb build"
		pairs["Architecture"] = "amd64" // TODO: figure this out
		pairs["Recommends"] = ""
		pairs["Source"] = "notsure"
		if me.repo == nil {
			pairs["Description"] = "put something here"
		} else {
			pairs["Description"] = me.repo.GetGoPath()
		}
	}
	defer file.Close()

	pairs["Version"] = trimNonNumericPrefix(repo.GetCurrentVersion())

	scanner := bufio.NewScanner(file)
	for scanner.Scan() {
		line := scanner.Text()

		// Skip empty lines and comments
		if line == "" || strings.HasPrefix(line, "#") || strings.HasPrefix(line, ";") {
			continue
		}

		// if line starts with a space, it's part of the last key
		if strings.HasPrefix(line, " ") {
			pairs[key] = pairs[key] + "\n" + strings.TrimSpace(line)
			continue
		}

		partsNew := strings.SplitN(line, ":", 2)
		if len(partsNew) < 2 {
			log.Warn("error on line:", line)
			continue
		}

		key = strings.TrimSpace(partsNew[0])
		value := strings.TrimSpace(partsNew[1])
		pairs[key] = value
	}
	if repo.Control == nil {
		repo.Control = make(map[string]string)
	}
	for key, value := range pairs {
		repo.Control[key] = value
		/*
			switch key {
			case "Source":
				c.Source.SetText(value)
			case "Build-Depends":
				c.BuildDepends.SetText(value)
			case "Description":
				c.Description.SetText(value)
			case "Maintainer":
				c.Maintainer.SetText(value)
			case "Packager":
				c.Packager.SetText(value)
			case "GoPath":
				c.GoPath.SetText(value)
			case "URL":
				c.URL.SetText(value)
			case "Depends":
				c.Depends.SetText(value)
			case "Recommends":
				c.Recommends.SetText(value)
			case "Conflicts":
				c.Conflicts.SetText(value)
			case "Version":
				c.Version.SetText(value)
			case "Package":
				c.Package.SetText(value)
				// if c.Package.String() != value {
				// 	log.Warn("not sure what to do with Package", c.Package.String(), value)
				// }
			case "Architecture":
				// todo: add logic to find OS arch
				if c.Architecture.String() != value {
					log.Warn("attempting to set arch to", value)
					c.Architecture.SetText(value)

				}
			default:
				log.Warn("the 'control' file has a value I don't know about")
				log.Warn("error unknown key", key, "value:", value)
			}
		*/
	}

	if err := scanner.Err(); err != nil {
		return err
	}

	return nil
}