Skip to content

Commit

Permalink
fix: incorrect path being displayed in the error when failing to crea…
Browse files Browse the repository at this point in the history
…te the temporary directory (#1033)

Signed-off-by: Harikrishnan Balagopal <[email protected]>
  • Loading branch information
HarikrishnanBalagopal authored May 9, 2023
1 parent f40ace2 commit 3fbfd87
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 29 deletions.
50 changes: 23 additions & 27 deletions common/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -899,89 +899,85 @@ func CreateAssetsData(assetsFS embed.FS, permissions map[string]int) (assetsPath
// Return the absolute version of existing asset paths.
tempPath, err = filepath.Abs(TempPath)
if err != nil {
logrus.Errorf("Unable to make the temporary directory path %q absolute. Error: %q", tempPath, err)
return "", "", "", err
return "", "", "", fmt.Errorf("failed to make the temporary directory path '%s' absolute. Error: %w", TempPath, err)
}
remoteTempPath, err = filepath.Abs(RemoteTempPath)
if err != nil {
logrus.Errorf("Unable to make the temporary directory path %q absolute. Error: %q", tempPath, err)
return "", "", "", err
return "", "", "", fmt.Errorf("failed to make the remote temporary directory path '%s' absolute. Error: %w", RemoteTempPath, err)
}
assetsPath, err = filepath.Abs(AssetsPath)
if err != nil {
logrus.Errorf("Unable to make the assets path %q absolute. Error: %q", assetsPath, err)
return "", "", "", err
return "", "", "", fmt.Errorf("failed to make the assets path '%s' absolute. Error: %w", AssetsPath, err)
}

// Try to create a new temporary directory for the assets.
if newTempPath, err := os.MkdirTemp("", types.AppName+"*"); err != nil {
logrus.Errorf("Unable to create temp dir. Defaulting to local path.")
logrus.Errorf("failed to create a temporary directory for the assets. Defaulting to the local path '%s' . Error: %q", tempPath, err)
} else {
tempPath = newTempPath
assetsPath = filepath.Join(newTempPath, AssetsDir)
}

// Try to create a new temporary directory for the remote source folders.
if newTempPath, err := os.MkdirTemp("", types.AppName+"*"); err != nil {
logrus.Errorf("Unable to create temp dir. Defaulting to local path.")
logrus.Errorf("failed to create a temporary directory for the remote sources. Defaulting to the local path '%s' . Error: %q", remoteTempPath, err)
} else {
remoteTempPath = newTempPath
}

// Either way create the subdirectory and untar the assets into it.
if err := os.MkdirAll(assetsPath, DefaultDirectoryPermission); err != nil {
logrus.Errorf("Unable to create the assets directory at path %q Error: %q", assetsPath, err)
return "", "", "", err
return "", "", "", fmt.Errorf("failed to create the assets directory at path '%s' . Error: %w", assetsPath, err)
}
if err := CopyEmbedFSToDir(assetsFS, ".", assetsPath, permissions); err != nil {
logrus.Errorf("Unable to untar the assets into the assets directory at path %q Error: %q", assetsPath, err)
return "", "", "", err
return "", "", "", fmt.Errorf("failed to untar the assets into the assets directory at path '%s' . Error: %w", assetsPath, err)
}
return assetsPath, tempPath, remoteTempPath, nil
}

// CopyEmbedFSToDir converts a string into a directory
func CopyEmbedFSToDir(embedFS embed.FS, source, dest string, permissions map[string]int) (err error) {
f, err := embedFS.Open(GetUnixPath(source))
sourceUnixPath := GetUnixPath(source)
f, err := embedFS.Open(sourceUnixPath)
if err != nil {
logrus.Errorf("Error while reading embedded file : %s", err)
return err
return fmt.Errorf("failed to open the embedded source file '%s' . Error: %w", sourceUnixPath, err)
}
finfo, err := f.Stat()
if err != nil {
logrus.Errorf("Error while reading stat of embedded file : %s", err)
return err
return fmt.Errorf("failed to stat the embedded source file '%s' . Error: %w", sourceUnixPath, err)
}
if finfo != nil && !finfo.Mode().IsDir() {
permission, ok := permissions[GetUnixPath(source)]
permission, ok := permissions[sourceUnixPath]
if !ok {
logrus.Errorf("Permission missing for file %s. Do `make generate` to update permissions file.", dest)
logrus.Errorf("permissions missing for the file '%s' . Do `make generate` to update permissions file.", dest)
}
df, err := os.OpenFile(dest, os.O_RDWR|os.O_CREATE|os.O_TRUNC, os.FileMode(permission))
if err != nil {
logrus.Errorf("Error while opening temp dest assets file : %s", err)
return err
return fmt.Errorf("failed to open the temporary dest assets file '%s' . Error: %w", dest, err)
}
defer df.Close()
size, err := io.Copy(df, f)
if err != nil {
logrus.Errorf("Error while copying embedded file : %s", err)
return err
return fmt.Errorf("failed to copy the embedded file. Error: %w", err)
}
if size != finfo.Size() {
return fmt.Errorf("size mismatch: Wrote %d, Expected %d", size, finfo.Size())
}
return nil
}
if err := os.MkdirAll(dest, DefaultDirectoryPermission); err != nil {
return err
return fmt.Errorf("failed to create the destination directory at '%s' . Error: %w", dest, err)
}
dirEntries, err := embedFS.ReadDir(GetUnixPath(source))
dirEntries, err := embedFS.ReadDir(sourceUnixPath)
if err != nil {
return err
return fmt.Errorf("failed to read the destination directory at '%s' . Error: %w", dest, err)
}
for _, de := range dirEntries {
CopyEmbedFSToDir(embedFS, filepath.Join(source, de.Name()), filepath.Join(dest, removeDollarPrefixFromHiddenDir(de.Name())), permissions)
t1Src := filepath.Join(source, de.Name())
t1Dest := filepath.Join(dest, removeDollarPrefixFromHiddenDir(de.Name()))
if err := CopyEmbedFSToDir(embedFS, t1Src, t1Dest, permissions); err != nil {
logrus.Errorf("failed to copy the embedded directory from '%s' to '%s' . Skipping. Error: %q", t1Src, t1Dest, err)
}
}
return nil
}
Expand Down
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ func main() {
assetsFilePermissions := map[string]int{}
err := yaml.Unmarshal([]byte(assets.AssetFilePermissions), &assetsFilePermissions)
if err != nil {
logrus.Fatalf("unable to convert permissions. Error: %q", err)
logrus.Fatalf("failed to unmarshal the assets permissions file as YAML. Error: %q", err)
}
assetsPath, tempPath, remoteTempPath, err := common.CreateAssetsData(assets.AssetsDir, assetsFilePermissions)
if err != nil {
logrus.Fatalf("unable to create the assets directory. Error: %q", err)
logrus.Fatalf("failed to create the assets directory. Error: %q", err)
}
common.TempPath = tempPath
common.AssetsPath = assetsPath
Expand Down

0 comments on commit 3fbfd87

Please sign in to comment.