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:
parent
b23755c895
commit
ab5f40b59c
1 changed files with 100 additions and 47 deletions
|
@ -32,48 +32,105 @@ 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"
|
||||||
let cli = $env.docker-cli
|
container?: string@"nu-complete docker containers"
|
||||||
if $cli == 'docker' {
|
] {
|
||||||
^$cli ps -a --format '{"id":"{{.ID}}", "image": "{{.Image}}", "name":"{{.Names}}", "cmd":{{.Command}}, "port":"{{.Ports}}", "status":"{{.Status}}", "created":"{{.CreatedAt}}"}'
|
if ($container | is-empty) {
|
||||||
| lines
|
let cli = $env.docker-cli
|
||||||
| each {|x|
|
if $cli == 'docker' {
|
||||||
let r = $x | from json
|
^$cli ps -a --format '{"id":"{{.ID}}", "image": "{{.Image}}", "name":"{{.Names}}", "cmd":{{.Command}}, "port":"{{.Ports}}", "status":"{{.Status}}", "created":"{{.CreatedAt}}"}'
|
||||||
let t = $r.created | str substring ..25 | into datetime -f '%Y-%m-%d %H:%M:%S %z'
|
| lines
|
||||||
$r | upsert created $t
|
| each {|x|
|
||||||
}
|
let r = $x | from json
|
||||||
} else if $cli == 'podman' {
|
let t = $r.created | str substring ..25 | into datetime -f '%Y-%m-%d %H:%M:%S %z'
|
||||||
^$cli ps -a --format '{"id":"{{.ID}}", "image": "{{.Image}}", "name":"{{.Names}}", "cmd":"{{.Command}}", "port":"{{.Ports}}", "status":"{{.Status}}", "created":"{{.Created}}"}'
|
$r | upsert created $t
|
||||||
| lines
|
}
|
||||||
| each {|x|
|
} else if $cli == 'podman' {
|
||||||
let r = $x | from json
|
^$cli ps -a --format '{"id":"{{.ID}}", "image": "{{.Image}}", "name":"{{.Names}}", "cmd":"{{.Command}}", "port":"{{.Ports}}", "status":"{{.Status}}", "created":"{{.Created}}"}'
|
||||||
let t = $r.created | str substring ..32 | into datetime
|
| lines
|
||||||
$r | upsert created $t
|
| each {|x|
|
||||||
|
let r = $x | from json
|
||||||
|
let t = $r.created | str substring ..32 | into datetime
|
||||||
|
$r | upsert created $t
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
^$cli ($n | with-flag -n) ps -a
|
||||||
|
| from ssv
|
||||||
|
| rename id image cmd created status port name
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
^$cli ($n | with-flag -n) ps -a
|
let r = ^$env.docker-cli ($n | with-flag -n) inspect $container
|
||||||
| from ssv
|
| from json
|
||||||
| rename id image cmd created status port name
|
| 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 [
|
||||||
^$env.docker-cli ($n | with-flag -n) images
|
-n: string@"nu-complete docker ns"
|
||||||
| from ssv -a
|
img?: string@"nu-complete docker images"
|
||||||
| each {|x|
|
] {
|
||||||
let size = $x.SIZE | into filesize
|
if ($img | is-empty) {
|
||||||
let path = $x.REPOSITORY | split row '/'
|
^$env.docker-cli ($n | with-flag -n) images
|
||||||
let image = $path | last
|
| from ssv -a
|
||||||
let repo = $path | range ..(($path|length) - 2) | str join '/'
|
| each {|x|
|
||||||
|
let size = $x.SIZE | into filesize
|
||||||
|
let path = $x.REPOSITORY | split row '/'
|
||||||
|
let image = $path | last
|
||||||
|
let repo = $path | range ..(($path|length) - 2) | str join '/'
|
||||||
|
{
|
||||||
|
repo: $repo
|
||||||
|
image: $image
|
||||||
|
tag: $x.TAG
|
||||||
|
id: $x.'IMAGE ID'
|
||||||
|
created: $x.CREATED
|
||||||
|
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?
|
||||||
|
}
|
||||||
{
|
{
|
||||||
repo: $repo
|
id: $r.Id
|
||||||
image: $image
|
created: $r.Created
|
||||||
tag: $x.TAG
|
author: $r.Author
|
||||||
id: $x.'IMAGE ID'
|
arch: $r.Architecture
|
||||||
created: $x.CREATED
|
os: $r.Os
|
||||||
size: $size
|
size: $r.Size
|
||||||
|
labels: $r.Labels?
|
||||||
|
env: $e
|
||||||
|
entrypoint: $r.Config.Entrypoint
|
||||||
|
cmd: $r.Config.Cmd?
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue