func extractFromLocalDir()

in kinder/pkg/extract/extract.go [339:410]


func extractFromLocalDir(src string, files []string, dst string, m fileNameMutator, addVersionFileToDst bool) (paths map[string]string, err error) {
	// checks if source folder exists
	src, _ = filepath.Abs(src)
	if _, err := os.Stat(src); os.IsNotExist(err) {
		return nil, errors.Errorf("source path %s does not exists", src)
	}

	// read version file (only if required by the fileNameMutator)
	if err := m.ReadVersionFile(src); err != nil {
		return nil, err
	}

	// checks if target folder exists
	dst, _ = filepath.Abs(dst)
	if _, err := os.Stat(dst); os.IsNotExist(err) {
		return nil, errors.Errorf("destination path %s does not exists", dst)
	}

	// ensure folder required by the fileNameMutator exist (if any)
	// nb. this will allow to save extracted files into a version folder
	if err := m.EnsureFolder(dst); err != nil {
		return nil, err
	}

	// if the local repository is a single file
	if info, err := os.Stat(src); err == nil && !info.IsDir() {
		// sets the extractor for getting this file only overriding the default file list
		files = []string{filepath.Base(src)}

		// points the extractor to the upper folder (the extractor expects a folder)
		parent := filepath.Dir(src)
		log.Debugf("%s is a file, moving up one level to %s", src, parent)
		src = parent
	} else {
		// Expanding wildcards defined in the list of files (if any)
		// NB. this is required because for imageBits we want to allow to extract
		// all the images in a folder, not only the Kubernetes one
		files, err = expandWildcards(src, files)
		if err != nil {
			return nil, err
		}
	}

	// if required, add the version file to the list of files to be copied to dest
	// nb. version file is created so the target folder can be eventually used as a source
	if addVersionFileToDst {
		files = append(files, "version")
	}

	// copy files from source to target
	paths = map[string]string{}
	for _, f := range files {
		srcFilePath := path.Join(src, f)
		if _, err := os.Stat(srcFilePath); err != nil {
			return nil, errors.Wrapf(err, "cannot access %s at %s", f, srcFilePath)
		}
		log.Infof("Copying %s", srcFilePath)

		dstFilePath := path.Join(dst, m.Mutate(f))
		// NOTE: we use copy not copyfile because copy ensures the dest dir
		if err := kindfs.Copy(srcFilePath, dstFilePath); err != nil {
			return nil, errors.Wrap(err, "failed to copy alter bits")
		}
		if f == kubeadmBinary || f == kubeletBinary || f == kubectlBinary {
			os.Chmod(dstFilePath, 0755)
		}
		paths[f] = dstFilePath
	}

	log.Infof("Copied files saved into %s", dst)
	return paths, nil
}