Skip to content

Commit c18c118

Browse files
authored
Return a callable object from install() (#53)
1 parent 9dd3c7c commit c18c118

File tree

3 files changed

+49
-6
lines changed

3 files changed

+49
-6
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "PkgAuthentication"
22
uuid = "4722fa14-9d28-45f9-a1e2-a38605bd88f0"
33
authors = ["Sebastian Pfitzner", "contributors"]
4-
version = "2.2.3"
4+
version = "2.3.0"
55

66
[deps]
77
Downloads = "f43a241f-c20a-4ad4-852c-f6b1247861c6"

src/PkgAuthentication.jl

Lines changed: 31 additions & 3 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`.
@@ -862,7 +862,7 @@ function install(server::AbstractString; maxcount::Integer=3)
862862
end
863863

864864
"""
865-
install(; maxcount = 3)
865+
install(; maxcount = 3) -> PkgAuthentication.Uninstall
866866
867867
Install Pkg authentication hooks for the Pkg server specified in the `$(pkg_server_env_var_name)`
868868
environment variable.
@@ -872,6 +872,8 @@ must be set to the URL of a valid Pkg server.
872872
873873
`maxcount` determines the number of retries.
874874
875+
You can call the returned object (no arguments) to uninstall the Pkg authentication hooks.
876+
875877
!!! compat "Julia 1.4"
876878
Pkg authentication hooks require at least Julia 1.4. On earlier versions, this
877879
method will instead force authentication immediately.
@@ -880,8 +882,10 @@ must be set to the URL of a valid Pkg server.
880882
881883
```julia
882884
julia> PkgAuthentication.install()
885+
PkgAuthentication.Uninstall (call this object to remove Pkg hooks)
883886
884887
julia> PkgAuthentication.install(; maxcount = 5)
888+
PkgAuthentication.Uninstall (call this object to remove Pkg hooks)
885889
```
886890
"""
887891
function install(; maxcount::Integer=3)
@@ -891,12 +895,15 @@ function install(; maxcount::Integer=3)
891895
_assert_pkg_server_env_var_is_set()
892896
server = String(pkg_server())
893897
auth_handler = generate_auth_handler(maxcount)
894-
@static if PkgAuthentication.is_new_auth_mechanism()
898+
uninstall_hook = @static if PkgAuthentication.is_new_auth_mechanism()
895899
Pkg.PlatformEngines.register_auth_error_handler(server, auth_handler)
896900
else
897901
# old Julia versions don't support auth hooks, so let's authenticate now and be done with it
898902
authenticate(server)
903+
# the returned Uninstall will be a silent no-op
904+
nothing
899905
end
906+
return Uninstall(uninstall_hook)
900907
end
901908

902909
function generate_auth_handler(maxcount::Integer)
@@ -921,6 +928,27 @@ function generate_auth_handler(maxcount::Integer)
921928
return auth_handler
922929
end
923930

931+
"""
932+
struct Uninstall
933+
934+
Wrapper around the closure returned by `Pkg.PlatformEngines.register_auth_error_handler`.
935+
Calling this object will remove the Pkg authentication hooks.
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+
924952
include("precompile.jl")
925953

926954
end # module

test/tests.jl

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,16 +136,31 @@ 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 isnothing(result.f)
150+
end
151+
152+
@testset "PkgAuthentication.Uninstall" begin
153+
let u = PkgAuthentication.Uninstall(nothing)
154+
@test u() === nothing
155+
end
156+
let count = 1
157+
u = PkgAuthentication.Uninstall(() -> (count += 1; nothing))
158+
@test count == 1
159+
@test u() === nothing
160+
@test count == 2
161+
@test u() === nothing
162+
@test count == 3
163+
end
149164
end
150165
end
151166

0 commit comments

Comments
 (0)