diff --git a/build.sh b/build.sh index 304786d..78b1bfc 100755 --- a/build.sh +++ b/build.sh @@ -1,5 +1,9 @@ #!/bin/bash +BINFILE=watchtower +if [ -n "$MSYSTEM" ]; then + BINFILE=watchtower.exe +fi VERSION=$(git describe --tags) echo "Building $VERSION..." -go build -o watchtower -ldflags "-X github.com/containrrr/watchtower/internal/meta.Version=$VERSION" +go build -o $BINFILE -ldflags "-X github.com/containrrr/watchtower/internal/meta.Version=$VERSION" diff --git a/internal/flags/flags.go b/internal/flags/flags.go index 664b669..2af8678 100644 --- a/internal/flags/flags.go +++ b/internal/flags/flags.go @@ -1,6 +1,7 @@ package flags import ( + "errors" "io/ioutil" "os" "strings" @@ -468,9 +469,13 @@ func getSecretFromFile(flags *pflag.FlagSet, secret string) { } func isFile(s string) bool { - _, err := os.Stat(s) - if os.IsNotExist(err) { + firstColon := strings.IndexRune(s, ':') + if firstColon != 1 && firstColon != -1 { + // If the string contains a ':', but it's not the second character, it's probably not a file + // and will cause a fatal error on windows if stat'ed + // This still allows for paths that start with 'c:\' etc. return false } - return true + _, err := os.Stat(s) + return !errors.Is(err, os.ErrNotExist) } diff --git a/internal/flags/flags_test.go b/internal/flags/flags_test.go index e298622..1a904dd 100644 --- a/internal/flags/flags_test.go +++ b/internal/flags/flags_test.go @@ -11,6 +11,10 @@ import ( ) func TestEnvConfig_Defaults(t *testing.T) { + // Unset testing environments own variables, since those are not what is under test + os.Unsetenv("DOCKER_TLS_VERIFY") + os.Unsetenv("DOCKER_HOST") + cmd := new(cobra.Command) SetDefaults() RegisterDockerFlags(cmd) @@ -94,3 +98,8 @@ func TestHTTPAPIPeriodicPollsFlag(t *testing.T) { assert.Equal(t, true, periodicPolls) } + +func TestIsFile(t *testing.T) { + assert.False(t, isFile("https://google.com"), "an URL should never be considered a file") + assert.True(t, isFile(os.Args[0]), "the currently running binary path should always be considered a file") +}