summaryrefslogtreecommitdiff
path: root/deb822/package.go
diff options
context:
space:
mode:
authorKatolaZ <katolaz@freaknet.org>2018-01-10 02:49:20 +0000
committerKatolaZ <katolaz@freaknet.org>2018-01-10 02:49:20 +0000
commitb3ca06d08f4bba7af522a085e942d523c697a684 (patch)
treeac1fe3ed403300d02a30222a987d7a1af250d56c /deb822/package.go
first commit
Diffstat (limited to 'deb822/package.go')
-rw-r--r--deb822/package.go108
1 files changed, 108 insertions, 0 deletions
diff --git a/deb822/package.go b/deb822/package.go
new file mode 100644
index 0000000..df444ea
--- /dev/null
+++ b/deb822/package.go
@@ -0,0 +1,108 @@
+package deb822
+
+import (
+ "fmt"
+ "html/template"
+ "io"
+ "log"
+ "os"
+ "regexp"
+ "strings"
+)
+
+type Package struct {
+ Name string
+ Version string
+ Description string
+ LongDescription string
+ Depends []string
+ Maintainer string
+ Filename string
+}
+
+var regexpRemove = regexp.MustCompile("(DEVUAN/|DEBIAN/|[0-9]+:)")
+
+var regexpDots = regexp.MustCompilePOSIX("(^[[:blank:]]+.[[:blank:]]*$)")
+
+func NewPackage(s Stanza) (Package, error) {
+
+ var p Package
+
+ if s["Package"] == "" || s["Version"] == "" {
+ return p, fmt.Errorf("empty package")
+ }
+
+ p.Name = s["Package"]
+ p.Version = regexpRemove.ReplaceAllString(s["Version"], "")
+ descr := strings.SplitN(s["Description"], "\n", 2)
+ p.Description = descr[0]
+ if len(descr) > 1 {
+ p.LongDescription = regexpDots.ReplaceAllString(descr[1], "")
+ }
+ p.Maintainer = s["Maintainer"]
+ p.Depends = strings.Split(s["Depends"], ",")
+
+ return p, nil
+
+}
+
+func PrintPackage(p Package, templ string, out io.Writer) {
+
+ t, err := template.New("webpage").Parse(templ)
+
+ if err != nil {
+ log.Fatal("Error in template")
+ }
+
+ t.Execute(out, p)
+}
+
+/*Stanza2HtmlPage Render the html webpage of a package and save it in the
+/* corresponding "pool" directory.
+*/
+func Stanza2HtmlPage(s Stanza, templ string, baseDir string) error {
+
+ fname := s["Filename"]
+ if fname == "" {
+ return fmt.Errorf("No Filename provided")
+ }
+ p, err := NewPackage(s)
+ if err != nil {
+ log.Fatal("empty package!!!")
+ }
+ nameVersion := fmt.Sprintf("%s_%s", p.Name, p.Version)
+ fmt.Printf(" nameVersion: %s\n", nameVersion)
+ fmt.Printf(" baseDir: %s\n", baseDir)
+
+ fname = regexpRemove.ReplaceAllString(fname, "")
+ fmt.Printf(" fname: %s\n", fname)
+
+ /// FIXME!!!! ERROR IN DETECTION OF PACKAGE NAME!!!!
+ dirName := fmt.Sprintf("%s/%s", baseDir, strings.Split(fname, nameVersion)[0])
+
+ err = os.MkdirAll(dirName, 0755)
+ if err == nil {
+ fmt.Printf(" dirName: %s\n Package: %s\n Version: %s\n", dirName, p.Name, p.Version)
+ htmlFile := fmt.Sprintf("%s%s_%s.html", dirName, p.Name, p.Version)
+ fmt.Printf("Creating file: %s\n---------\n", htmlFile)
+ if f, err := os.Open(htmlFile); err == nil {
+ f.Close()
+ fmt.Printf("File %s exists -- skipping\n", htmlFile)
+ return err
+ }
+ file, err := os.Create(htmlFile)
+ if err != nil {
+ err = fmt.Errorf("Error creating file: %s", err)
+ return err
+ } else {
+ PrintPackage(p, templ, file)
+ file.Close()
+ return nil
+ }
+ } else {
+ err = fmt.Errorf("Error creating path: %s", err)
+ return err
+ }
+
+ return nil
+}