github.com/rajasrinivasan/spm@v0.0.0-20200125100127-755649755f3f/src/impl/build.go (about)

     1  package impl
     2  
     3  import (
     4  	"io"
     5  	"io/ioutil"
     6  	"log"
     7  	"os"
     8  	"path/filepath"
     9  
    10  	"../cfg"
    11  	"../pkg"
    12  )
    13  
    14  var Verbose = false
    15  var Workarea = "/tmp"
    16  var KeepWorkArea bool
    17  var PkgPassword string
    18  
    19  const ManifestFileName = "Packagefile"
    20  
    21  func copyFile(from, to string) error {
    22  	log.Printf("Copying file %s to %s\n", from, to)
    23  	fullname, _ := filepath.Abs(from)
    24  	infile, err := os.Open(from)
    25  	if err != nil {
    26  		log.Fatalf("Failed to open %s\n", fullname)
    27  		return err
    28  	}
    29  	defer infile.Close()
    30  	outfilename := filepath.Join(to, filepath.Base(from))
    31  	outfile, err := os.Create(outfilename)
    32  	if err != nil {
    33  		log.Printf("Failed to create %s\n", outfilename)
    34  		return err
    35  	}
    36  	defer outfile.Close()
    37  	io.Copy(outfile, infile)
    38  	return nil
    39  }
    40  
    41  func assembleFiles(workarea string, pkgconfig *cfg.Config) error {
    42  	target := workarea
    43  	nfiles := len(pkgconfig.Contents)
    44  	for no := 0; no < nfiles; no++ {
    45  		f := pkgconfig.Contents[no]
    46  		copyFile(f.From, target)
    47  	}
    48  	return nil
    49  }
    50  func makePackageName(cfgname string) string {
    51  	bn := filepath.Base(cfgname)
    52  	cfgtype := filepath.Ext(cfgname)
    53  	pkgname := bn[:len(bn)-len(cfgtype)] + ".spm"
    54  	return pkgname
    55  }
    56  
    57  func Build(cfgfile string, outfile string) {
    58  	log.Printf("Building package for configuration file %s\n", cfgfile)
    59  	pkg.CreateWorkArea(Workarea)
    60  	if !KeepWorkArea {
    61  		defer pkg.CleanupWorkArea()
    62  	}
    63  
    64  	pkgconfig, err := cfg.LoadConfig(cfgfile)
    65  	if err != nil {
    66  		return
    67  	}
    68  
    69  	assembleFiles(pkg.ContentsDir, pkgconfig)
    70  	var pkgcontent = cfg.Content{From: ManifestFileName, To: ManifestFileName}
    71  	pkgconfig.Contents = append(pkgconfig.Contents, pkgcontent)
    72  
    73  	pvtkeyfile := filepath.Join(pkg.WorkDir, pkg.DefaultPrivateKeyFileName)
    74  	pubkeyfile := filepath.Join(pkg.ContentsDir, pkg.DefaultPublicKeyFileName)
    75  	pkg.GenerateKeys(pvtkeyfile, pubkeyfile)
    76  	log.Printf("Created keypair %s and %s\n", pvtkeyfile, pubkeyfile)
    77  
    78  	contfiles, _ := ioutil.ReadDir(pkg.ContentsDir)
    79  	var contnames []string
    80  	for _, fi := range contfiles {
    81  		cfn, _ := filepath.Abs(filepath.Join(pkg.ContentsDir, fi.Name()))
    82  		log.Printf("Content file %s\n", cfn)
    83  		contnames = append(contnames, cfn)
    84  	}
    85  	log.Printf("Files: %v\n", contnames)
    86  	pkg.SignFiles(contnames, pvtkeyfile)
    87  
    88  	pkgfilename := filepath.Join(pkg.ContentsDir, ManifestFileName)
    89  	pkgconfig.SaveManifest(pkgfilename)
    90  	log.Printf("Saved manifest %s\n", pkgfilename)
    91  
    92  	sigfilename := pkgfilename + ".sig"
    93  	pkg.SignFile(pkgfilename, sigfilename, pvtkeyfile)
    94  	log.Printf("Signed the Package file. Generated %s\n", sigfilename)
    95  
    96  	spmbasename := makePackageName(cfgfile)
    97  	spmname := filepath.Join(pkg.WorkDir, spmbasename)
    98  	pkg.Packfiles(spmname, pkg.ContentsDir)
    99  	log.Printf("Created %s\n", spmname)
   100  
   101  	encspmname := outfile
   102  	fi, err := os.Stat(outfile)
   103  	if err == nil {
   104  		if fi.IsDir() {
   105  			encspmname = filepath.Join(outfile, spmbasename)
   106  		}
   107  	}
   108  	if len(PkgPassword) < 1 {
   109  		log.Printf("No password provided for finalization. Cannot create %s\n", encspmname)
   110  		return
   111  	}
   112  	pkg.Encrypt(PkgPassword, spmname, encspmname)
   113  	log.Printf("Created %s\n", encspmname)
   114  
   115  }