Skip to content

Commit a1378d7

Browse files
committed
return a consistent object
1 parent c6ef45a commit a1378d7

File tree

2 files changed

+35
-8
lines changed

2 files changed

+35
-8
lines changed

src/PkgAuthentication.jl

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -835,7 +835,7 @@ function open_browser(url::AbstractString)
835835
end
836836

837837
"""
838-
install(server::AbstractString; maxcount = 3)
838+
install(server::AbstractString; maxcount = 3) -> PkgAuthentication.Uninstall
839839
840840
Install Pkg authentication hooks for the Pkg server specified by `server`. Also
841841
sets the `$(pkg_server_env_var_name)` environment variable to `server`.
@@ -858,12 +858,11 @@ julia> PkgAuthentication.install("my-pkg-server.example.com"; maxcount = 5)
858858
"""
859859
function install(server::AbstractString; maxcount::Integer=3)
860860
ENV[pkg_server_env_var_name] = server
861-
install(; maxcount=maxcount)
862-
return nothing
861+
return install(; maxcount=maxcount)
863862
end
864863

865864
"""
866-
install(; maxcount = 3)
865+
install(; maxcount = 3) -> PkgAuthentication.Uninstall
867866
868867
Install Pkg authentication hooks for the Pkg server specified in the `$(pkg_server_env_var_name)`
869868
environment variable.
@@ -873,6 +872,8 @@ must be set to the URL of a valid Pkg server.
873872
874873
`maxcount` determines the number of retries.
875874
875+
You can call the returned object (no arguments) to uninstall the Pkg authentication hooks.
876+
876877
!!! compat "Julia 1.4"
877878
Pkg authentication hooks require at least Julia 1.4. On earlier versions, this
878879
method will instead force authentication immediately.
@@ -881,8 +882,10 @@ must be set to the URL of a valid Pkg server.
881882
882883
```julia
883884
julia> PkgAuthentication.install()
885+
PkgAuthentication.Uninstall (call this object to remove Pkg hooks)
884886
885887
julia> PkgAuthentication.install(; maxcount = 5)
888+
PkgAuthentication.Uninstall (call this object to remove Pkg hooks)
886889
```
887890
"""
888891
function install(; maxcount::Integer=3)
@@ -892,13 +895,15 @@ function install(; maxcount::Integer=3)
892895
_assert_pkg_server_env_var_is_set()
893896
server = String(pkg_server())
894897
auth_handler = generate_auth_handler(maxcount)
895-
@static if PkgAuthentication.is_new_auth_mechanism()
898+
uninstall_hook = @static if PkgAuthentication.is_new_auth_mechanism()
896899
Pkg.PlatformEngines.register_auth_error_handler(server, auth_handler)
897900
else
898901
# old Julia versions don't support auth hooks, so let's authenticate now and be done with it
899902
authenticate(server)
903+
# the returned Uninstall will be a silent no-op
904+
nothing
900905
end
901-
return nothing
906+
return Uninstall(uninstall_hook)
902907
end
903908

904909
function generate_auth_handler(maxcount::Integer)
@@ -923,6 +928,27 @@ function generate_auth_handler(maxcount::Integer)
923928
return auth_handler
924929
end
925930

931+
"""
932+
struct Uninstall
933+
934+
Wrapper around the closure returned by `Pkg.PlatformEngines.register_auth_error_handler`.
935+
Callin
936+
937+
```julia
938+
julia> uninstall = PkgAuthentication.install()
939+
PkgAuthentication.Uninstall (call this object to remove Pkg hooks)
940+
941+
julia> uninstall()
942+
```
943+
"""
944+
struct Uninstall
945+
f::Union{Base.Callable, Nothing}
946+
end
947+
(c::Uninstall)() = isnothing(c.f) ? nothing : c.f()
948+
function Base.show(io::IO, ::MIME"text/plain", ::Uninstall)
949+
print(io, "PkgAuthentication.Uninstall (call this object to remove Pkg hooks)")
950+
end
951+
926952
include("precompile.jl")
927953

928954
end # module

test/tests.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,16 +136,17 @@ end
136136
delete_token()
137137

138138
result = PkgAuthentication.install(test_pkg_server)
139+
@test result isa PkgAuthentication.Uninstall
139140
@static if PkgAuthentication.is_new_auth_mechanism()
140141
# On Julia 1.4+, the return value of `PkgAuthentication.install` will be
141142
# the return value from the `Pkg.PlatformEngines.register_auth_error_handler`
142143
# call. `Pkg.PlatformEngines.register_auth_error_handler` returns a zero-arg function
143144
# that can be called to deregister the handler.
144-
@test result isa Function
145+
@test result.f isa Function
145146
else
146147
# On Julia <1.4, the return value of `PkgAuthentication.install` will be
147148
# the return value from the `PkgAuthentication.authenticate` call.
148-
@test result isa PkgAuthentication.Success
149+
@test result.f isa PkgAuthentication.Success
149150
end
150151
end
151152

0 commit comments

Comments
 (0)