From e8bed5d4e76361cbf4be99744fee4992827f029c Mon Sep 17 00:00:00 2001 From: danny Date: Tue, 31 May 2022 12:09:23 +0300 Subject: [PATCH] adding support for conf-files --- config/config.go | 1 + deb/canonical.go | 2 +- deb/deb.go | 33 ++++++++++++++++++++++++++------- main.go | 41 +++++++++++++++++++---------------------- main_test.go | 1 + 5 files changed, 48 insertions(+), 30 deletions(-) diff --git a/config/config.go b/config/config.go index a0cb484..b51bdf8 100644 --- a/config/config.go +++ b/config/config.go @@ -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 diff --git a/deb/canonical.go b/deb/canonical.go index 3341cf2..79879ba 100644 --- a/deb/canonical.go +++ b/deb/canonical.go @@ -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 diff --git a/deb/deb.go b/deb/deb.go index 8797064..086a284 100644 --- a/deb/deb.go +++ b/deb/deb.go @@ -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 @@ -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 { @@ -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) diff --git a/main.go b/main.go index 60262a0..3a67126 100644 --- a/main.go +++ b/main.go @@ -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 @@ -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 } @@ -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) diff --git a/main_test.go b/main_test.go index b777c40..0197d48 100644 --- a/main_test.go +++ b/main_test.go @@ -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