1
Fork 0
mirror of https://github.com/RGBCube/nu_scripts synced 2025-08-02 07:07:46 +00:00

merge container-inspect into container-process-list (#711)

The behavior of container-process-list and image-list when argument is
passed in is to `inspect`

Co-authored-by: nash <nash@iffy.me>
This commit is contained in:
fj0r 2023-12-22 21:04:26 +08:00 committed by GitHub
parent b23755c895
commit ab5f40b59c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -32,8 +32,11 @@ def "nu-complete docker ns" [] {
} }
# list containers # list containers
export def container-process-list [-n: string@"nu-complete docker ns"] { export def container-process-list [
# ^$env.docker-cli ps --all --no-trunc --format='{{json .}}' | jq -n: string@"nu-complete docker ns"
container?: string@"nu-complete docker containers"
] {
if ($container | is-empty) {
let cli = $env.docker-cli let cli = $env.docker-cli
if $cli == 'docker' { if $cli == 'docker' {
^$cli ps -a --format '{"id":"{{.ID}}", "image": "{{.Image}}", "name":"{{.Names}}", "cmd":{{.Command}}, "port":"{{.Ports}}", "status":"{{.Status}}", "created":"{{.CreatedAt}}"}' ^$cli ps -a --format '{"id":"{{.ID}}", "image": "{{.Image}}", "name":"{{.Names}}", "cmd":{{.Command}}, "port":"{{.Ports}}", "status":"{{.Status}}", "created":"{{.CreatedAt}}"}'
@ -56,10 +59,43 @@ export def container-process-list [-n: string@"nu-complete docker ns"] {
| from ssv | from ssv
| rename id image cmd created status port name | rename id image cmd created status port name
} }
} else {
let r = ^$env.docker-cli ($n | with-flag -n) inspect $container
| from json
| get 0
#let e = $r.Config.Env | reduce -f {} {|i, a|
# let x = $i | split row '='
# $a | upsert $x.0 $x.1?
#}
let m = $r.Mounts
| reduce -f {} {|i, a|
if $i.Type == 'bind' {
$a | upsert $i.Source? $i.Destination?
} else { $a }
}
let p = $r.NetworkSettings.Ports? | default {} | transpose k v
| reduce -f {} {|i, a| $a | upsert $i.k $"($i.v.HostIp?.0?):($i.v.HostPort?.0?)"}
{
name: $r.Name?
hostname: $r.Config.Hostname?
image: $r.Image
created: $r.Created
id: $r.Id
ports: $p
# FIXME: env
mounts: $m
path: $r.Path
args: $r.Args
}
}
} }
# list images # list images
export def image-list [-n: string@"nu-complete docker ns"] { export def image-list [
-n: string@"nu-complete docker ns"
img?: string@"nu-complete docker images"
] {
if ($img | is-empty) {
^$env.docker-cli ($n | with-flag -n) images ^$env.docker-cli ($n | with-flag -n) images
| from ssv -a | from ssv -a
| each {|x| | each {|x|
@ -76,6 +112,27 @@ export def image-list [-n: string@"nu-complete docker ns"] {
size: $size size: $size
} }
} }
} else {
let r = ^$env.docker-cli ($n | with-flag -n) inspect $img
| from json
| get 0
let e = $r.Config.Env | reduce -f {} {|i, a|
let x = $i | split row '='
$a | upsert $x.0 $x.1?
}
{
id: $r.Id
created: $r.Created
author: $r.Author
arch: $r.Architecture
os: $r.Os
size: $r.Size
labels: $r.Labels?
env: $e
entrypoint: $r.Config.Entrypoint
cmd: $r.Config.Cmd?
}
}
} }
def "nu-complete docker ps" [] { def "nu-complete docker ps" [] {
@ -84,13 +141,13 @@ def "nu-complete docker ps" [] {
| each {|x| {description: $x.NAMES value: $x.'CONTAINER ID'}} | each {|x| {description: $x.NAMES value: $x.'CONTAINER ID'}}
} }
def "nu-complete docker container" [] { def "nu-complete docker containers" [] {
^$env.docker-cli ps ^$env.docker-cli ps
| from ssv -a | from ssv -a
| each {|x| {description: $x.'CONTAINER ID' value: $x.NAMES}} | each {|x| {description: $x.'CONTAINER ID' value: $x.NAMES}}
} }
def "nu-complete docker all container" [] { def "nu-complete docker all containers" [] {
^$env.docker-cli ps -a ^$env.docker-cli ps -a
| from ssv -a | from ssv -a
| each {|x| {description: $x.'CONTAINER ID' value: $x.NAMES}} | each {|x| {description: $x.'CONTAINER ID' value: $x.NAMES}}
@ -103,7 +160,7 @@ def "nu-complete docker images" [] {
} }
# container log # container log
export def container-log [ctn: string@"nu-complete docker container" export def container-log [ctn: string@"nu-complete docker containers"
-l: int = 100 # line -l: int = 100 # line
] { ] {
let l = if $l == 0 { [] } else { [--tail $l] } let l = if $l == 0 { [] } else { [--tail $l] }
@ -111,7 +168,7 @@ export def container-log [ctn: string@"nu-complete docker container"
} }
# container log with namespace # container log with namespace
export def container-log-namespace [ctn: string@"nu-complete docker container" export def container-log-namespace [ctn: string@"nu-complete docker containers"
-l: int = 100 # line -l: int = 100 # line
-n: string@"nu-complete docker ns" # namespace -n: string@"nu-complete docker ns" # namespace
] { ] {
@ -121,7 +178,7 @@ export def container-log-namespace [ctn: string@"nu-complete docker container"
# attach container # attach container
export def container-attach [ export def container-attach [
ctn: string@"nu-complete docker container" ctn: string@"nu-complete docker containers"
-n: string@"nu-complete docker ns" -n: string@"nu-complete docker ns"
...args ...args
] { ] {
@ -162,20 +219,17 @@ export def container-copy-file [
} }
# remove container # remove container
export def container-remove [ctn: string@"nu-complete docker all container" -n: string@"nu-complete docker ns"] { export def container-remove [ctn: string@"nu-complete docker all containers" -n: string@"nu-complete docker ns"] {
^$env.docker-cli ($n | with-flag -n) container rm -f $ctn ^$env.docker-cli ($n | with-flag -n) container rm -f $ctn
} }
# inspect
export def container-inspect [img: string@"nu-complete docker images" -n: string@"nu-complete docker ns"] {
^$env.docker-cli ($n | with-flag -n) inspect $img
}
# history # history
export def container-history [img: string@"nu-complete docker images" -n: string@"nu-complete docker ns"] { export def container-history [img: string@"nu-complete docker images" -n: string@"nu-complete docker ns"] {
^$env.docker-cli ($n | with-flag -n) history --no-trunc $img | from ssv -a ^$env.docker-cli ($n | with-flag -n) history --no-trunc $img | from ssv -a
} }
# save images # save images
export def image-save [-n: string@"nu-complete docker ns" ...img: string@"nu-complete docker images"] { export def image-save [-n: string@"nu-complete docker ns" ...img: string@"nu-complete docker images"] {
^$env.docker-cli ($n | with-flag -n) save $img ^$env.docker-cli ($n | with-flag -n) save $img
@ -284,7 +338,7 @@ export def container-create [
--ports(-p): any # { 8080: 80 } --ports(-p): any # { 8080: 80 }
--envs(-e): any # { FOO: BAR } --envs(-e): any # { FOO: BAR }
--daemon(-d) --daemon(-d)
--attach(-a): string@"nu-complete docker container" # attach --attach(-a): string@"nu-complete docker containers" # attach
--workdir(-w): string # workdir --workdir(-w): string # workdir
--entrypoint: string # entrypoint --entrypoint: string # entrypoint
--dry-run --dry-run
@ -422,7 +476,6 @@ export alias dln = container-log-namespace
export alias da = container-attach export alias da = container-attach
export alias dcp = container-copy-file export alias dcp = container-copy-file
export alias dcr = container-remove export alias dcr = container-remove
export alias dci = container-inspect
export alias dh = container-history export alias dh = container-history
export alias dsv = image-save export alias dsv = image-save
export alias dld = image-load export alias dld = image-load