mirror of
https://github.com/nestriness/cli.git
synced 2025-12-12 20:25:36 +02:00
✨ feat: Use last argument to contain game executable
This commit is contained in:
125
cmd/root.go
125
cmd/root.go
@@ -4,7 +4,6 @@ Copyright © 2024 Nestri <>
|
|||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
|
||||||
_ "embed"
|
_ "embed"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
@@ -13,9 +12,6 @@ import (
|
|||||||
|
|
||||||
"github.com/charmbracelet/lipgloss"
|
"github.com/charmbracelet/lipgloss"
|
||||||
"github.com/charmbracelet/lipgloss/table"
|
"github.com/charmbracelet/lipgloss/table"
|
||||||
"github.com/docker/docker/api/types/container"
|
|
||||||
"github.com/docker/docker/client"
|
|
||||||
"github.com/docker/docker/pkg/stdcopy"
|
|
||||||
"github.com/muesli/termenv"
|
"github.com/muesli/termenv"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
@@ -23,6 +19,17 @@ import (
|
|||||||
//go:embed nestri.ascii
|
//go:embed nestri.ascii
|
||||||
var art string
|
var art string
|
||||||
|
|
||||||
|
type GameConfig struct {
|
||||||
|
Directory string
|
||||||
|
Executable string
|
||||||
|
GPU int
|
||||||
|
Vendor string
|
||||||
|
Resolution struct {
|
||||||
|
Height int
|
||||||
|
Width int
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// rootCmd represents the base command when called without any subcommands
|
// rootCmd represents the base command when called without any subcommands
|
||||||
var rootCmd = &cobra.Command{
|
var rootCmd = &cobra.Command{
|
||||||
Use: "nestri",
|
Use: "nestri",
|
||||||
@@ -80,7 +87,7 @@ var neoFetchCmd = &cobra.Command{
|
|||||||
|
|
||||||
// this is the "nestri run" subcommand, takes no arguments for now
|
// this is the "nestri run" subcommand, takes no arguments for now
|
||||||
var runCmd = &cobra.Command{
|
var runCmd = &cobra.Command{
|
||||||
Use: "run",
|
Use: "run [options] [game]",
|
||||||
Short: "Run a game using nestri",
|
Short: "Run a game using nestri",
|
||||||
Args: cobra.MaximumNArgs(1),
|
Args: cobra.MaximumNArgs(1),
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
@@ -98,6 +105,42 @@ var runCmd = &cobra.Command{
|
|||||||
//5. Run the game
|
//5. Run the game
|
||||||
//6. Provide the URL to play or throw an error otherwise.
|
//6. Provide the URL to play or throw an error otherwise.
|
||||||
|
|
||||||
|
// The last argument is the game to run
|
||||||
|
game := args[len(args)-1]
|
||||||
|
|
||||||
|
// Load game configuration
|
||||||
|
// var gameConfig GameConfig
|
||||||
|
// if err := viper.UnmarshalKey(fmt.Sprintf("games.%s", game), &gameConfig); err != nil {
|
||||||
|
// return fmt.Errorf("error parsing game configuration: %w", err)
|
||||||
|
// }
|
||||||
|
|
||||||
|
// // Override config with command-line flags
|
||||||
|
// cmd.Flags().Visit(func(f *pflag.Flag) {
|
||||||
|
// switch f.Name {
|
||||||
|
// case "directory":
|
||||||
|
// gameConfig.Directory = viper.GetString(f.Name)
|
||||||
|
// case "executable":
|
||||||
|
// gameConfig.Executable = viper.GetString(f.Name)
|
||||||
|
// case "gpu":
|
||||||
|
// gameConfig.GPU = viper.GetInt(f.Name)
|
||||||
|
// case "vendor":
|
||||||
|
// gameConfig.Vendor = viper.GetString(f.Name)
|
||||||
|
// case "resolution-height":
|
||||||
|
// gameConfig.Resolution.Height = viper.GetInt(f.Name)
|
||||||
|
// case "resolution-width":
|
||||||
|
// gameConfig.Resolution.Width = viper.GetInt(f.Name)
|
||||||
|
// }
|
||||||
|
// })
|
||||||
|
|
||||||
|
fmt.Printf("Running game: %s\n\n", game)
|
||||||
|
|
||||||
|
// cli, err := client.NewClientWithOpts(client.FromEnv)
|
||||||
|
// if err != nil {
|
||||||
|
// return fmt.Errorf("error creating Docker client: %w", err)
|
||||||
|
// }
|
||||||
|
|
||||||
|
// ctx := context.Background()
|
||||||
|
|
||||||
// var game string
|
// var game string
|
||||||
// if len(args) > 0 {
|
// if len(args) > 0 {
|
||||||
// game = args[0]
|
// game = args[0]
|
||||||
@@ -115,48 +158,48 @@ var runCmd = &cobra.Command{
|
|||||||
|
|
||||||
// fmt.Printf("Running game: %s\n\n", game)
|
// fmt.Printf("Running game: %s\n\n", game)
|
||||||
|
|
||||||
cli, err := client.NewClientWithOpts()
|
// cli, err := client.NewClientWithOpts()
|
||||||
if err != nil {
|
// if err != nil {
|
||||||
panic(err)
|
// panic(err)
|
||||||
}
|
// }
|
||||||
|
|
||||||
ctx := context.Background()
|
// ctx := context.Background()
|
||||||
resp, err := cli.ContainerCreate(ctx, &container.Config{
|
// resp, err := cli.ContainerCreate(ctx, &container.Config{
|
||||||
Image: "hello-world",
|
// Image: "hello-world",
|
||||||
}, nil, nil, nil, "hello-world")
|
// }, nil, nil, nil, "hello-world")
|
||||||
|
|
||||||
if err != nil {
|
// if err != nil {
|
||||||
panic(err)
|
// panic(err)
|
||||||
}
|
// }
|
||||||
|
|
||||||
if err := cli.ContainerStart(ctx, resp.ID, container.StartOptions{}); err != nil {
|
// if err := cli.ContainerStart(ctx, resp.ID, container.StartOptions{}); err != nil {
|
||||||
panic(err)
|
// panic(err)
|
||||||
}
|
// }
|
||||||
|
|
||||||
// Attach to the container to get logs
|
// // Attach to the container to get logs
|
||||||
out, err := cli.ContainerLogs(ctx, resp.ID, container.LogsOptions{ShowStdout: true, ShowStderr: true, Follow: true})
|
// out, err := cli.ContainerLogs(ctx, resp.ID, container.LogsOptions{ShowStdout: true, ShowStderr: true, Follow: true})
|
||||||
if err != nil {
|
// if err != nil {
|
||||||
fmt.Printf("Error attaching to container logs: %s\n", err)
|
// fmt.Printf("Error attaching to container logs: %s\n", err)
|
||||||
}
|
// }
|
||||||
defer out.Close()
|
// defer out.Close()
|
||||||
|
|
||||||
// Copy the logs to stdout and stderr
|
// // Copy the logs to stdout and stderr
|
||||||
stdcopy.StdCopy(os.Stdout, os.Stderr, out)
|
// stdcopy.StdCopy(os.Stdout, os.Stderr, out)
|
||||||
|
|
||||||
// Wait for the container to finish
|
// // Wait for the container to finish
|
||||||
statusCh, errCh := cli.ContainerWait(ctx, resp.ID, container.WaitConditionNotRunning)
|
// statusCh, errCh := cli.ContainerWait(ctx, resp.ID, container.WaitConditionNotRunning)
|
||||||
select {
|
// select {
|
||||||
case err := <-errCh:
|
// case err := <-errCh:
|
||||||
if err != nil {
|
// if err != nil {
|
||||||
fmt.Printf("Error waiting for container: %s\n", err)
|
// fmt.Printf("Error waiting for container: %s\n", err)
|
||||||
}
|
// }
|
||||||
case <-statusCh:
|
// case <-statusCh:
|
||||||
fmt.Println("Container finished")
|
// fmt.Println("Container finished")
|
||||||
}
|
// }
|
||||||
// Clean up the container
|
// // Clean up the container
|
||||||
if err := cli.ContainerRemove(ctx, resp.ID, container.RemoveOptions{}); err != nil {
|
// if err := cli.ContainerRemove(ctx, resp.ID, container.RemoveOptions{}); err != nil {
|
||||||
fmt.Printf("Error removing container: %s\n", err)
|
// fmt.Printf("Error removing container: %s\n", err)
|
||||||
}
|
// }
|
||||||
// if gpu > 0 {
|
// if gpu > 0 {
|
||||||
// fmt.Print("Using gpu %s\n", gpu)
|
// fmt.Print("Using gpu %s\n", gpu)
|
||||||
// }
|
// }
|
||||||
|
|||||||
Reference in New Issue
Block a user