Skip to content

Commit d024470

Browse files
committed
containertool: Move authentication validation into the authentication option struct
1 parent bb59bac commit d024470

File tree

1 file changed

+27
-24
lines changed

1 file changed

+27
-24
lines changed

Sources/containertool/containertool.swift

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,33 @@ enum AllowHTTP: String, ExpressibleByArgument, CaseIterable { case source, desti
102102

103103
@Option(help: "Connect to the registry using plaintext HTTP")
104104
var allowInsecureHttp: AllowHTTP?
105+
106+
mutating func validate() throws {
107+
// The `--username` and `--password` options present v1.0 were deprecated and replaced by more descriptive
108+
// `--default-username` and `--default-password`. The old names are still accepted, but specifying both the old
109+
// and the new names at the same time is ambiguous and causes an error.
110+
if username != nil {
111+
guard defaultUsername == nil else {
112+
throw ValidationError(
113+
"--default-username and --username cannot be specified together. --username is deprecated, please use --default-username instead."
114+
)
115+
}
116+
117+
log("Deprecation warning: --username is deprecated, please use --default-username instead.")
118+
defaultUsername = username
119+
}
120+
121+
if password != nil {
122+
guard defaultPassword == nil else {
123+
throw ValidationError(
124+
"--default-password and --password cannot be specified together. --password is deprecated, please use --default-password instead."
125+
)
126+
}
127+
128+
log("Deprecation warning: --password is deprecated, please use --default-password instead.")
129+
defaultPassword = password
130+
}
131+
}
105132
}
106133

107134
@OptionGroup(title: "Authentication options")
@@ -112,30 +139,6 @@ enum AllowHTTP: String, ExpressibleByArgument, CaseIterable { case source, desti
112139
@Flag(name: .shortAndLong, help: "Verbose output")
113140
private var verbose: Bool = false
114141

115-
mutating func validate() throws {
116-
if authenticationOptions.username != nil {
117-
guard authenticationOptions.defaultUsername == nil else {
118-
throw ValidationError(
119-
"--default-username and --username cannot be specified together. Please use --default-username only."
120-
)
121-
}
122-
123-
log("Deprecation warning: --username is deprecated, please use --default-username instead.")
124-
authenticationOptions.defaultUsername = authenticationOptions.username
125-
}
126-
127-
if authenticationOptions.password != nil {
128-
guard authenticationOptions.defaultPassword == nil else {
129-
throw ValidationError(
130-
"--default-password and --password cannot be specified together. Please use --default-password only."
131-
)
132-
}
133-
134-
log("Deprecation warning: --password is deprecated, please use --default-password instead.")
135-
authenticationOptions.defaultPassword = authenticationOptions.password
136-
}
137-
}
138-
139142
func run() async throws {
140143
// MARK: Apply defaults for unspecified configuration flags
141144

0 commit comments

Comments
 (0)