Skip to content

Commit

Permalink
don't export terminateOptions
Browse files Browse the repository at this point in the history
  • Loading branch information
moogacs committed Dec 25, 2024
1 parent 9e111b7 commit 31203a9
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 19 deletions.
22 changes: 11 additions & 11 deletions cleanup.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,20 @@ import (
// DefaultTimeout for termination
var DefaultTimeout = 10 * time.Second

// TerminateOptions is a type that holds the options for terminating a container.
type TerminateOptions struct {
// terminateOptions is a type that holds the options for terminating a container.
type terminateOptions struct {
ctx context.Context
stopTimeout *time.Duration
volumes []string
}

// TerminateOption is a type that represents an option for terminating a container.
type TerminateOption func(*TerminateOptions)
type TerminateOption func(*terminateOptions)

// NewTerminateOptions returns a fully initialised TerminateOptions.
// Defaults: StopTimeout: 10 seconds.
func NewTerminateOptions(ctx context.Context, opts ...TerminateOption) *TerminateOptions {
options := &TerminateOptions{
func NewTerminateOptions(ctx context.Context, opts ...TerminateOption) *terminateOptions {
options := &terminateOptions{
stopTimeout: &DefaultTimeout,
ctx: ctx,
}
Expand All @@ -35,17 +35,17 @@ func NewTerminateOptions(ctx context.Context, opts ...TerminateOption) *Terminat
}

// Context returns the context to use duration a Terminate.
func (o *TerminateOptions) Context() context.Context {
func (o *terminateOptions) Context() context.Context {
return o.ctx
}

// StopTimeout returns the stop timeout to use duration a Terminate.
func (o *TerminateOptions) StopTimeout() *time.Duration {
func (o *terminateOptions) StopTimeout() *time.Duration {
return o.stopTimeout
}

// Cleanup performs any clean up needed
func (o *TerminateOptions) Cleanup() error {
func (o *terminateOptions) Cleanup() error {
// TODO: simplify this when when perform the client refactor.
if len(o.volumes) == 0 {
return nil
Expand All @@ -68,15 +68,15 @@ func (o *TerminateOptions) Cleanup() error {
// StopContext returns a TerminateOption that sets the context.
// Default: context.Background().
func StopContext(ctx context.Context) TerminateOption {
return func(c *TerminateOptions) {
return func(c *terminateOptions) {
c.ctx = ctx
}
}

// StopTimeout returns a TerminateOption that sets the timeout.
// Default: See [Container.Stop].
func StopTimeout(timeout time.Duration) TerminateOption {
return func(c *TerminateOptions) {
return func(c *terminateOptions) {
c.stopTimeout = &timeout
}
}
Expand All @@ -86,7 +86,7 @@ func StopTimeout(timeout time.Duration) TerminateOption {
// which are not removed by default.
// Default: nil.
func RemoveVolumes(volumes ...string) TerminateOption {
return func(c *TerminateOptions) {
return func(c *terminateOptions) {
c.volumes = volumes
}
}
Expand Down
4 changes: 2 additions & 2 deletions docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -298,14 +298,14 @@ func (c *DockerContainer) Stop(ctx context.Context, timeout *time.Duration) erro

// WithStopTimeout is a functional option that sets the timeout for the container termination.
func WithStopTimeout(timeout time.Duration) TerminateOption {
return func(c *TerminateOptions) {
return func(c *terminateOptions) {
c.stopTimeout = &timeout
}
}

// WithTerminateVolumes is a functional option that sets the volumes for the container termination.
func WithTerminateVolumes(volumes ...string) TerminateOption {
return func(opts *TerminateOptions) {
return func(opts *terminateOptions) {
opts.volumes = volumes
}
}
Expand Down
12 changes: 6 additions & 6 deletions docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1134,27 +1134,27 @@ func TestContainerCreationWithVolumeCleaning(t *testing.T) {

func TestContainerTerminationOptions(t *testing.T) {
t.Run("volumes", func(t *testing.T) {
var options TerminateOptions
var options terminateOptions
WithTerminateVolumes("vol1", "vol2")(&options)
require.Equal(t, TerminateOptions{
require.Equal(t, terminateOptions{
volumes: []string{"vol1", "vol2"},
}, options)
})
t.Run("stop-timeout", func(t *testing.T) {
var options TerminateOptions
var options terminateOptions
timeout := 11 * time.Second
WithStopTimeout(timeout)(&options)
require.Equal(t, TerminateOptions{
require.Equal(t, terminateOptions{
stopTimeout: &timeout,
}, options)
})

t.Run("all", func(t *testing.T) {
var options TerminateOptions
var options terminateOptions
timeout := 9 * time.Second
WithStopTimeout(timeout)(&options)
WithTerminateVolumes("vol1", "vol2")(&options)
require.Equal(t, TerminateOptions{
require.Equal(t, terminateOptions{
stopTimeout: &timeout,
volumes: []string{"vol1", "vol2"},
}, options)
Expand Down

0 comments on commit 31203a9

Please sign in to comment.