Skip to content

Commit

Permalink
Re-apply based on new go flags package (#336)
Browse files Browse the repository at this point in the history
  • Loading branch information
zoispag authored and simskij committed Jul 21, 2019
1 parent 9eca883 commit baf5e50
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 34 deletions.
24 changes: 14 additions & 10 deletions actions/actions_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@ var _ = Describe("the actions package", func() {
})
BeforeEach(func() {
client = mockClient{
api: dockerClient,
pullImages: false,
TestData: &TestData{},
api: dockerClient,
pullImages: false,
removeVolumes: false,
TestData: &TestData{},
}
})

Expand Down Expand Up @@ -62,8 +63,9 @@ var _ = Describe("the actions package", func() {
When("given multiple containers", func() {
BeforeEach(func() {
client = mockClient{
api: dockerClient,
pullImages: false,
api: dockerClient,
pullImages: false,
removeVolumes: false,
TestData: &TestData{
NameOfContainerToKeep: "test-container-02",
Containers: []container.Container{
Expand All @@ -89,8 +91,9 @@ var _ = Describe("the actions package", func() {
When("deciding whether to cleanup images", func() {
BeforeEach(func() {
client = mockClient{
api: dockerClient,
pullImages: false,
api: dockerClient,
pullImages: false,
removeVolumes: false,
TestData: &TestData{
Containers: []container.Container{
createMockContainer(
Expand Down Expand Up @@ -134,9 +137,10 @@ func createMockContainer(id string, name string, image string, created time.Time
}

type mockClient struct {
TestData *TestData
api cli.CommonAPIClient
pullImages bool
TestData *TestData
api cli.CommonAPIClient
pullImages bool
removeVolumes bool
}

type TestData struct {
Expand Down
8 changes: 4 additions & 4 deletions actions/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ var (

// UpdateParams contains all different options available to alter the behavior of the Update func
type UpdateParams struct {
Filter container.Filter
Cleanup bool
NoRestart bool
Timeout time.Duration
Filter container.Filter
Cleanup bool
NoRestart bool
Timeout time.Duration
MonitorOnly bool
}

Expand Down
21 changes: 11 additions & 10 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
package cmd

import (
"os"
"os/signal"
"strconv"
"syscall"
"time"

"github.com/containrrr/watchtower/actions"
"github.com/containrrr/watchtower/container"
"github.com/containrrr/watchtower/internal/flags"
"github.com/containrrr/watchtower/notifications"
"github.com/robfig/cron"
log "github.com/sirupsen/logrus"
"os"
"os/signal"
"strconv"
"syscall"
"time"

"github.com/spf13/cobra"
)
Expand All @@ -32,9 +33,9 @@ var (
)

var rootCmd = &cobra.Command{
Use: "watchtower",
Short: "Automatically updates running Docker containers",
Long: `
Use: "watchtower",
Short: "Automatically updates running Docker containers",
Long: `
Watchtower automatically updates running Docker containers whenever a new image is released.
More information available at https://github.com/containrrr/watchtower/.
`,
Expand Down Expand Up @@ -92,9 +93,11 @@ func PreRun(cmd *cobra.Command, args []string) {

noPull, _ := f.GetBool("no-pull")
includeStopped, _ := f.GetBool("include-stopped")
removeVolumes, _ := f.GetBool("remove-volumes")
client = container.NewClient(
!noPull,
includeStopped,
removeVolumes,
)

notifier = notifications.NewNotifier(cmd)
Expand Down Expand Up @@ -176,5 +179,3 @@ func runUpdatesWithNotifications(filter container.Filter) {
}
notifier.SendNotification()
}


13 changes: 8 additions & 5 deletions container/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package container

import (
"fmt"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/filters"
"io/ioutil"
"time"

"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/filters"

"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/network"
dockerclient "github.com/docker/docker/client"
Expand Down Expand Up @@ -35,7 +36,7 @@ type Client interface {
// * DOCKER_HOST the docker-engine host to send api requests to
// * DOCKER_TLS_VERIFY whether to verify tls certificates
// * DOCKER_API_VERSION the minimum docker api version to work with
func NewClient(pullImages bool, includeStopped bool) Client {
func NewClient(pullImages bool, includeStopped bool, removeVolumes bool) Client {
cli, err := dockerclient.NewClientWithOpts(dockerclient.FromEnv)

if err != nil {
Expand All @@ -45,13 +46,15 @@ func NewClient(pullImages bool, includeStopped bool) Client {
return dockerClient{
api: cli,
pullImages: pullImages,
removeVolumes: removeVolumes,
includeStopped: includeStopped,
}
}

type dockerClient struct {
api dockerclient.CommonAPIClient
pullImages bool
removeVolumes bool
includeStopped bool
}

Expand All @@ -71,7 +74,7 @@ func (client dockerClient) ListContainers(fn Filter) ([]Container, error) {
types.ContainerListOptions{
Filters: filter,
})

if err != nil {
return nil, err
}
Expand Down Expand Up @@ -131,7 +134,7 @@ func (client dockerClient) StopContainer(c Container, timeout time.Duration) err
} else {
log.Debugf("Removing container %s", c.ID())

if err := client.api.ContainerRemove(bg, c.ID(), types.ContainerRemoveOptions{Force: true, RemoveVolumes: false}); err != nil {
if err := client.api.ContainerRemove(bg, c.ID(), types.ContainerRemoveOptions{Force: true, RemoveVolumes: client.removeVolumes}); err != nil {
return err
}
}
Expand Down
10 changes: 10 additions & 0 deletions docs/arguments.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,16 @@ Environment Variable: WATCHTOWER_CLEANUP
Default: false
```

## Remove attached volumes
Removes attached volumes after updating. When this flag is specified, watchtower will remove all attached volumes from the container before restarting container with a new image. Use this option to force new volumes to be populated as containers are updated.

```
Argument: --remove-volumes
Environment Variable: WATCHTOWER_REMOVE_VOLUMES
Type: Boolean
Default: false
```

## Debug
Enable debug mode with verbose logging.

Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5y
github.com/golang/protobuf v1.3.1 h1:YF8+flBXS5eO826T4nzqPrxfhQThhXl0YzfuUPu4SBg=
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/google/btree v1.0.0/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ=
github.com/google/go-cmp v0.2.0 h1:+dTQ8DZQJz0Mb/HjFlkptS1FeQ4cWSnN941F8aEG4SQ=
github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M=
github.com/gorilla/mux v1.7.0 h1:tOSd0UKHQd6urX6ApfOn4XdBMY6Sh1MfxV3kmaazO+U=
github.com/gorilla/mux v1.7.0/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs=
Expand Down Expand Up @@ -258,5 +259,6 @@ gopkg.in/yaml.v2 v2.0.0-20170812160011-eb3733d160e7/go.mod h1:JAlM8MvJe8wmxCU4Bl
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gotest.tools v2.2.0+incompatible h1:VsBPFP1AI068pPrMxtb/S8Zkgf9xEmTLJjfM+P5UIEo=
gotest.tools v2.2.0+incompatible/go.mod h1:DsYFclhRJ6vuDpmuTbkuFWG+y2sxOXAzmJt81HFBacw=
honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=
14 changes: 9 additions & 5 deletions internal/flags/flags.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package flags

import (
"os"
"time"

log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"os"
"time"
)

// RegisterDockerFlags that are used directly by the docker api client
Expand Down Expand Up @@ -52,6 +53,12 @@ func RegisterSystemFlags(rootCmd *cobra.Command) {
viper.GetBool("WATCHTOWER_CLEANUP"),
"remove previously used images after updating")

flags.BoolP(
"remove-volumes",
"",
viper.GetBool("WATCHTOWER_REMOVE_VOLUMES"),
"remove attached volumes before updating")

flags.BoolP(
"label-enable",
"e",
Expand All @@ -64,7 +71,6 @@ func RegisterSystemFlags(rootCmd *cobra.Command) {
viper.GetBool("WATCHTOWER_DEBUG"),
"enable debug mode with verbose logging")


flags.BoolP(
"monitor-only",
"m",
Expand Down Expand Up @@ -253,7 +259,6 @@ func ReadFlags(cmd *cobra.Command) (bool, bool, bool, time.Duration) {
return cleanup, noRestart, monitorOnly, timeout
}


func setEnvOptStr(env string, opt string) error {
if opt == "" || opt == os.Getenv(env) {
return nil
Expand All @@ -271,4 +276,3 @@ func setEnvOptBool(env string, opt bool) error {
}
return nil
}

0 comments on commit baf5e50

Please sign in to comment.