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

Refactor reusable code to ensure aws-sdk-go repository #355

Merged
merged 2 commits into from
Jul 19, 2022

Conversation

JamesJHPark
Copy link
Contributor

@JamesJHPark JamesJHPark commented Jul 18, 2022

Signed-off-by: James Park jamesjhp@amazon.com

Issue: aws-controllers-k8s/community/issues/1358

Description of changes:

  • This PR is to refactor reusable code of EnsureRepo method and its related methods into the pkg/ 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.

Signed-off-by: James Park <jamesjhp@amazon.com>
@ack-bot ack-bot requested review from a-hilaly and jaypipes July 18, 2022 18:04
Signed-off-by: James Park <jamesjhp@amazon.com>
@ack-bot
Copy link
Collaborator

ack-bot commented Jul 18, 2022

@JamesJHPark: The following tests failed, say /retest to rerun all failed tests:

Test name Commit Details Rerun command
s3-controller-test a39f703 link /test s3-controller-test
dynamodb-controller-test a39f703 link /test dynamodb-controller-test
ecr-controller-test a39f703 link /test ecr-controller-test

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.

Copy link
Collaborator

@jaypipes jaypipes left a comment

Choose a reason for hiding this comment

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

👍

Comment on lines +37 to +91
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
}
Copy link
Collaborator

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)

@jaypipes
Copy link
Collaborator

/lgtm

@ack-bot ack-bot added the lgtm Indicates that a PR is ready to be merged. label Jul 19, 2022
@ack-bot
Copy link
Collaborator

ack-bot commented Jul 19, 2022

[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 /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved lgtm Indicates that a PR is ready to be merged.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants