mirror of
https://github.com/opencontainers/runc.git
synced 2026-06-24 08:48:44 +00:00
Migrate from urfave/cli v1 (maintenance mode) to v3 to benefit from active development, improved features, and long-term support. Signed-off-by: lifubang <lifubang@acmcoder.com>
62 lines
1.5 KiB
Go
62 lines
1.5 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"os"
|
|
|
|
"github.com/opencontainers/runc/libcontainer"
|
|
"github.com/opencontainers/runc/libcontainer/utils"
|
|
"github.com/urfave/cli/v3"
|
|
)
|
|
|
|
var stateCommand = &cli.Command{
|
|
Name: "state",
|
|
Usage: "output the state of a container",
|
|
ArgsUsage: `<container-id>
|
|
|
|
Where "<container-id>" is your name for the instance of the container.`,
|
|
Description: `The state command outputs current state information for the
|
|
instance of a container.`,
|
|
// Disable comma as separator for slice flags.
|
|
DisableSliceFlagSeparator: true,
|
|
Action: func(_ context.Context, cmd *cli.Command) error {
|
|
if err := checkArgs(cmd, 1, exactArgs); err != nil {
|
|
return err
|
|
}
|
|
container, err := getContainer(cmd)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
containerStatus, err := container.Status()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
state, err := container.State()
|
|
if err != nil {
|
|
return err
|
|
}
|
|
pid := state.BaseState.InitProcessPid
|
|
if containerStatus == libcontainer.Stopped {
|
|
pid = 0
|
|
}
|
|
bundle, annotations := utils.Annotations(state.Config.Labels)
|
|
cs := containerState{
|
|
Version: state.BaseState.Config.Version,
|
|
ID: state.BaseState.ID,
|
|
InitProcessPid: pid,
|
|
Status: containerStatus.String(),
|
|
Bundle: bundle,
|
|
Rootfs: state.BaseState.Config.Rootfs,
|
|
Created: state.BaseState.Created,
|
|
Annotations: annotations,
|
|
}
|
|
data, err := json.MarshalIndent(cs, "", " ")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
os.Stdout.Write(data)
|
|
return nil
|
|
},
|
|
}
|