// Build returns the zipped contents of the function. func (f *Function) Build() (io.Reader, error) { f.Log.Debugf("creating build") if err := f.hook(BuildHook); err != nil { return nil, err } buf := new(bytes.Buffer) zip := archive.NewZipWriter(buf) files, err := utils.LoadFiles(f.Path, f.IgnoredPatterns) if err != nil { return nil, err } for path, file := range files { f.Log.WithField("file", path).Debug("add file to zip") if err := zip.AddFile(path, file); err != nil { return nil, err } if err := file.Close(); err != nil { return nil, err } } if err := zip.Close(); err != nil { return nil, err } return buf, nil }
// Build returns the zipped contents of the function. func (f *Function) Build() (io.Reader, error) { f.Log.Debugf("creating build") buf := new(bytes.Buffer) zip := archive.NewCompressedZipWriter(buf, flate.DefaultCompression) if err := f.hookBuild(zip); err != nil { return nil, err } files, err := utils.LoadFiles(f.Path, f.IgnoreFile) if err != nil { return nil, err } for _, path := range files { f.Log.WithField("file", path).Debug("add file to zip") info, err := os.Lstat(filepath.Join(f.Path, path)) if err != nil { return nil, err } realPath := filepath.Join(f.Path, path) if info.Mode()&os.ModeSymlink == os.ModeSymlink { linkPath, err := filepath.EvalSymlinks(realPath) if err != nil { return nil, err } realPath = linkPath } fh, err := os.Open(realPath) if err != nil { return nil, err } info, err = fh.Stat() if err != nil { return nil, err } unixPath := strings.Replace(path, "\\", "/", -1) if err := zip.AddInfoFile(unixPath, timelessInfo{info}, fh); err != nil { return nil, err } if err := fh.Close(); err != nil { return nil, err } } if err := zip.Close(); err != nil { return nil, err } return buf, nil }
// Build returns the zipped contents of the function. func (f *Function) Build() (io.Reader, error) { f.Log.Debugf("creating build") buf := new(bytes.Buffer) zip := archive.NewZip(buf) if err := f.hookBuild(zip); err != nil { return nil, err } paths, err := utils.LoadFiles(f.Path, f.IgnoreFile) if err != nil { return nil, err } for _, path := range paths { f.Log.WithField("file", path).Debug("add file to zip") fullPath := filepath.Join(f.Path, path) fh, err := os.Open(fullPath) if err != nil { return nil, err } info, err := fh.Stat() if err != nil { return nil, err } if info.IsDir() { // It's a symlink, otherwise it shouldn't be returned by LoadFiles linkPath, err := filepath.EvalSymlinks(fullPath) if err != nil { return nil, err } if err := zip.AddDir(linkPath, path); err != nil { return nil, err } } else { if err := zip.AddFile(path, fh); err != nil { return nil, err } } if err := fh.Close(); err != nil { return nil, err } } if err := zip.Close(); err != nil { return nil, err } return buf, nil }
// Build returns the zipped contents of the function. func (f *Function) Build() (io.Reader, error) { f.Log.Debugf("creating build") if err := f.RunHook("build"); err != nil { return nil, err } buf := new(bytes.Buffer) zip := archive.NewZipWriter(buf) if r, ok := f.runtime.(runtime.CompiledRuntime); ok { f.Log.Debugf("compiling") if err := r.Build(f.Path); err != nil { return nil, fmt.Errorf("compiling: %s", err) } } if f.env != nil { f.Log.Debugf("adding .env.json") b, err := json.Marshal(f.env) if err != nil { return nil, err } zip.AddBytes(".env.json", b) } if f.runtime.Shimmed() { f.Log.Debugf("adding nodejs shim") zip.AddBytes("index.js", shim.MustAsset("index.js")) zip.AddBytes("byline.js", shim.MustAsset("byline.js")) } files, err := utils.LoadFiles(f.Path, f.IgnoredPatterns) if err != nil { return nil, err } for path, file := range files { f.Log.WithField("file", path).Debug("add file") if err := zip.AddFile(path, file); err != nil { return nil, err } if err := file.Close(); err != nil { return nil, err } } if err := zip.Close(); err != nil { return nil, err } return buf, nil }
// Open the function.json file and prime the config. func (f *Function) Open() error { p, err := os.Open(filepath.Join(f.Path, "function.json")) if err == nil { if err := json.NewDecoder(p).Decode(&f.Config); err != nil { return err } } if f.Runtime == "" { if runtimeName, err := runtime.Detect(f.Path); err == nil { f.Runtime = runtimeName } } if err := validator.Validate(&f.Config); err != nil { return fmt.Errorf("error opening function %s: %s", f.Name, err.Error()) } r, err := runtime.ByName(f.Runtime) if err != nil { return err } f.runtime = r patterns, err := utils.ReadIgnoreFile(f.Path) if err != nil { return err } f.IgnoredPatterns = append(f.IgnoredPatterns, patterns...) f.files, err = utils.LoadFiles(f.Path, f.IgnoredPatterns) if err != nil { return err } f.Log = f.Log.WithField("function", f.Name) return nil }
func Test_LoadFiles(t *testing.T) { files, _ := utils.LoadFiles("_fixtures/fileAndDir", []string{"testfile"}) assert.Equal(t, "testdir/indir", files[0]) assert.Equal(t, 2, len(files)) }
func Test_LoadFiles(t *testing.T) { files, _ := utils.LoadFiles("_fixtures/fileAndDir", []byte("testfile\nignored_folder")) assert.Equal(t, "testdir/indir", files[0]) assert.Equal(t, 2, len(files)) }