Skip to content

Commit 5f1c183

Browse files
committed
Allow explicit docker login errors to be ignored
* Add support for suppressing errors triggered by explicit calls to `docker login` and `docker logout` by setting the `IGNORE_DOCKER_LOGIN` environment variable.
1 parent e04a7eb commit 5f1c183

File tree

3 files changed

+33
-4
lines changed

3 files changed

+33
-4
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ The `docker-credential-env` binary must be installed to `$PATH`, configured via
3939
}
4040
```
4141

42+
By default, attempts to explicitly `docker {login,logout}` will generate an error. To ignore these errors, set the environment variable `IGNORE_DOCKER_LOGIN=1`.
43+
4244
## Example Usage
4345

4446
### Jenkins

env.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ const (
2626
envUsernameSuffix = "USR"
2727
envPasswordSuffix = "PSW"
2828
envSeparator = "_"
29+
envIgnoreLogin = "IGNORE_DOCKER_LOGIN"
2930
)
3031

3132
type NotSupportedError struct{}
@@ -39,12 +40,22 @@ type Env struct{}
3940

4041
// Add implements the set verb
4142
func (*Env) Add(*docker_credentials.Credentials) error {
42-
return fmt.Errorf("add: %w", &NotSupportedError{})
43+
switch {
44+
case os.Getenv(envIgnoreLogin) != "":
45+
return nil
46+
default:
47+
return fmt.Errorf("add: %w", &NotSupportedError{})
48+
}
4349
}
4450

4551
// Delete implements the erase verb
4652
func (*Env) Delete(string) error {
47-
return fmt.Errorf("delete: %w", &NotSupportedError{})
53+
switch {
54+
case os.Getenv(envIgnoreLogin) != "":
55+
return nil
56+
default:
57+
return fmt.Errorf("delete: %w", &NotSupportedError{})
58+
}
4859
}
4960

5061
// List implements the list verb

env_test.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,17 +213,33 @@ func TestEnvNotSupportedMethods(t *testing.T) {
213213
}
214214
})
215215

216+
t.Run("Add is ignored with IGNORE_DOCKER_LOGIN", func(t *testing.T) {
217+
t.Setenv(envIgnoreLogin, "1")
218+
actualErr := e.Add(nil)
219+
if actualErr != nil {
220+
t.Errorf("Add() actual = (%v), expected (%v)", actualErr, nil)
221+
}
222+
})
223+
216224
t.Run("Delete is not supported", func(t *testing.T) {
217225
actualErr := e.Delete("")
218226
if !errors.Is(actualErr, &NotSupportedError{}) {
219-
t.Errorf("Add() actual = (%v), expected (%v)", actualErr, &NotSupportedError{})
227+
t.Errorf("Delete() actual = (%v), expected (%v)", actualErr, &NotSupportedError{})
228+
}
229+
})
230+
231+
t.Run("Delete is ignored with IGNORE_DOCKER_LOGIN", func(t *testing.T) {
232+
t.Setenv(envIgnoreLogin, "1")
233+
actualErr := e.Delete("")
234+
if actualErr != nil {
235+
t.Errorf("Delete() actual = (%v), expected (%v)", actualErr, nil)
220236
}
221237
})
222238

223239
t.Run("List is not supported", func(t *testing.T) {
224240
_, actualErr := e.List()
225241
if !errors.Is(actualErr, &NotSupportedError{}) {
226-
t.Errorf("Add() actual = (%v), expected (%v)", actualErr, &NotSupportedError{})
242+
t.Errorf("List() actual = (%v), expected (%v)", actualErr, &NotSupportedError{})
227243
}
228244
})
229245
}

0 commit comments

Comments
 (0)