-
Notifications
You must be signed in to change notification settings - Fork 190
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
Refactor reusable code to ensure aws-sdk-go
repository
#355
Refactor reusable code to ensure aws-sdk-go
repository
#355
Conversation
Signed-off-by: James Park <jamesjhp@amazon.com>
Signed-off-by: James Park <jamesjhp@amazon.com>
@JamesJHPark: The following tests failed, say
Full PR test history. Your PR dashboard. Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes/test-infra repository. I understand the commands that are listed here. |
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.
👍
func ContextWithSigterm(ctx context.Context) (context.Context, context.CancelFunc) { | ||
ctx, cancel := context.WithCancel(ctx) | ||
signalCh := make(chan os.Signal, 1) | ||
|
||
// recreate the context.CancelFunc | ||
cancelFunc := func() { | ||
signal.Stop(signalCh) | ||
cancel() | ||
} | ||
|
||
// notify on SIGINT or SIGTERM | ||
signal.Notify(signalCh, os.Interrupt, syscall.SIGTERM) | ||
go func() { | ||
select { | ||
case <-signalCh: | ||
cancel() | ||
case <-ctx.Done(): | ||
} | ||
}() | ||
|
||
return ctx, cancelFunc | ||
} | ||
|
||
// EnsureDir makes sure that a supplied directory exists and | ||
// returns whether the directory already existed. | ||
func EnsureDir(fp string) (bool, error) { | ||
fi, err := os.Stat(fp) | ||
if err != nil { | ||
if os.IsNotExist(err) { | ||
return false, os.MkdirAll(fp, os.ModePerm) | ||
} | ||
return false, err | ||
} | ||
if !fi.IsDir() { | ||
return false, fmt.Errorf("expected %s to be a directory", fp) | ||
} | ||
if !isDirWriteable(fp) { | ||
return true, fmt.Errorf("%s is not a writeable directory", fp) | ||
} | ||
|
||
return true, nil | ||
} | ||
|
||
// isDirWriteable returns true if the supplied directory path is writeable, | ||
// false otherwise | ||
func isDirWriteable(fp string) bool { | ||
testPath := filepath.Join(fp, "test") | ||
f, err := os.Create(testPath) | ||
if err != nil { | ||
return false | ||
} | ||
f.Close() | ||
os.Remove(testPath) | ||
return true | ||
} |
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.
These functions I think do eventually deserve to be in the main pkg/
source code repository, because they are only using standard Go libraries (context
, filepath
, 'os
and syscall
) and are universally relevant. No need to change this particular PR, though. I will move it to pkg/
source code repository separately after this one is merged and I figure out the best name for the moved subpackage... (I'm thinking fileutil
and signalutil
)
/lgtm |
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: JamesJHPark, jaypipes The full list of commands accepted by this bot can be found here. The pull request process is described here
Needs approval from an approver in each of these files:
Approvers can indicate their approval by writing |
Signed-off-by: James Park jamesjhp@amazon.com
Issue: aws-controllers-k8s/community/issues/1358
Description of changes:
EnsureRepo
method and its related methods into thepkg/
directory of the code-generator repository.By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.