File: dependency.go

package info (click to toggle)
golang-github-cavaliergopher-rpm 1.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 704 kB
  • sloc: python: 84; makefile: 4
file content (103 lines) | stat: -rw-r--r-- 2,716 bytes parent folder | download
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
package rpm

import (
	"fmt"
)

// Dependency flags indicate how versions comparisons should be computed when
// comparing versions of dependent packages.
const (
	DepFlagAny            = 0
	DepFlagLesser         = (1 << 1)
	DepFlagGreater        = (1 << 2)
	DepFlagEqual          = (1 << 3)
	DepFlagLesserOrEqual  = (DepFlagEqual | DepFlagLesser)
	DepFlagGreaterOrEqual = (DepFlagEqual | DepFlagGreater)
	DepFlagPrereq         = (1 << 6)
	DepFlagScriptPre      = (1 << 9)
	DepFlagScriptPost     = (1 << 10)
	DepFlagScriptPreUn    = (1 << 11)
	DepFlagScriptPostUn   = (1 << 12)
	DepFlagRpmlib         = (1 << 24)
)

// See: https://github.com/rpm-software-management/rpm/blob/master/lib/rpmds.h#L25

// Dependency is an interface which represents a relationship between two
// packages. It might indicate that one package requires, conflicts with,
// obsoletes or provides another package.
//
// Dependency implements the Version interface and so may be used when comparing
// versions.
type Dependency interface {
	Version // Version of the other package

	Name() string // Name of the other package
	Flags() int   // See the DepFlag constants
}

// private basic implementation of a package dependency.
type dependency struct {
	flags   int
	name    string
	epoch   int
	version string
	release string
}

var _ Dependency = &dependency{}

// Flags determines the nature of the package relationship and the comparison
// used for the given version constraint.
func (c *dependency) Flags() int {
	return c.flags
}

// Name is the name of the package target package.
func (c *dependency) Name() string {
	return c.name
}

// Epoch is the epoch constraint of the target package.
func (c *dependency) Epoch() int {
	return c.epoch
}

// Version is the version constraint of the target package.
func (c *dependency) Version() string {
	return c.version
}

// Release is the release constraint of the target package.
func (c *dependency) Release() string {
	return c.release
}

// String returns a string representation of a package dependency in a similar
// format to `rpm -qR`.
func (c *dependency) String() string {
	s := c.name
	switch {
	case DepFlagLesserOrEqual == (c.flags & DepFlagLesserOrEqual):
		s = fmt.Sprintf("%s <=", s)

	case DepFlagLesser == (c.flags & DepFlagLesser):
		s = fmt.Sprintf("%s <", s)

	case DepFlagGreaterOrEqual == (c.flags & DepFlagGreaterOrEqual):
		s = fmt.Sprintf("%s >=", s)

	case DepFlagGreater == (c.flags & DepFlagGreater):
		s = fmt.Sprintf("%s >", s)

	case DepFlagEqual == (c.flags & DepFlagEqual):
		s = fmt.Sprintf("%s =", s)
	}
	if c.version != "" {
		s = fmt.Sprintf("%s %s", s, c.version)
	}
	if c.release != "" {
		s = fmt.Sprintf("%s.%s", s, c.release)
	}
	return s
}