mirror of
https://github.com/nestriness/cli.git
synced 2025-12-15 05:15:35 +02:00
Compare commits
2 Commits
93e22767c7
...
1ff9878359
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
1ff9878359 | ||
|
|
852478c784 |
@@ -1,20 +1,22 @@
|
|||||||
**************************************************
|
:*@@@@@*-
|
||||||
##################################################
|
+@@@@@@@@@@#=.
|
||||||
##################################################
|
:@@@@@@@@@@@@@@%+:
|
||||||
##################################################
|
.:--. .#@@@@:.=*@@@@@@@@@*-
|
||||||
##################################################
|
-%@@@@@@*-. .=#@. :+#@@@@@@@@#+.
|
||||||
::::::::::::::::::::::::::::::::::::::::::::::::::
|
*@@@@@@@@@@@%+: .=#@@@@@@@@%=
|
||||||
|
@@@@@@#@@@@@@@@@*-. -*@@@@@@@*
|
||||||
**************************************************
|
@@@@@* :+%@@@@@@@@%+: +@@@@@@
|
||||||
##################################################
|
@@@@@+ -#@@@@@@@@@*. :+@@@@@@@*
|
||||||
##################################################
|
@@@@@+ -+%@@@*=. .=*@@@@@@@@%=
|
||||||
##################################################
|
@@@@@+ :*=. :+%@@@@@@@@#=.
|
||||||
##################################################
|
@@@@@+ -@@@%- +@@@@@@@@@#-
|
||||||
::::::::::::::::::::::::::::::::::::::::::::::::::
|
@@@@@+ -@@@@@ @@@@@@%*- -*%%*.
|
||||||
|
@@@@@* -@@@@@: @@@#=. +@@@@@@@=
|
||||||
**************************************************
|
@@@@@* . -@@@@@: +: .+@@@@@@
|
||||||
##################################################
|
@@@@@#=*%* -@@@@@- :+@@@@@@%
|
||||||
##################################################
|
%@@@@@@@@+ -@@@@@- .-*@@@@@@@@#.
|
||||||
##################################################
|
.*@@@@@@@+ -@@@@@- .+%@@@@@@@@%+.
|
||||||
##################################################
|
.=+**+- :@@@@@= .-*%@@@@@@@@#=.
|
||||||
::::::::::::::::::::::::::::::::::::::::::::::::::
|
:@@@@@%%@@@@@@@@%+-
|
||||||
|
%@@@@@@@@@@@#=.
|
||||||
|
.+%@@@@@%*-
|
||||||
|
|||||||
45
cmd/root.go
45
cmd/root.go
@@ -10,7 +10,7 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"github.com/nestriness/cli/pkg/specs"
|
"github.com/netrisdotme/cli/pkg/specs"
|
||||||
|
|
||||||
"github.com/charmbracelet/lipgloss"
|
"github.com/charmbracelet/lipgloss"
|
||||||
"github.com/charmbracelet/lipgloss/table"
|
"github.com/charmbracelet/lipgloss/table"
|
||||||
@@ -22,6 +22,7 @@ import (
|
|||||||
var art string
|
var art string
|
||||||
|
|
||||||
// rootCmd represents the base command when called without any subcommands
|
// rootCmd represents the base command when called without any subcommands
|
||||||
|
// For a good reference point, start here: https://github.com/charmbracelet/taskcli/blob/main/cmds.go
|
||||||
var rootCmd = &cobra.Command{
|
var rootCmd = &cobra.Command{
|
||||||
Use: "nestri",
|
Use: "nestri",
|
||||||
Short: "A CLI tool to manage your cloud gaming service",
|
Short: "A CLI tool to manage your cloud gaming service",
|
||||||
@@ -31,6 +32,7 @@ var rootCmd = &cobra.Command{
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// this is for the "nestri neofetch" subcommand, has no arguments
|
||||||
var neoFetchCmd = &cobra.Command{
|
var neoFetchCmd = &cobra.Command{
|
||||||
Use: "neofetch",
|
Use: "neofetch",
|
||||||
Short: "Show important system information",
|
Short: "Show important system information",
|
||||||
@@ -47,7 +49,7 @@ var neoFetchCmd = &cobra.Command{
|
|||||||
var (
|
var (
|
||||||
b strings.Builder
|
b strings.Builder
|
||||||
lines = strings.Split(art, "\n")
|
lines = strings.Split(art, "\n")
|
||||||
colors = []string{"#CC3D00", "#CC3D00"}
|
colors = []string{"#F8481C", "#F74127", "#F53B30", "#F23538", "#F02E40"}
|
||||||
step = len(lines) / len(colors)
|
step = len(lines) / len(colors)
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -59,16 +61,16 @@ var neoFetchCmd = &cobra.Command{
|
|||||||
|
|
||||||
t := table.New().
|
t := table.New().
|
||||||
Border(lipgloss.HiddenBorder()).BorderStyle(lipgloss.NewStyle().Width(3))
|
Border(lipgloss.HiddenBorder()).BorderStyle(lipgloss.NewStyle().Width(3))
|
||||||
//TODO: show this specs
|
|
||||||
// info := &specs.Specs{}
|
|
||||||
// infoChan := make(chan specs.Specs, 1)
|
|
||||||
// var wg sync.WaitGroup
|
|
||||||
// wg.Add(1)
|
|
||||||
// go getSpecs(info, infoChan, &wg)
|
|
||||||
// wg.Wait()
|
|
||||||
// newInfo := <-infoChan
|
|
||||||
|
|
||||||
t.Row(b.String())
|
info := &specs.Specs{}
|
||||||
|
infoChan := make(chan specs.Specs, 1)
|
||||||
|
var wg sync.WaitGroup
|
||||||
|
wg.Add(1)
|
||||||
|
go getSpecs(info, infoChan, &wg)
|
||||||
|
wg.Wait()
|
||||||
|
newInfo := <-infoChan
|
||||||
|
|
||||||
|
t.Row(b.String(), newInfo.GPU)
|
||||||
|
|
||||||
fmt.Print(t)
|
fmt.Print(t)
|
||||||
|
|
||||||
@@ -76,6 +78,17 @@ var neoFetchCmd = &cobra.Command{
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// this is the "nestri run" subcommand, takes no arguments for now
|
||||||
|
var runCmd = &cobra.Command{
|
||||||
|
Use: "run",
|
||||||
|
Short: "Run a game using nestri",
|
||||||
|
Args: cobra.NoArgs,
|
||||||
|
//For now just show the "help"
|
||||||
|
RunE: func(cmd *cobra.Command, args []string) error {
|
||||||
|
return cmd.Help()
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
// Execute adds all child commands to the root command and sets flags appropriately.
|
// Execute adds all child commands to the root command and sets flags appropriately.
|
||||||
// This is called by main.main(). It only needs to happen once to the rootCmd.
|
// This is called by main.main(). It only needs to happen once to the rootCmd.
|
||||||
func Execute() {
|
func Execute() {
|
||||||
@@ -86,16 +99,12 @@ func Execute() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
// Here you will define your flags and configuration settings.
|
|
||||||
// Cobra supports persistent flags, which, if defined here,
|
|
||||||
// will be global for your application.
|
|
||||||
rootCmd.AddCommand(neoFetchCmd)
|
rootCmd.AddCommand(neoFetchCmd)
|
||||||
|
|
||||||
// rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is $HOME/.cli.yaml)")
|
rootCmd.AddCommand(runCmd)
|
||||||
|
|
||||||
// Cobra also supports local flags, which will only run
|
//If you want to add subcommands to run for example "netri run -fsr" do it like this
|
||||||
// when this action is called directly.
|
// runCmd.Flags().BoolP("fsr", "f", false, "Run the Game with FSR enabled or not")
|
||||||
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func colorize(c, s string) string {
|
func colorize(c, s string) string {
|
||||||
|
|||||||
2
go.mod
2
go.mod
@@ -1,4 +1,4 @@
|
|||||||
module github.com/nestriness/cli
|
module github.com/netrisdotme/cli
|
||||||
|
|
||||||
go 1.22.2
|
go 1.22.2
|
||||||
|
|
||||||
|
|||||||
3
main.go
3
main.go
@@ -1,9 +1,10 @@
|
|||||||
/*
|
/*
|
||||||
Copyright © 2024 NAME HERE <EMAIL ADDRESS>
|
Copyright © 2024 NAME HERE <EMAIL ADDRESS>
|
||||||
|
|
||||||
*/
|
*/
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import "github.com/nestriness/cli/cmd"
|
import "github.com/netrisdotme/cli/cmd"
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
cmd.Execute()
|
cmd.Execute()
|
||||||
|
|||||||
302
root.go
302
root.go
@@ -1,302 +0,0 @@
|
|||||||
/*
|
|
||||||
Copyright © 2024 Nestri <>
|
|
||||||
*/
|
|
||||||
package cmd
|
|
||||||
|
|
||||||
import (
|
|
||||||
_ "embed"
|
|
||||||
"fmt"
|
|
||||||
"os"
|
|
||||||
"os/exec"
|
|
||||||
"path/filepath"
|
|
||||||
"runtime"
|
|
||||||
"strings"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/charmbracelet/lipgloss"
|
|
||||||
"github.com/charmbracelet/lipgloss/table"
|
|
||||||
"github.com/muesli/termenv"
|
|
||||||
"github.com/spf13/cobra"
|
|
||||||
"github.com/spf13/viper"
|
|
||||||
)
|
|
||||||
|
|
||||||
//go:embed nestri.ascii
|
|
||||||
var art string
|
|
||||||
|
|
||||||
var (
|
|
||||||
gpu int
|
|
||||||
hdr bool
|
|
||||||
)
|
|
||||||
|
|
||||||
// rootCmd represents the base command when called without any subcommands
|
|
||||||
// For a good reference point, start here: https://github.com/charmbracelet/taskcli/blob/main/cmds.go
|
|
||||||
var rootCmd = &cobra.Command{
|
|
||||||
Use: "nestri",
|
|
||||||
Short: "A CLI tool to manage your cloud gaming service",
|
|
||||||
Args: cobra.NoArgs,
|
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
|
||||||
return cmd.Help()
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
// this is for the "nestri neofetch" subcommand, has no arguments
|
|
||||||
var neoFetchCmd = &cobra.Command{
|
|
||||||
Use: "neofetch",
|
|
||||||
Short: "Show important system information",
|
|
||||||
Args: cobra.NoArgs,
|
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
|
||||||
lipgloss.SetColorProfile(termenv.TrueColor)
|
|
||||||
|
|
||||||
baseStyle := lipgloss.NewStyle().
|
|
||||||
PaddingTop(1).
|
|
||||||
PaddingRight(4).
|
|
||||||
PaddingBottom(1).
|
|
||||||
PaddingLeft(4)
|
|
||||||
|
|
||||||
var (
|
|
||||||
b strings.Builder
|
|
||||||
lines = strings.Split(art, "\n")
|
|
||||||
colors = []string{"#F8481C", "#F74127", "#F53B30", "#F23538", "#F02E40"}
|
|
||||||
step = len(lines) / len(colors)
|
|
||||||
)
|
|
||||||
|
|
||||||
for i, l := range lines {
|
|
||||||
n := clamp(0, len(colors)-1, i/step)
|
|
||||||
b.WriteString(colorize(colors[n], l))
|
|
||||||
b.WriteRune('\n')
|
|
||||||
}
|
|
||||||
|
|
||||||
t := table.New().
|
|
||||||
Border(lipgloss.HiddenBorder())
|
|
||||||
|
|
||||||
t.Row(baseStyle.Render(b.String()), baseStyle.Render("System Info goes here"))
|
|
||||||
|
|
||||||
fmt.Print(t)
|
|
||||||
|
|
||||||
return nil
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
// this is the "nestri run" subcommand, takes no arguments for now
|
|
||||||
var runCmd = &cobra.Command{
|
|
||||||
Use: "run",
|
|
||||||
Short: "Run a game using nestri",
|
|
||||||
Args: cobra.MaximumNArgs(1),
|
|
||||||
RunE: func(cmd *cobra.Command, args []string) error {
|
|
||||||
if runtime.GOOS != "linux" {
|
|
||||||
//make sure os is linux
|
|
||||||
fmt.Println("This command is only supported on Linux.")
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
var game string
|
|
||||||
if len(args) > 0 {
|
|
||||||
game = args[0]
|
|
||||||
viper.Set("game", game)
|
|
||||||
viper.WriteConfig()
|
|
||||||
} else {
|
|
||||||
game = viper.GetString("game")
|
|
||||||
if filepath.Ext(game) != ".exe" {
|
|
||||||
return fmt.Errorf("Make sure the game is a .exe")
|
|
||||||
}
|
|
||||||
if game == "" {
|
|
||||||
return fmt.Errorf("no game specified and no previous game selected")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fmt.Printf("Running game: %s\n\n", game)
|
|
||||||
if gpu > 0 {
|
|
||||||
fmt.Print("Using gpu %s\n", gpu)
|
|
||||||
}
|
|
||||||
if hdr {
|
|
||||||
fmt.Println("Enabling HDR mode")
|
|
||||||
}
|
|
||||||
|
|
||||||
//get linux version
|
|
||||||
versionCmd := exec.Command("grep", "VERSION", "/etc/os-release")
|
|
||||||
versionOutput, err := versionCmd.CombinedOutput()
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("error getting linux version:")
|
|
||||||
}
|
|
||||||
fmt.Printf("Linux version:\n%s\n", string(versionOutput))
|
|
||||||
|
|
||||||
//Step 1: change to games dir
|
|
||||||
fmt.Println("changing to game dir.") //this is a temp command for debug as well as leads to a hardcoded dir
|
|
||||||
|
|
||||||
HomeDir, err := os.UserHomeDir()
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("error getting home directory %v\n", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
err = os.Chdir(fmt.Sprintf("%s/game", HomeDir))
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("error changing directory: %v\n", err)
|
|
||||||
}
|
|
||||||
//verify we are in game dir
|
|
||||||
dir, err := os.Getwd()
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("error getting current directory: %v\n", err)
|
|
||||||
}
|
|
||||||
fmt.Printf("Current directory: %s\n\n", dir)
|
|
||||||
|
|
||||||
//list games dir
|
|
||||||
listDir := exec.Command("ls", "-la", ".")
|
|
||||||
listDirOutput, err := listDir.CombinedOutput()
|
|
||||||
if err != nil {
|
|
||||||
fmt.Errorf("error listing games: %v\n")
|
|
||||||
}
|
|
||||||
fmt.Printf("List of Games: \n%s\n", listDirOutput)
|
|
||||||
|
|
||||||
//step 2: Generate a Session ID
|
|
||||||
//generate id
|
|
||||||
SID := exec.Command("bash", "-c", "head /dev/urandom | LC_ALL=C tr -dc 'a-zA-Z0-9' | head -c 16")
|
|
||||||
|
|
||||||
//save output to variable
|
|
||||||
output, err := SID.Output()
|
|
||||||
if err != nil {
|
|
||||||
fmt.Errorf("Error generating Session ID: %v\n", err)
|
|
||||||
}
|
|
||||||
sessionID := strings.TrimSpace(string(output))
|
|
||||||
fmt.Printf("Your Session ID is: %s\n\n", sessionID)
|
|
||||||
|
|
||||||
//step 3: Launch netris server
|
|
||||||
fmt.Println("Installing Netris/Launching Netris Server\n")
|
|
||||||
checkRunning := exec.Command("sudo", "docker", "ps", "-q", "-f", "name=netris")
|
|
||||||
containerId, err := checkRunning.Output()
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("error checking running Docker container: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(containerId) == 0 {
|
|
||||||
checkExisting := exec.Command("sudo", "docker", "ps", "-aq", "-f", "name=netris")
|
|
||||||
containerId, err = checkExisting.Output()
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("error checking for existing docker container: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(containerId) == 0 {
|
|
||||||
installCmd := exec.Command(
|
|
||||||
"sudo", "docker", "run", "-d", "--gpus", "all", "--device=/dev/dri",
|
|
||||||
"--name", "netris", "-it", "--entrypoint", "/bin/bash",
|
|
||||||
"-e", fmt.Sprintf("SESSION_ID=%s", sessionID),
|
|
||||||
"-v", fmt.Sprintf("%s:/game", dir), "-p", "8080:8080/udp",
|
|
||||||
"--cap-add=SYS_NICE", "--cap-add=SYS_ADMIN", "ghcr.io/netrisdotme/netris/server:nightly",
|
|
||||||
)
|
|
||||||
installCmd.Stdout = os.Stdout
|
|
||||||
installCmd.Stderr = os.Stderr
|
|
||||||
|
|
||||||
if err := installCmd.Run(); err != nil {
|
|
||||||
return fmt.Errorf("error running docker command: %v", err)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
startContainer := exec.Command("sudo", "docker", "start", "netris")
|
|
||||||
startContainer.Stdout = os.Stdout
|
|
||||||
startContainer.Stderr = os.Stderr
|
|
||||||
|
|
||||||
if err := startContainer.Run(); err != nil {
|
|
||||||
return fmt.Errorf("error starting existing Docker container: %v", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//main part of step 4:
|
|
||||||
//start netris server
|
|
||||||
|
|
||||||
fmt.Println("starting netris server\n\n")
|
|
||||||
checkFileCmd := exec.Command("sudo", "docker", "exec", "netris", "ls", "-la", "/tmp")
|
|
||||||
output, err = checkFileCmd.Output()
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("error checking /tmp dir in docker container: %v\n", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if !strings.Contains(string(output), ".X11-unix") {
|
|
||||||
startupCmd := exec.Command("sudo", "docker", "exec", "netris", "/etc/startup.sh", ">", "/dev/null", "&")
|
|
||||||
startupCmd.Stdout = os.Stdout
|
|
||||||
startupCmd.Stderr = os.Stderr
|
|
||||||
|
|
||||||
if err := startupCmd.Run(); err != nil {
|
|
||||||
return fmt.Errorf("error running startup command: %v\n", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
for {
|
|
||||||
time.Sleep(7 * time.Minute)
|
|
||||||
output, err := checkFileCmd.Output()
|
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("error checking /tmp directory in container: %v\n", err)
|
|
||||||
}
|
|
||||||
if strings.Contains(string(output), ".X11-unix") {
|
|
||||||
break
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
gameCmd := fmt.Sprintf("netris-proton -pr %s", game)
|
|
||||||
execCmd := exec.Command("sudo", "docker", "exec", "netris", gameCmd)
|
|
||||||
execCmd.Stdout = os.Stdout
|
|
||||||
execCmd.Stderr = os.Stderr
|
|
||||||
|
|
||||||
if err := execCmd.Run(); err != nil {
|
|
||||||
return fmt.Errorf("error executing game command in docker container: %v\n", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
// Execute adds all child commands to the root command and sets flags appropriately.
|
|
||||||
// This is called by main.main(). It only needs to happen once to the rootCmd.
|
|
||||||
func Execute() {
|
|
||||||
err := rootCmd.Execute()
|
|
||||||
if err != nil {
|
|
||||||
os.Exit(1)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func init() {
|
|
||||||
rootCmd.AddCommand(neoFetchCmd)
|
|
||||||
|
|
||||||
rootCmd.AddCommand(runCmd)
|
|
||||||
|
|
||||||
runCmd.Flags().IntVar(&gpu, "gpu", 0, "Specify GPU index")
|
|
||||||
runCmd.Flags().BoolVar(&hdr, "hdr", false, "Enable HDR mode")
|
|
||||||
|
|
||||||
viper.SetConfigName("config")
|
|
||||||
viper.SetConfigType("yaml")
|
|
||||||
viper.AddConfigPath(".")
|
|
||||||
|
|
||||||
if err := viper.ReadInConfig(); err != nil {
|
|
||||||
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
|
|
||||||
|
|
||||||
} else {
|
|
||||||
fmt.Println("error reading config file: %v(you should be able to ignore this)", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//If you want to add subcommands to run for example "netri run -fsr" do it like this
|
|
||||||
// runCmd.Flags().BoolP("fsr", "f", false, "Run the Game with FSR enabled or not")
|
|
||||||
}
|
|
||||||
|
|
||||||
func colorize(c, s string) string {
|
|
||||||
return lipgloss.NewStyle().Foreground(lipgloss.Color(c)).Render(s)
|
|
||||||
}
|
|
||||||
|
|
||||||
func clamp(v, low, high int) int {
|
|
||||||
if high < low {
|
|
||||||
low, high = high, low
|
|
||||||
}
|
|
||||||
return min(high, max(low, v))
|
|
||||||
}
|
|
||||||
|
|
||||||
func min(a, b int) int {
|
|
||||||
if a < b {
|
|
||||||
return a
|
|
||||||
}
|
|
||||||
return b
|
|
||||||
}
|
|
||||||
|
|
||||||
func max(a, b int) int {
|
|
||||||
if a > b {
|
|
||||||
return a
|
|
||||||
}
|
|
||||||
return b
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user