From f75db6dc5f150ed62198a54f6d13f3e3e1031be6 Mon Sep 17 00:00:00 2001 From: fj0r <82698591+fj0r@users.noreply.github.com> Date: Tue, 23 Jan 2024 23:17:22 +0800 Subject: [PATCH] docker.nu : fix container-log and container-attach, add registry (#742) - fix container-log - container-attach allow passing flag - add `registry delete` --------- Co-authored-by: nash --- modules/docker/docker.nu | 43 +++++++++++++++++++++++++++++++++------- 1 file changed, 36 insertions(+), 7 deletions(-) diff --git a/modules/docker/docker.nu b/modules/docker/docker.nu index 63de58c..8894300 100644 --- a/modules/docker/docker.nu +++ b/modules/docker/docker.nu @@ -160,7 +160,7 @@ export def container-log [ctn: string@"nu-complete docker containers" -l: int = 100 # line ] { let l = if $l == 0 { [] } else { [--tail $l] } - ^$env.docker-cli logs -f $l $ctn + ^$env.docker-cli logs -f ...$l $ctn } # container log with namespace @@ -173,7 +173,7 @@ export def container-log-namespace [ctn: string@"nu-complete docker containers" } # attach container -export def container-attach [ +export def --wrapped container-attach [ ctn: string@"nu-complete docker containers" -n: string@"nu-complete docker ns" ...args @@ -413,22 +413,51 @@ def "nu-complete registry show" [cmd: string, offset: int] { } ### docker registry show -export def "registry show" [ +export def "docker registry show" [ url: string reg?: string@"nu-complete registry show" tag?: string@"nu-complete registry show" ] { - let auth = if ($env | has 'REGISTRY_TOKEN') { + let header = if ($env | has 'REGISTRY_TOKEN') { [-H $"Authorization: Basic ($env.REGISTRY_TOKEN)"] } else { [] } + | append [-H 'Accept: application/vnd.oci.image.manifest.v1+json'] if ($reg | is-empty) { - curl -sSL ...$auth $"($url)/v2/_catalog" | from json | get repositories + curl -sSL ...$header $"($url)/v2/_catalog" | from json | get repositories } else if ($tag | is-empty) { - curl -sSL ...$auth $"($url)/v2/($reg)/tags/list" | from json | get tags + curl -sSL ...$header $"($url)/v2/($reg)/tags/list" | from json | get tags } else { - curl -sSL -H 'Accept: application/vnd.oci.image.manifest.v1+json' ...$auth $"($url)/v2/($reg)/manifests/($tag)" | from json + curl -sSL ...$header $"($url)/v2/($reg)/manifests/($tag)" | from json + } +} + +### docker registry delete +export def "docker registry delete" [ + url: string + reg: string@"nu-complete registry show" + tag: string@"nu-complete registry show" +] { + let header = if ($env | has 'REGISTRY_TOKEN') { + [-H $"Authorization: Basic ($env.REGISTRY_TOKEN)"] + } else { + [] + } + | append [-H 'Accept: application/vnd.oci.image.manifest.v1+json'] + #| append [-H 'Accept: application/vnd.docker.distribution.manifest.v2+json'] + let digest = do -i { + curl -sSI ...$header $"($url)/v2/($reg)/manifests/($tag)" + | rg docker-content-digest + | split row ' ' + | get 1 + | str trim + } + print -e $digest + if not ($digest | is-empty) { + curl -sSL -X DELETE ...$header $"($url)/v2/($reg)/manifests/($digest)" + } else { + 'not found' } }