step011: run command
This commit is contained in:
6
internal/cmd/init.go
Normal file
6
internal/cmd/init.go
Normal file
@ -0,0 +1,6 @@
|
||||
package cmd
|
||||
|
||||
func init() {
|
||||
initRootCommand()
|
||||
initRunCommand()
|
||||
}
|
16
internal/cmd/root.go
Normal file
16
internal/cmd/root.go
Normal 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
44
internal/cmd/run.go
Normal 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
19
internal/cmd/start.go
Normal 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)
|
||||
}
|
Reference in New Issue
Block a user