Skip to content

Add format flag to network inspect#17481

Merged
estesp merged 1 commit intomoby:masterfrom
vdemeester:17446-network-inspect-format
Dec 3, 2015
Merged

Add format flag to network inspect#17481
estesp merged 1 commit intomoby:masterfrom
vdemeester:17446-network-inspect-format

Conversation

@vdemeester
Copy link
Member

…for consistency as docker inspect and docker volume inspect supports it too 🐙.

$ docker network inspect -f "{{.Name}}" bridge host
bridge
host

Closes #17446

/ping @albers @mavenugo

🐸

Signed-off-by: Vincent Demeester [email protected]

@albers
Copy link
Member

albers commented Oct 29, 2015

Thanks, @vdemeester.

Two things, first:
The filters require capitalized names (.Name, .Containers) whereas the raw output of docker network inspect has them in lowercase (.name, .containers).
This is different from docker inspect where the unfiltered command also contains the capitalized variants (.Id).
I think the output of the unfiltered docker network inspect was implemented incorrectly with all keys being lowercased. It seems to just print the JSON.

Second, not all keys are available. .Id and .Ipam do not work:

template: :1:2: executing "" at <.Ipam>: Ipam is not a field of struct type *types.NetworkResource

@cpuguy83
Copy link
Member

@albers It's probably because it needs to be ID and IPAM

Agreed, this is stinky, really we have the same issue on docker inspect.

@cpuguy83
Copy link
Member

Maybe we can just inspect against the raw json?

@vdemeester
Copy link
Member Author

@cpuguy83 not sure what you mean on that ; reading the json and applying our template/format ?

I did the simplest implementation ever (which looks almost exactly as docker volume inspect too 😝)

@cpuguy83
Copy link
Member

Yeah, but volume inspect's json output matches the struct type...for now.

@cpuguy83
Copy link
Member

If you unmarshal to interface{} or even map[string]interface{} instead of the struct type, that's what I mean by using the raw json.

@vdemeester
Copy link
Member Author

@cpuguy83 ok, sounds pretty good to me, I'll try that 😉

@vdemeester
Copy link
Member Author

@cpuguy83 Using interface{} or map[string]interface make the json look like :

$ docker network inspect host
[
    {
        "containers": {},
        "driver": "host",
        "id": "0ef30effd29074809e8592e71b1a580f078ca12a4fe7c7f3447607df6f8087f8",
        "ipam": {
            "config": [],
            "driver": "default"
        },
        "name": "host",
        "options": {},
        "scope": "local"
    }
]

All lowercase, feels a little bit strange compared to othersinspect commands.

Edit: oh well, that's how it's specified in https://github.com/docker/docker/blob/master/api/types/types.go#L337 so.. might be ok then 😅.

@albers
Copy link
Member

albers commented Oct 30, 2015

I created an issue (#17519) that the output of docker network inspect should be changed to use the key names form the struct. This would improve consistency and should also ease implementaion of --format.

@calavera
Copy link
Contributor

The api respects the types now and we don't need to use interfaces or maps here anymore 🎉

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you shouldn't execute directly on cli.out, otherwise you might end up printing partial results.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@calavera I should put it into a buffer and push it to cli.out at the end ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes

@vdemeester vdemeester force-pushed the 17446-network-inspect-format branch from c61052f to 4de1002 Compare November 1, 2015 13:23
@vdemeester
Copy link
Member Author

Rebased against master.

@calavera did the same as api/client/volume.go inspect for now 😛.

@vdemeester vdemeester force-pushed the 17446-network-inspect-format branch from 4de1002 to 7126b87 Compare November 5, 2015 15:57
@WeiZhang555
Copy link
Contributor

I just found another issue on this, since this PR hasn't been merged yet, I think it's better to modify in this commit. docker network inspect one non-exist network is inconsistent with docker inspect and docker volume inspect, for example:

$ docker inspect aaaa
Error: No such image or container: aaaa
[]

$ docker network inspect aaaa
Error: No such network: aaaa
null

$docker volume inspect aaaa
Error response from daemon: no such volume

I think it's better for them to have consistent behavior.
As @vdemeester is also adding --format for volume, I think I can leave opinions about volume here though it's not quite relevant to this PR. 😄

@calavera
Copy link
Contributor

calavera commented Nov 6, 2015

I agree with you @WeiZhang555. I think the best consistent behavior is to not print any inspect output if the object doesn't exist, like the volume command does.

@vdemeester
Copy link
Member Author

This would mean :

$ docker network inspect aaaa bbbbb # where bbbbb exists
Error response from daemon: no such network: aaaa
[{
# // …
}]

I'm ok with that 😉. I'll update this PR to reflect that, and probably open more to get the behavior consistent on other inspect if needed.

@tianon
Copy link
Member

tianon commented Nov 15, 2015

❤️ ❤️

@calavera
Copy link
Contributor

LGTM.

It will need documentation though 😜

@vdemeester vdemeester force-pushed the 17446-network-inspect-format branch from acc9154 to 0c80a36 Compare November 18, 2015 14:58
@vdemeester
Copy link
Member Author

@calavera updated with some docs 😉

ping @thaJeztah for docs, ping @albers @sdurrheimer for completion I just added 😉

@mavenugo
Copy link
Contributor

thanks @vdemeester . LGTM

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not sufficient: As this option takes an argument, you also have to deal with that, see https://github.com/docker/docker/blob/master/contrib/completion/bash/docker#L985 for an example.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I see, will update 😉

@vdemeester vdemeester force-pushed the 17446-network-inspect-format branch from 0c80a36 to a8cdc38 Compare November 19, 2015 10:39
@vdemeester
Copy link
Member Author

Updated, thanks guys @albers @sdurrheimer

@sdurrheimer
Copy link
Contributor

LGTM

@albers
Copy link
Member

albers commented Nov 19, 2015

Thanks a lot, Vincent.
bash completion LGTM.

@jessfraz
Copy link
Contributor

jessfraz commented Dec 2, 2015

LGTM

@jessfraz
Copy link
Contributor

jessfraz commented Dec 2, 2015

moving to docs review ping @thaJeztah @moxiegirl

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add this text to the man page as well?

@thaJeztah
Copy link
Member

Thanks @vdemeester one note, but LGTM otherwise!

@moxiegirl
Copy link
Contributor

LGTM @vdemeester thank you! ⭐

…for consistency as docker inspect and docker volume inspect supports it too

Signed-off-by: Vincent Demeester <[email protected]>
@vdemeester vdemeester force-pushed the 17446-network-inspect-format branch from 0368d61 to 295c273 Compare December 2, 2015 21:32
@vdemeester
Copy link
Member Author

@thaJeztah @moxiegirl updated 😉

@thaJeztah
Copy link
Member

Thanks! LGTM!

estesp added a commit that referenced this pull request Dec 3, 2015
@estesp estesp merged commit 4d84961 into moby:master Dec 3, 2015
@vdemeester vdemeester deleted the 17446-network-inspect-format branch December 3, 2015 21:17
@thaJeztah thaJeztah added this to the 1.10.0 milestone Jan 15, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.