Skip to content

Let's refactor the nerdctl CLI package #1680

Open
@Zheaoli

Description

@Zheaoli

Update in 2022-12-26

After some discussion on this issue, most of the nerdctl maintainers reach the same point.

We need to refactor the command flagging process. After the refactor process, all logic in cmd package should only validate cobra flags and construct the CommandOption.

Refactor Check List:

  • Create a file in pkg/api/types/${cmd}_types.go, and define the CommandOption for this command
  • Create some file in pkg/cmd/${cmd}, and move the command entry point in real into this package
  • [ ] Create new docs in docs/reference/api/${cmd}_types.md, and describe the field in CommandOption

I will create single issue for each subcommand

What is the problem you're trying to solve

OK, I'm back!

For now, the cmd package has a lot of go files mixed. It takes a lot of work to keep maintainer to maintain this project.

And in the community, people may need to add some features of their own, like #1631, so it's time to refactor the command package!

Describe the solution you'd like

I have made a previous PR, #1639; it's a nasty case for the community.

We may need to make some changes to make this issue happen.

I want to split this issue into three stages.

First: we need to decompose the flagging process in the command entry point

For now, we mix the flagging register and flagging process, like https://github.com/containerd/nerdctl/blob/main/cmd/nerdctl/exec.go#L53-L62

It means we can not split the common logic unrelated to cobra.Command, and care about the flag value from the current cmd package.

We can take a look at some projects in the community alike https://github.com/prometheus/prometheus/blob/main/cmd/prometheus/main.go#L127-L159 and
https://github.com/containers/podman/blob/main/cmd/podman/containers/attach.go#L45-L54, we will find out that we may collect the flag value into a centralize struct, so we do not need care about the cobra.Command in the entry point in real.

Second: split the common function into another package

For now, the cmd package maybe can be grouped into three types.

  1. helper function, like https://github.com/containerd/nerdctl/blob/main/cmd/nerdctl/client.go
  2. shellCompletion , like https://github.com/containerd/nerdctl/blob/main/cmd/nerdctl/client.go
  3. the logic function, mixed with the flag process logic, like https://github.com/containerd/nerdctl/blob/main/cmd/nerdctl/apparmor_ls_linux.go#L56

In this stage, we need to do three things.

  1. make the helper function out of the cmd package
  2. make the shellCompletion together
  3. after stage1, we can split the logic function into the pkg/ (for now, we don't need care about the cobra action in the logic function now!)

Third: restructed the cmd package by the subcommand

And now, we can restruct the cmd package by subcommand. The code in the cmd package has already been cleaned and will focus on the logic to process the flag. So the restruct process will be easy.

And the final problem in this stage the integration test would be a problem. For most circumstances, the integration test may be written with the command. But here's some in issue in go test, the go test command will run the test in different directory parallel, so there would be some potential conflict in this circumstance. I would prefer to make all the integration tests into a single directory together.

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions