Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

COPY command #40

Merged
merged 9 commits into from
Mar 26, 2018
Merged

Conversation

priyawadhwa
Copy link
Collaborator

#38

The COPY command copies from sources to a specified destination. The sources are relative to a directory (which I call buildcontext in the command), and which should already be unpacked by the time the command is called.

@priyawadhwa priyawadhwa force-pushed the copy-cmd branch 6 times, most recently from 0bb35a9 to f15b63a Compare March 15, 2018 19:07
// ContainsWildcards returns true if any entry in paths contains wildcards
func ContainsWildcards(paths []string) bool {
for _, path := range paths {
for i := 0; i < len(path); i++ {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can probably use indexFunc here instead of a for loop:
https://golang.org/pkg/strings/#IndexFunc

srcs := srcsAndDest[:len(srcsAndDest)-1]
dest := srcsAndDest[len(srcsAndDest)-1]
// If destination is a directory, return nil
if IsDestDir(dest) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes it seem like it's ok to copy no source files if the destination is a directory. Is that actually OK?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

whoops, nope, fixed!

baseDir := filepath.Dir(path)
if _, err := os.Stat(baseDir); os.IsNotExist(err) {
logrus.Debugf("baseDir %s for file %s does not exist. Creating.", baseDir, path)
if err := os.MkdirAll(baseDir, perm); err != nil {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like the perm passed in applies to the directory, not the file. Is that intentional?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed as well, it should be applied to just the file

// If source is a dir:
// Assume dest is also a dir, and copy to dest/relpath
// If dest is not an absolute filepath, add /cwd to the beginning
func RelativeFilepath(filename, srcName, dest, cwd, buildcontext string) (string, error) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This name is a bit weird since it returns absolute paths right?

return strings.HasSuffix(path, "/")
}

func IsAbsoluteFilepath(path string) bool {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

} else {
// ... Else, we want to copy over a file
logrus.Infof("Copying file %s to %s", file, destPath)
contents, err := ioutil.ReadFile(filepath.Join(c.buildcontext, file))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The files are all local, right? You might be able to use an os.Copy, instead of buffering the file into memory then writing it back.

// FilesToSnapshot should return an empty array if still nil; no files were changed
func (c *CopyCommand) FilesToSnapshot() []string {
if c.snapshotFiles == nil {
return []string{}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You might just be able to return c.snapshotFiles, for most things an empty slice is the same as nil.

// FilepathExists returns true if the path exists
func FilepathExists(path string) bool {
_, err := os.Stat(path)
return (err == nil)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should technically be

 !os.IsNotExist(err)

stat could fail for another reason.

}

// CreateFile creates a file at path with contents specified
func CreateFile(path string, contents []byte, perm os.FileMode) error {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can replace some of this function with iotuil.WriteFile, but you might not need it after switching to os.Copy above.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I ended up replacing with ioutil.WriteFile since the directories still need to be created if they don't already exist

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just kidding, ended up using io.Copy and passing in a Reader instead of contents :)

@priyawadhwa priyawadhwa merged commit 0825115 into GoogleContainerTools:master Mar 26, 2018
@priyawadhwa priyawadhwa deleted the copy-cmd branch March 26, 2018 16:46
@priyawadhwa priyawadhwa added the area/dockerfile-command For all bugs related to dockerfile file commands label Feb 9, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/dockerfile-command For all bugs related to dockerfile file commands
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants