Description
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 indocs/reference/api/${cmd}_types.md
, and describe the field in CommandOption
I will create single issue for each subcommand
- Refactor the apparmor command flagging process Refactor the apparmor flagging process #1774
- Refactor the build command flagging process #1689
- Refactor the commit command flagging process #1690
- Refactor the compose command flagging process #1691
- Refactor the compose build command flagging process #1692
- Refactor the compose config command flagging process #1693
- Refactor the compose create command flagging process #1694
- Refactor the compose down command flagging process #1695
- Refactor the compose exec command flagging process #1696
- Refactor the compose images command flagging process #1697
- Refactor the compose kill command flagging process #1698
- Refactor the compose log command flagging process #1699
- Refactor the compose pause command flagging process #1700
- Refactor the compose port command flagging process #1701
- Refactor the compose ps command flagging process #1702
- Refactor the compose pull command flagging process #1703
- Refactor the compose push command flagging process #1704
- Refactor the compose rm command flagging process #1705
- Refactor the compose run command flagging process #1706
- Refactor the compose start command flagging process #1707
- Refactor the compose stop command flagging process #1708
- Refactor the compose top command flagging process #1709
- Refactor the compose up command flagging process #1711
- Refactor the compose version command flagging process #1710
- Refactor the container command flagging process #1712
- Refactor the container inspect command flagging process #1713
- Refactor the container prune command flagging process #1714
- Refactor the cp command flagging process #1716
- Refactor the exec command flagging process #1715
- Refactor the history command flagging process #1717
- Refactor the image command flagging process #1718
- Refactor the image convert command flagging process #1719
- Refactor the image encrypt command flagging process #1720
- Refactor the image decrypt command flagging process #1721
- Refactor the image inspect command flagging process #1722
- Refactor the image prune command flagging process #1723
- Refactor the images command flagging process #1724
- Refactor the info command flagging process #1725
- Refactor the inspect command flagging process #1726
- Refactor the ipfs command flagging process #1727
- Refactor the ipfs registry command flagging process #1728
- Refactor the kill command flagging process #1729
- Refactor the load command flagging process #1730
- Refactor the login command flagging process #1731
- Refactor the logout command flagging process #1732
- Refactor the logs command flagging process #1734
- Refactor the namepace command flagging process #1733
- Refactor the namespace create command flagging process #1735
- Refactor the namespace rm command flagging process #1736
- Refactor the namespace update command flagging process #1737
- Refactor the network command flagging process #1738
- Refactor the network create command flagging process #1739
- Refactor the network inspect command flagging process #1740
- Refactor the network ls command flagging process #1741
- Refactor the network prune command flagging process #1742
- Refactor the network rm command flagging process #1743
- Refactor the pause command flagging process #1744
- Refactor the port command flagging process #1745
- Refactor the ps command flagging process #1746
- Refactor the pull command flagging process #1747
- Refactor the push command flagging process #1748
- Refactor the rename command flagging process #1749
- Refactor the restart command flagging process #1750
- Refactor the rm command flagging process #1751
- Refactor the rmi command flagging process #1752
- Refactor the run command flagging process #1753
- Refactor the save command flagging process #1754
- Refactor the stats command flagging process #1755
- Refactor the start command flagging process #1756
- Refactor the stop command flagging process #1757
- Refactor the system command flagging process #1758
- Refactor the unpause command flagging process #1759
- Refactor the update command flagging process #1760
- Refactor the version command flagging process #1761
- Refactor the volume command flagging process #1762
- Refactor the volume create command flagging process #1763
- Refactor the volume inspect command flagging process #1764
- Refactor the volume ls command flagging process #1765
- Refactor the volume prune command flagging process #1766
- Refactor the volume rm command flagging process #1767
- Refactor the volume wait command flagging process #1768
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.
- helper function, like https://github.com/containerd/nerdctl/blob/main/cmd/nerdctl/client.go
- shellCompletion , like https://github.com/containerd/nerdctl/blob/main/cmd/nerdctl/client.go
- 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.
- make the helper function out of the cmd package
- make the shellCompletion together
- 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