File: pkg.go

package info (click to toggle)
golang-github-tredoe-osutil 1.5.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 476 kB
  • sloc: makefile: 4
file content (112 lines) | stat: -rw-r--r-- 2,399 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
104
105
106
107
108
109
110
111
112
// Copyright 2012 Jonas mg
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

// Package pkgutil handles basic operations in the management of packages in
// operating systems.
//
// Important
//
// If you are going to use a package manager different to Deb, then you should
// check the options since I cann't test all.
//
// TODO
//
// Add managers of BSD systems.
//
// Use flag to do not show questions.
package pkgutil

import (
	"errors"
	"os/exec"
)

// Packager is the common interface to handle different package systems.
type Packager interface {
	// Install installs packages.
	Install(name ...string) error

	// Remove removes packages.
	Remove(name ...string) error

	// Purge removes packages and its configuration files.
	Purge(name ...string) error

	// Update resynchronizes the package index files from their sources.
	Update() error

	// Upgrade upgrades all the packages on the system.
	Upgrade() error

	// Clean erases both packages downloaded and orphaned dependencies.
	Clean() error
}

// PackageType represents a package management system.
type PackageType int8

const (
	// Linux
	Deb PackageType = iota + 1
	RPM
	Pacman
	Ebuild
	ZYpp
)

func (pkg PackageType) String() string {
	switch pkg {
	case Deb:
		return "Deb"
	case RPM:
		return "RPM"
	case Pacman:
		return "Pacman"
	case Ebuild:
		return "Ebuild"
	case ZYpp:
		return "ZYpp"
	}
	panic("unreachable")
}

// New returns the interface to handle the package manager.
func New(pkg PackageType) Packager {
	switch pkg {
	case Deb:
		return new(deb)
	case RPM:
		return new(rpm)
	case Pacman:
		return new(pacman)
	case Ebuild:
		return new(ebuild)
	case ZYpp:
		return new(zypp)
	}
	panic("unreachable")
}

// execPackagers is a list of executables of package managers.
var execPackagers = [...]string{
	Deb:    "apt-get",
	RPM:    "yum",
	Pacman: "pacman",
	Ebuild: "emerge",
	ZYpp:   "zypper",
}

// Detect tries to get the package system used in the system, looking for
// executables in directory "/usr/bin".
func Detect() (PackageType, error) {
	for k, v := range execPackagers {
		_, err := exec.LookPath("/usr/bin/" + v)
		if err == nil {
			return PackageType(k), nil
		}
	}
	return -1, errors.New("package manager not found in directory '/usr/bin'")
}