Skip to content

Commit

Permalink
adding support for conf-files
Browse files Browse the repository at this point in the history
  • Loading branch information
dtylman committed May 31, 2022
1 parent f9fe190 commit e8bed5d
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 30 deletions.
1 change: 1 addition & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type PackageOptions struct {
Folders map[string]string `json:"folders"`
Files map[string]string `json:"files"`
Script Scripts `json:"scripts"`
Conffiles string `json:"conffiles"`
}

//Load loads configuration from file
Expand Down
2 changes: 1 addition & 1 deletion deb/canonical.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func (c *canonical) AddLink(name string, linkName string) error {
func (c *canonical) AddEmptyFolder(name string) error {
name = strings.TrimPrefix(name, "/")
if name == "" {
return fmt.Errorf("Cannot add empty name for empty folder")
return fmt.Errorf("cannot add empty name for empty folder")
}
header := new(tar.Header)
header.Name = name
Expand Down
33 changes: 26 additions & 7 deletions deb/deb.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ type Deb struct {
PostInst string `json':"post_inst"`
PreRm string `json:"pre_rm"`
PostRm string `json:"post_rm"`
//A package declares its list of conffiles by including a conffiles file in its control archive
ConfFiles string `json:"conf_files"`
}

//New creates new deb writer
Expand All @@ -57,7 +59,7 @@ func New(name, version, revision, arch string) (*Deb, error) {
//Create creates the deb file
func (d *Deb) Create(folder string) (string, error) {
if d.Info.Package == "" {
return "", errors.New("Package name cannot be empty")
return "", errors.New("package name cannot be empty")
}
err := d.Control.AddEmptyFolder("./")
if err != nil {
Expand All @@ -71,19 +73,36 @@ func (d *Deb) Create(folder string) (string, error) {
if err != nil {
return "", err
}

if d.PostInst != "" {
d.Control.AddBytes([]byte(d.PostInst), "postinst")
err = d.Control.AddBytes([]byte(d.PostInst), "postinst")
if err != nil {
return "", err
}
}
if d.PreInst != "" {
d.Control.AddBytes([]byte(d.PreInst), "preinst")

err = d.Control.AddBytes([]byte(d.PreInst), "preinst")
if err != nil {
return "", err
}
}
if d.PostRm != "" {
d.Control.AddBytes([]byte(d.PostRm), "postrm")

err = d.Control.AddBytes([]byte(d.PostRm), "postrm")
if err != nil {
return "", err
}
}
if d.PreRm != "" {
d.Control.AddBytes([]byte(d.PreRm), "prerm")
err = d.Control.AddBytes([]byte(d.PreRm), "prerm")
if err != nil {
return "", err
}
}
if d.ConfFiles != "" {
err = d.Control.AddBytes([]byte(d.ConfFiles), "conffiles")
if err != nil {
return "", err
}
}
fileName := filepath.Join(folder, fmt.Sprintf("%s_%s_%s.deb", d.Info.Package, d.Info.Version, d.Info.Architecture))
debFile, err := os.Create(fileName)
Expand Down
41 changes: 19 additions & 22 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ import (
)

//Version is gopack version
var Version = "0.0.1"
var Version = "0.0.2"

//Options holds commandline options
//Options holds command line options
var Options struct {
//OutPath is the output path
OutPath string
Expand All @@ -33,18 +33,19 @@ var Options struct {
Revision string
}

func addScript(sourceFileName string, value *string) error {
if sourceFileName == "" {
//loadFile loads from file name to value
func loadFile(from string, value *string) error {
if from == "" {
return nil
}
data, err := ioutil.ReadFile(sourceFileName)
data, err := ioutil.ReadFile(from)
if err != nil {
return err
}
if value == nil {
return errors.New("script value is null")
return errors.New("value is null")
}
log.Printf("Adding script '%v' (%v bytes)", sourceFileName, len(data))
log.Printf("loaded file '%v' (%v bytes)", from, len(data))
*value = string(data)
return nil
}
Expand Down Expand Up @@ -116,23 +117,19 @@ func createDeb(cfg *config.PackageOptions) error {
}
}

err = addScript(cfg.Script.PostInst, &deb.PostInst)
if err != nil {
return fmt.Errorf("failed to add script '%v'", err)
}
err = addScript(cfg.Script.PreInst, &deb.PreInst)
if err != nil {
return fmt.Errorf("failed to add script '%v'", err)
files := map[string]*string{
cfg.Script.PostInst: &deb.PostInst,
cfg.Script.PreInst: &deb.PreInst,
cfg.Script.PostUnInst: &deb.PostRm,
cfg.Script.PreUnInst: &deb.PreRm,
cfg.Conffiles: &deb.ConfFiles,
}

err = addScript(cfg.Script.PostUnInst, &deb.PostRm)
if err != nil {
return fmt.Errorf("failed to add script '%v'", err)
}

err = addScript(cfg.Script.PreUnInst, &deb.PreRm)
if err != nil {
return fmt.Errorf("failed to add script '%v'", err)
for source, target := range files {
err = loadFile(source, target)
if err != nil {
return fmt.Errorf("failed to load file %v", source)
}
}

fileName, err := deb.Create(Options.OutPath)
Expand Down
1 change: 1 addition & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ func sampleDeb() error {
d.Info.Description = `Hello world
Lorum ipsum
Yada yada`
d.ConfFiles = "/etc/hoho.cfg"
debFileName, err := d.Create("")
fmt.Println("Created " + debFileName)
return err
Expand Down

0 comments on commit e8bed5d

Please sign in to comment.