Skip to content

Commit

Permalink
add warning when overwriting file + fix comments
Browse files Browse the repository at this point in the history
Signed-off-by: Ludvig Liljenberg <lliljenberg@microsoft.com>
  • Loading branch information
ludfjig committed Sep 1, 2023
1 parent c4d00bd commit 8aa5a47
Showing 1 changed file with 8 additions and 27 deletions.
35 changes: 8 additions & 27 deletions pkg/porter/create.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package porter

import (
"errors"
"fmt"
"os"
"path/filepath"
Expand All @@ -23,30 +24,10 @@ func (p *Porter) Create(bundleName string) error {
// Check if the directory in which bundle needs to be created already exists.
// If not, create the directory.
_, err := os.Stat(bundleName)
if err != nil {
if os.IsNotExist(err) {
// This code here attempts to create the directory in which bundle needs to be created,
// if the directory does not exist.
// bundleName can handle both the relative path and absolute path into consideration,
// For example if we want to create a bundle named mybundle in an existing directory /home/user we can call porter create /home/user/mybundle or porter create mybundle in the /home/user directory.
// If we are in a directory /home/user and we want to create mybundle in the directory /home/user/directory given the directory exists,
// we can call porter create directory/mybundle from the /home/user directory or with any relative paths' combinations that one can come up with.
// Only condition to use porter create with absolute and relative paths is that all the directories in the path except the last one should strictly exist.
err = os.Mkdir(bundleName, os.ModePerm)
// This error message is returned when the os.Mkdir call encounters an error
// during the directory creation process. It specifically indicates that the attempt
// to create the bundle directory failed. This could occur due to reasons such as
// lack of permissions, a file system error, or if the parent directory doesn't exist.
if err != nil {
return fmt.Errorf("failed to create directory for bundle: %w", err)
}
// This error message is returned when the os.Stat call encounters an error other than
// the directory not existing. It implies that there was an issue with checking the bundle directory,
// but it doesn't mean that the directory creation itself failed.
// It could be due to various reasons, such as insufficient permissions, an invalid directory path,
// or other file system-related errors.
} else {
return fmt.Errorf("failed to check bundle directory: %w", err)
if err != nil && errors.Is(err, os.ErrNotExist) {
err = os.Mkdir(bundleName, os.ModePerm)
if err != nil {
return fmt.Errorf("failed to create directory for bundle: %w", err)
}
}

Expand Down Expand Up @@ -75,8 +56,6 @@ func (p *Porter) Create(bundleName string) error {
return err
}

fmt.Fprintf(p.Out, "creating porter configuration in %s\n", bundleName)

return p.CopyTemplate(p.Templates.GetGitignore, filepath.Join(bundleName, ".gitignore"))
}

Expand All @@ -90,7 +69,9 @@ func (p *Porter) CopyTemplate(getTemplate func() ([]byte, error), dest string) e
if filepath.Ext(dest) == ".sh" {
mode = pkg.FileModeExecutable
}

if _, err := os.Stat(dest); err == nil {
fmt.Fprintf(os.Stderr, "WARNING: File %q already exists. Overwriting.\n", dest)
}
err = p.FileSystem.WriteFile(dest, tmpl, mode)
if err != nil {
return fmt.Errorf("failed to write template to %s: %w", dest, err)
Expand Down

0 comments on commit 8aa5a47

Please sign in to comment.