Skip to content

Commit 8212e8b

Browse files
dieselburnerlimdingwen
authored andcommitted
Fix ONCE environment variable ignorance
Hi there, Here's the MR to fix the issue I have faced with ONCE environment variable being ignored, when executed via Docker. One important note: this changes docker-compose-watcher (or, in fact, compose-updater) default value of once to false, so that it would be possible to remove -once=0 from Dockerfile, but still keeping the old behavior: CMD ["docker-compose-watcher", "-once=0", "-printSettings"] The reason of removing this argument from CMD above is rather simple: due to code structure command line arguments takes precedence over environment variables, which I think is the right way to go. However, keeping -once=0 will prevent ability to override behavior of passing environment variable ONCE to Docker. Basically, this MR makes everyone happy. Few other small things irrelevant to original issue comes as a bonus.
1 parent 89d3ef9 commit 8212e8b

File tree

2 files changed

+17
-9
lines changed

2 files changed

+17
-9
lines changed

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,4 +38,4 @@ RUN \
3838
curl -SLf https://github.com/docker/compose/releases/download/v${COMPOSE_VERSION}/docker-compose-linux-${DOWNLOAD_ARCH} -o /usr/local/lib/docker/cli-plugins/docker-compose && \
3939
chmod +x /usr/local/lib/docker/cli-plugins/docker-compose
4040
COPY --from=builder /go/src/app/main /usr/local/bin/docker-compose-watcher
41-
CMD ["docker-compose-watcher", "-once=0", "-printSettings"]
41+
CMD ["docker-compose-watcher", "-printSettings"]

src/settings.go

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"log"
66
"os"
77
"strconv"
8-
"strings"
98
)
109

1110
// Settings holds the program runtime configuration
@@ -34,7 +33,7 @@ func ReadSettings() {
3433
s.boolFlagEnv(&s.Dry, "dry", "DRY", false, "dry run: check and pull, but don't restart")
3534
s.boolFlagEnv(&s.Help, "help", "HELP", false, "print usage instructions")
3635
s.int64FlagEnv(&s.Interval, "interval", "INTERVAL", 60, "interval in minutes between runs")
37-
s.boolFlagEnv(&s.Once, "once", "ONCE", true, "run once and exit, do not run in background")
36+
s.boolFlagEnv(&s.Once, "once", "ONCE", false, "run once and exit, do not run in background")
3837
s.boolFlagEnv(&s.PrintSettings, "printSettings", "PRINT_SETTINGS", false, "print used settings")
3938
s.stringFlagEnv(&s.UpdateLog, "updateLog", "UPDATE_LOG", "", "update log file")
4039
//s.boolFlagEnv(&s.CompleteStop, "completeStop", "COMPLETE_STOP", false, "Restart all services in docker-compose.yml (even unmanaged) after a new image is pulled")
@@ -51,23 +50,32 @@ func ReadSettings() {
5150
func (settings *Settings) boolFlagEnv(p *bool, name string, env string, value bool, usage string) {
5251
flag.BoolVar(p, name, value, usage+" (env "+env+")")
5352
val := os.Getenv(env)
54-
if (val != "") && (val != "0") && (strings.ToLower(val) != "false") {
55-
*p = true
53+
if val != "" {
54+
b, err := strconv.ParseBool(val)
55+
if err != nil {
56+
log.Fatal(err)
57+
}
58+
*p = b
5659
}
5760
}
5861

5962
func (settings *Settings) int64FlagEnv(p *int64, name string, env string, value int64, usage string) {
6063
flag.Int64Var(p, name, value, usage+" (env "+env+")")
61-
if os.Getenv(env) != "" {
62-
i, _ := strconv.ParseInt(os.Getenv(env), 10, 0)
64+
val := os.Getenv(env)
65+
if val != "" {
66+
i, err := strconv.ParseInt(val, 10, 0)
67+
if err != nil {
68+
log.Fatal(err)
69+
}
6370
*p = i
6471
}
6572
}
6673

6774
func (settings *Settings) stringFlagEnv(p *string, name string, env string, value string, usage string) {
6875
flag.StringVar(p, name, value, usage+" (env "+env+")")
69-
if os.Getenv(env) != "" {
70-
*p = os.Getenv(env)
76+
val := os.Getenv(env)
77+
if val != "" {
78+
*p = val
7179
}
7280
}
7381

0 commit comments

Comments
 (0)