This repository has been archived by the owner on Aug 14, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
Add assets loading, binary selection and VCS information in manifest #13
Merged
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
835e5f2
Put options into separate function
krnowak 19f485a
Allow specifying which binary to put into ACI
krnowak d6a542a
Allow specifying assets
krnowak 578979c
Allow preserving temporary directory
krnowak 51d508a
Add VCS specific label to manifest
krnowak 9f9d591
Move asset code into separate file
krnowak 897d6c3
Preserve absolute symlink targets
krnowak 3c49b94
Add placeholders for assets
krnowak fcbabce
Refactor asset handling
krnowak 2ccaf9b
Put misc functions in separate file
krnowak fa54476
More reliable project option parsing
krnowak 4c2f499
The grand refactoring of code
krnowak 28b9f0a
Add documentation for some more obscure functions.
krnowak 9dd4763
Update README
krnowak b7a5848
Lift restrictions for symlinks in assets
krnowak 3c5741d
Put some strings into quotes
krnowak 1a0af7d
Close files after copying asset
krnowak b378df3
Use better variable name
krnowak f7f50b3
Add a FIXME for an uig/gid leaking issue
krnowak 4143688
Use %q instead of "%s"
krnowak 1f5782c
Put an URL to issue instead of just a number
krnowak File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
) | ||
|
||
func copyRegularFile(src, dest string) error { | ||
srcFile, err := os.Open(src) | ||
if err != nil { | ||
return err | ||
} | ||
defer srcFile.Close() | ||
destFile, err := os.Create(dest) | ||
if err != nil { | ||
return err | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. defer close(destFile)? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for catching it. |
||
defer destFile.Close() | ||
if _, err := io.Copy(destFile, srcFile); err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func copySymlink(src, dest string) error { | ||
symTarget, err := os.Readlink(src) | ||
if err != nil { | ||
return err | ||
} | ||
if err := os.Symlink(symTarget, dest); err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func copyTree(src, dest string) error { | ||
return filepath.Walk(src, func(path string, info os.FileInfo, err error) error { | ||
if err != nil { | ||
return err | ||
} | ||
rootLess := path[len(src):] | ||
target := filepath.Join(dest, rootLess) | ||
mode := info.Mode() | ||
switch { | ||
case mode.IsDir(): | ||
err := os.Mkdir(target, mode.Perm()) | ||
if err != nil { | ||
return err | ||
} | ||
case mode.IsRegular(): | ||
if err := copyRegularFile(path, target); err != nil { | ||
return err | ||
} | ||
case mode&os.ModeSymlink == os.ModeSymlink: | ||
if err := copySymlink(path, target); err != nil { | ||
return err | ||
} | ||
default: | ||
return fmt.Errorf("Unsupported node %q in assets, only regular files, directories and symlinks are supported.", path, mode.String()) | ||
} | ||
return nil | ||
}) | ||
} | ||
|
||
func replacePlaceholders(path string, placeholderMapping map[string]string) string { | ||
Debug("Processing path: ", path) | ||
newPath := path | ||
for placeholder, replacement := range placeholderMapping { | ||
newPath = strings.Replace(newPath, placeholder, replacement, -1) | ||
} | ||
Debug("Processed path: ", newPath) | ||
return newPath | ||
} | ||
|
||
func validateAsset(ACIAsset, localAsset string) error { | ||
if !filepath.IsAbs(ACIAsset) { | ||
return fmt.Errorf("Wrong ACI asset: '%v' - ACI asset has to be absolute path", ACIAsset) | ||
} | ||
if !filepath.IsAbs(localAsset) { | ||
return fmt.Errorf("Wrong local asset: '%v' - local asset has to be absolute path", localAsset) | ||
} | ||
fi, err := os.Stat(localAsset) | ||
if err != nil { | ||
return fmt.Errorf("Error stating %v: %v", localAsset, err) | ||
} | ||
if fi.Mode().IsDir() || fi.Mode().IsRegular() { | ||
return nil | ||
} | ||
return fmt.Errorf("Can't handle local asset %v - not a file, not a dir", fi.Name()) | ||
} | ||
|
||
func PrepareAssets(assets []string, rootfs string, placeholderMapping map[string]string) error { | ||
for _, asset := range assets { | ||
splitAsset := filepath.SplitList(asset) | ||
if len(splitAsset) != 2 { | ||
return fmt.Errorf("Malformed asset option: '%v' - expected two absolute paths separated with %v", asset, ListSeparator()) | ||
} | ||
ACIAsset := replacePlaceholders(splitAsset[0], placeholderMapping) | ||
localAsset := replacePlaceholders(splitAsset[1], placeholderMapping) | ||
if err := validateAsset(ACIAsset, localAsset); err != nil { | ||
return err | ||
} | ||
ACIAssetSubPath := filepath.Join(rootfs, filepath.Dir(ACIAsset)) | ||
err := os.MkdirAll(ACIAssetSubPath, 0755) | ||
if err != nil { | ||
return fmt.Errorf("Failed to create directory tree for asset '%v': %v", asset, err) | ||
} | ||
err = copyTree(localAsset, filepath.Join(rootfs, ACIAsset)) | ||
if err != nil { | ||
return fmt.Errorf("Failed to copy assets for '%v': %v", asset, err) | ||
} | ||
} | ||
return nil | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
defer close(srcFile)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for catching it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And BTW.
srcFile.Close()
;)