step011: run command

This commit is contained in:
root
2024-03-20 19:14:31 -07:00
commit 2893ff3c21
11 changed files with 234 additions and 0 deletions

6
internal/cmd/init.go Normal file
View File

@ -0,0 +1,6 @@
package cmd
func init() {
initRootCommand()
initRunCommand()
}

16
internal/cmd/root.go Normal file
View File

@ -0,0 +1,16 @@
package cmd
import (
"github.com/spf13/cobra"
)
var (
rootCommand = &cobra.Command{
Use: "upod",
Short: "upod is a simple container runtime implementation",
}
)
func initRootCommand() {
}

44
internal/cmd/run.go Normal file
View File

@ -0,0 +1,44 @@
package cmd
import (
"errors"
"os"
"upod/internal/container"
"upod/internal/log"
"github.com/spf13/cobra"
)
var (
runCommand = &cobra.Command{
Use: "run",
Short: "upod run [OPTIONS] IMAGE [COMMAND] [ARG...]",
RunE: func(ctx *cobra.Command, args []string) error {
var (
err error
)
log.Debug("runCommand: args=%v", args)
if len(args) == 0 {
return errors.New("upod run requires at least 1 argument")
}
cmd := container.NewParentProcess(true, args[0])
if err = cmd.Start(); err != nil {
return err
}
cmd.Wait()
os.Exit(0)
return nil
},
}
)
func initRunCommand() {
rootCommand.AddCommand(runCommand)
}

19
internal/cmd/start.go Normal file
View File

@ -0,0 +1,19 @@
package cmd
import (
"context"
"os"
"upod/internal/container"
"upod/internal/log"
)
func Start(ctx context.Context) error {
if len(os.Args) >= 3 && os.Args[1] == "init" {
log.Debug("cmd.Start: init args=%v", os.Args)
container.RunContainerInitProcess(os.Args[2], os.Args[2:])
}
return rootCommand.ExecuteContext(ctx)
}