func()

in utils/zip.go [269:363]


func (zw *ZipWriter) Zip() error {

	var zipFile *os.File
	var err error
	var fileInfo os.FileInfo
	var verboseMsg string

	// create zip file e.g. greeting.zip
	if zipFile, err = os.Create(zw.des); err != nil {
		return err
	}
	defer zipFile.Close()

	verboseMsg = wski18n.T(wski18n.ID_VERBOSE_CREATING_ZIP_FILE_X_path_X,
		map[string]interface{}{
			wski18n.KEY_PATH: zipFile.Name(),
		})
	wskprint.PrintlnOpenWhiskVerbose(Flags.Verbose, verboseMsg)

	// creating a new zip writter for greeting.zip
	zw.zipWriter = zip.NewWriter(zipFile)

	// build a map of file names and bool indicating whether the file is included or excluded
	// iterate over the directory specified in "function", find the list of files and mark them as not excluded
	if err = zw.findExcludedIncludedFiles(zw.src, false); err != nil {
		return err
	}

	if err = zw.buildExcludeMetadata(); err != nil {
		return err
	}

	// walk file system rooted at the directory specified in "function"
	// walk over each file and dir under root directory e.g. function: actions/greeting
	// add actions/greeting/index.js and actions/greeting/package.json to zip file
	if err = filepath.Walk(zw.src, zw.zipFile); err != nil {
		return nil
	}

	// maintain a list of included files and/or directories with their destination
	var includeInfo []Include
	includeInfo, err = zw.buildIncludeMetadata()
	if err != nil {
		return err
	}

	for _, i := range includeInfo {
		if i.source != i.destination {
			// now determine whether the included item is file or dir
			// it could list something like this as well, "actions/common/*.js"
			if fileInfo, err = os.Stat(i.source); err != nil {
				return err
			}

			// if the included item is a directory, call a function to copy the
			// entire directory recursively including its subdirectories and files
			if fileInfo.Mode().IsDir() {
				if err = copyDir(i.source, i.destination); err != nil {
					return err
				}
				// if the included item is a file, call a function to copy the file
				// along with its path by creating the parent directories
			} else if fileInfo.Mode().IsRegular() {
				if err = copyFile(i.source, i.destination); err != nil {
					return err
				}
			}
		}
		// add included item into zip file greeting.zip
		if err = filepath.Walk(i.destination, zw.zipFile); err != nil {
			return nil
		}
	}

	// now close the zip file greeting.zip as all the included items
	// are added into the zip file along with the action root dir
	if err = zw.zipWriter.Close(); err != nil {
		return err
	}

	// and its safe to delete the files/directories which we copied earlier
	// to include them in the zip file greeting.zip
	for _, i := range includeInfo {
		if filepath.Clean(i.source) != filepath.Clean(i.destination) {
			verboseMsg = wski18n.T(wski18n.ID_VERBOSE_DELETING_FILE_X_path_X,
				map[string]interface{}{
					wski18n.KEY_PATH: i.destination,
				})
			wskprint.PrintlnOpenWhiskVerbose(Flags.Verbose, verboseMsg)
			os.RemoveAll(i.destination)
		}
	}

	return nil
}