deb

package
v0.0.0-...-b076d91 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 21, 2014 License: Apache-2.0 Imports: 17 Imported by: 7

Documentation

Overview

Package deb provides the building blocks for reading and writing deb files.

For a template-based system for generating deb files, see the `debgen` package in this repository

**Warning: debgo [is being renamed to 'debber'](https://github.com/debber), Please note that the this version should still be used until debber-v0.3 becomes stable. It will not receive updates after that point. Ta**

Example (BuildBinaryDeb)
package main

import (
	"github.com/laher/debgo-v0.2/deb"
	"log"
	"os"
	"path/filepath"
)

func main() {

	pkg := deb.NewPackage("testpkg", "0.0.2", "me", "lovely package\n")
	pkg.Description = "hiya"
	exesMap := map[string][]string{
		"amd64": []string{filepath.Join(deb.TempDirDefault, "/a.amd64")},
		"i386":  []string{filepath.Join(deb.TempDirDefault, "/a.i386")},
		"armhf": []string{filepath.Join(deb.TempDirDefault, "/a.armhf")}}
	err := createExes(exesMap)
	if err != nil {
		log.Fatalf("%v", err)
	}
	artifacts, err := deb.NewDebWriters(pkg)
	if err != nil {
		log.Fatalf("Error building binary: %v", err)
	}
	artifacts[deb.ArchAmd64].MappedFiles = map[string]string{"/usr/bin/a": filepath.Join(deb.TempDirDefault, "/a.amd64")}
	artifacts[deb.ArchI386].MappedFiles = map[string]string{"/usr/bin/a": filepath.Join(deb.TempDirDefault, "/a.i386")}
	artifacts[deb.ArchArmhf].MappedFiles = map[string]string{"/usr/bin/a": filepath.Join(deb.TempDirDefault, "/a.armhf")}
	buildDeb := func(art *deb.DebWriter) error {
		//generate artifact here ...
		return nil
	}
	for arch, artifact := range artifacts {
		//build binary deb here ...
		err = buildDeb(artifact)
		if err != nil {
			log.Fatalf("Error building for '%s': %v", arch, err)
		}
	}
}

func createExes(exesMap map[string][]string) error {
	for _, exes := range exesMap {
		for _, exe := range exes {
			err := os.MkdirAll(filepath.Dir(exe), 0777)
			if err != nil {
				return err
			}
			fi, err := os.Create(exe)
			if err != nil {
				return err
			}
			_, err = fi.Write([]byte("echo 1"))
			if err != nil {
				return err
			}
			err = fi.Close()
			if err != nil {
				return err
			}
		}
	}
	return nil
}
Output:

Example (BuildDevPackage)
package main

import (
	"github.com/laher/debgo-v0.2/deb"
	"log"
)

func main() {

	pkg := deb.NewPackage("testpkg", "0.0.2", "me", "A package\ntestpkg is a lovel package with many wow")
	buildFunc := func(dpkg *deb.Package) error {
		// Generate files here.
		return nil
	}
	dpkg := deb.NewDevPackage(pkg)
	err := buildFunc(dpkg)
	if err != nil {
		log.Fatalf("%v", err)
	}
}
Output:

Example (BuildSourceDeb)
package main

import (
	"github.com/laher/debgo-v0.2/deb"
	"github.com/laher/debgo-v0.2/targz"
	"io/ioutil"
	"log"
	"path/filepath"
)

func main() {
	pkg := deb.NewPackage("testpkg", "0.0.2", "me <me@b.c>", "Nice of all the package\n")
	pkg.Description = "hiya"
	spkg := deb.NewSourcePackage(pkg)
	err := buildOrigArchive(spkg) // it's up to you how to build this
	if err != nil {
		log.Fatalf("Error building source package: %v", err)
	}
	err = buildDebianArchive(spkg) // again - do it yourself
	if err != nil {
		log.Fatalf("Error building source package: %v", err)
	}
	err = buildDscFile(spkg) // yep, same again
	if err != nil {
		log.Fatalf("Error building source package: %v", err)
	}
}

func buildOrigArchive(spkg *deb.SourcePackage) error {
	origFilePath := filepath.Join(deb.DistDirDefault, spkg.OrigFileName)
	tgzw, err := targz.NewWriterFromFile(origFilePath)
	if err != nil {
		return err
	}

	err = tgzw.Close()
	if err != nil {
		return err
	}
	return nil
}

func buildDebianArchive(spkg *deb.SourcePackage) error {
	tgzw, err := targz.NewWriterFromFile(filepath.Join(deb.DistDirDefault, spkg.DebianFileName))
	if err != nil {
		return err
	}

	err = tgzw.Close()
	if err != nil {
		return err
	}
	return nil
}

func buildDscFile(spkg *deb.SourcePackage) error {
	dscData := []byte{}
	dscFilePath := filepath.Join(deb.DistDirDefault, spkg.DscFileName)
	err := ioutil.WriteFile(dscFilePath, dscData, 0644)
	if err != nil {
		return err
	}
	return nil
}
Output:

Index

Examples

Constants

View Source
const (
	DebianBinaryVersionDefault = "2.0"         // This is the current version as specified in .deb archives (filename debian-binary)
	DebianCompatDefault        = "9"           // compatibility. Current version
	FormatDefault              = "3.0 (quilt)" // Format as in a .dsc file
	StatusDefault              = "unreleased"  // Status is unreleased by default. Change this once you're happy with it.

	SectionDefault          = "devel"                           //TODO: correct to use this?
	PriorityDefault         = "extra"                           //Most packages should be 'extra'
	DependsDefault          = ""                                //No dependencies
	BuildDependsDefault     = "debhelper (>= 9.1.0), golang-go" //Default build dependencies for Go packages
	StandardsVersionDefault = "3.9.4"                           //Current standards version
	ArchitectureDefault     = "any"                             //Any is the default architecture for packages

	TemplateDirDefault  = "templates"
	ResourcesDirDefault = "resources"
	WorkingDirDefault   = "."

	ExeDirDefault                   = "/usr/bin" //default directory for exes within the control archive
	BinaryDataArchiveNameDefault    = "data.tar.gz"
	BinaryControlArchiveNameDefault = "control.tar.gz"
)

Variables

View Source
var (
	TempDirDefault = filepath.Join("_out", "tmp")
	DistDirDefault = filepath.Join("_out", "dist")

	MaintainerScripts = []string{"postinst", "postrm", "prerm", "preinst"}
)

Functions

func DebExtractFileL2

func DebExtractFileL2(rdr io.Reader, topLevelFilename string, secondLevelFilename string, destination io.Writer) error

func DebGetContents

func DebGetContents(rdr io.Reader, topLevelFilename string) ([]string, error)

func NewDebWriters

func NewDebWriters(pkg *Package) (map[Architecture]*DebWriter, error)

NewDebWriters gets and returns an artifact for each architecture. Returns an error if the package's architecture is un-parseable

func ParseVersion

func ParseVersion(packageVersion string) (string, string, string, error)

ParseVersion parses a debian version string into its [up to] 3 parts. Briefly, the format is: [epoch:]upstream_version[-debian_revision]

See http://www.debian.org/doc/debian-policy/ch-controlfields.html#s-f-Version

func SetDefaults

func SetDefaults(pkg *Package)

Sets fields which can be initialised appropriately

func ValidateArchitecture

func ValidateArchitecture(archString string) error

ValidateArchitecture checks the architecture string by parsing it. Returns an error on failure. Only supported architectures are considered valid. (e.g. armel is not supported, and non-linux OSes are also not supported yet)

See https://www.debian.org/doc/debian-policy/ch-controlfields.html#s-f-Architecture

func ValidateName

func ValidateName(packageName string) error

ValidateName validates a package name for both 'Source:' and 'Package:' names

See http://www.debian.org/doc/debian-policy/ch-controlfields.html#s-f-Source

func ValidatePackage

func ValidatePackage(pkg *Package) error

ValidatePackage checks required fields and certain restricted values.

This can be considered a work-in-progress.

func ValidateVersion

func ValidateVersion(packageVersion string) error

ValidateVersion checks a version string against the policy manual definition.

See ParseVersion

Example
package main

import (
	"github.com/laher/debgo-v0.2/deb"
	"log"
)

func main() {
	v := "1.0.1-git123"
	err := deb.ValidateVersion(v)
	if err != nil {
		log.Fatalf("Version validation broken for %v", v)
	}

}
Output:

Types

type Architecture

type Architecture string

Architecture - processor architecture (ARM/x86/AMD64) - as named by Debian. At this stage: i386, armhf, amd64 and 'all'. Note that 'any' is not valid for a binary package, and resolves to [i386, armhf, amd64] TODO: armel (Note that armhf = ARMv7 and armel = ARMv5. In Go terms, this is is is governed by the environment variable GOARM, and 7 is the default)

const (
	ArchI386  Architecture = "i386"  // x86
	ArchArmhf Architecture = "armhf" //ARMv7 TODO: armel
	ArchAmd64 Architecture = "amd64" //For 64-bit machines
	ArchAll   Architecture = "all"   //for binary packages
)

type Checksum

type Checksum struct {
	Checksum string
	Size     int64
	File     string
}

Checksum stores a checksum for a file

type Checksums

type Checksums struct {
	ChecksumsMd5    []Checksum
	ChecksumsSha1   []Checksum
	ChecksumsSha256 []Checksum
}

Checksums stores the 3 required checksums for a list of files

func (*Checksums) Add

func (cs *Checksums) Add(filepath, basename string) error

Add adds checksum entries for each checksum algorithm

type DebReader

type DebReader struct {
	Reader           io.Reader
	ArReader         *ar.Reader
	HasDebianVersion bool
}

func NewDebReader

func NewDebReader(rdr io.Reader) (*DebReader, error)

func (*DebReader) NextTar

func (drdr *DebReader) NextTar() (string, *tar.Reader, error)

Gets next tar header for supported types

type DebWriter

type DebWriter struct {
	Package             *Package
	Architecture        Architecture
	Filename            string
	DebianBinaryVersion string
	ControlArchive      string
	DataArchive         string
	MappedFiles         map[string]string
}

DebWriter is an architecture-specific deb

func NewDebWriter

func NewDebWriter(pkg *Package, architecture Architecture) *DebWriter

Factory of platform build information

func (*DebWriter) Build

func (bdeb *DebWriter) Build(tempDir, destDir string) error

func (*DebWriter) SetDefaults

func (bdeb *DebWriter) SetDefaults()

SetDefaults sets some default properties

type DscReader

type DscReader struct {
	Reader io.Reader
}

DscReader reads a control file.

func NewDscReader

func NewDscReader(rdr io.Reader) *DscReader

NewDscReader is a factory for reading Dsc files.

func (*DscReader) Parse

func (dscr *DscReader) Parse() (*Package, error)

Parse parses a file into a package.

type Package

type Package struct {
	Name        string // Package name
	Version     string // Package version
	Description string // Description
	Maintainer  string // Maintainer

	AdditionalControlData map[string]string // Other key/values to go into the Control file.

	Architecture string // Supported values: "all", "x386", "amd64", "armhf". TODO: armel

	Depends    string // Depends
	Recommends string
	Suggests   string
	Enhances   string
	PreDepends string
	Conflicts  string
	Breaks     string
	Provides   string
	Replaces   string

	BuildDepends      string // BuildDepends is only required for "sourcedebs".
	BuildDependsIndep string
	ConflictsIndep    string
	BuiltUsing        string

	Priority         string
	StandardsVersion string
	Section          string
	Format           string
	Status           string
	Other            string
	Source           string

	ExtraData map[string]interface{} // Optional for templates

}

Package is the base unit for this library. A *Package contains metadata.

func Copy

func Copy(pkg *Package) *Package

func DebParseMetadata

func DebParseMetadata(rdr io.Reader) (*Package, error)

ParseDebMetadata reads an artifact's contents.

func NewDevPackage

func NewDevPackage(pkg *Package) *Package

NewDevPackage is a factory for creating '-dev' packages from packages. It just does a copy, appends "-dev" to the name, and sets the

func NewPackage

func NewPackage(name, version, maintainer, description string) *Package

NewPackage is a factory for a Package. Name, Version, Maintainer and Description are mandatory.

func (*Package) GetArches

func (pkg *Package) GetArches() ([]Architecture, error)

GetArches resolves architecture(s) and return as a slice

func (*Package) SetField

func (pkg *Package) SetField(key, value string)

SetField sets a control field by name Unrecognised keys are added to AdditionalControlData

type SourcePackage

type SourcePackage struct {
	Package        *Package
	DscFileName    string
	OrigFileName   string
	DebianFileName string
	DebianFiles    []string
}

SourcePackage is a cross-platform package with a .dsc file.

func NewSourcePackage

func NewSourcePackage(pkg *Package) *SourcePackage

NewSourcePackage is a factory for SourcePackage. Sets up default paths.. Initialises default filenames, using .tar.gz as the archive type

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL