lots o progress
- added 'command' subcommand - removed viper - setup commands added - still a WIP - readme TODO update
This commit is contained in:
70
cmd/server/add.go
Normal file
70
cmd/server/add.go
Normal file
@@ -0,0 +1,70 @@
|
||||
/*
|
||||
Copyright © 2025 Jake jake.young.dev@gmail.com
|
||||
*/
|
||||
package server
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
"os"
|
||||
"strconv"
|
||||
|
||||
"code.jakeyoungdev.com/jake/mctl/database"
|
||||
"code.jakeyoungdev.com/jake/mctl/model"
|
||||
"github.com/spf13/cobra"
|
||||
"golang.org/x/term"
|
||||
)
|
||||
|
||||
var addCmd = &cobra.Command{
|
||||
Use: "add",
|
||||
Example: "mctl server add",
|
||||
Short: "Saves a new server configuration",
|
||||
Long: `Saves server address, alias, port, and password.`,
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
scanner := bufio.NewScanner(os.Stdin)
|
||||
|
||||
//get server information
|
||||
var cfgname string
|
||||
fmt.Printf("Server alias: ")
|
||||
if scanner.Scan() {
|
||||
cfgname = scanner.Text()
|
||||
}
|
||||
|
||||
var cfgaddress string
|
||||
fmt.Printf("Server address: ")
|
||||
if scanner.Scan() {
|
||||
cfgaddress = scanner.Text()
|
||||
}
|
||||
|
||||
var cfgport string
|
||||
fmt.Printf("Server port: ")
|
||||
if scanner.Scan() {
|
||||
cfgport = scanner.Text()
|
||||
}
|
||||
|
||||
//read in password using term to keep it secure/hidden from bash history
|
||||
fmt.Printf("Password: ")
|
||||
ps, err := term.ReadPassword(int(os.Stdin.Fd()))
|
||||
cobra.CheckErr(err)
|
||||
|
||||
db, err := database.New()
|
||||
cobra.CheckErr(err)
|
||||
defer db.Close()
|
||||
|
||||
fp, err := strconv.Atoi(cfgport)
|
||||
cobra.CheckErr(err)
|
||||
|
||||
err = db.SaveServer(model.Server{
|
||||
Name: cfgname,
|
||||
Server: cfgaddress,
|
||||
Port: fp,
|
||||
Password: base64.StdEncoding.EncodeToString(ps),
|
||||
})
|
||||
cobra.CheckErr(err)
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
ServerCmd.AddCommand(addCmd)
|
||||
}
|
36
cmd/server/delete.go
Normal file
36
cmd/server/delete.go
Normal file
@@ -0,0 +1,36 @@
|
||||
/*
|
||||
Copyright © 2025 Jake jake.young.dev@gmail.com
|
||||
*/
|
||||
package server
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"code.jakeyoungdev.com/jake/mctl/database"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var deleteCmd = &cobra.Command{
|
||||
Use: "delete",
|
||||
Example: "mctl server delete <server>",
|
||||
// Short: "Deletes a server",
|
||||
// Long: `Deletes server configuration`,
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
db, err := database.New()
|
||||
cobra.CheckErr(err)
|
||||
defer db.Close()
|
||||
|
||||
err = db.DeleteServer(args[0])
|
||||
cobra.CheckErr(err)
|
||||
},
|
||||
PreRunE: func(cmd *cobra.Command, args []string) error {
|
||||
if len(args) == 0 {
|
||||
return errors.New("server parameter missing")
|
||||
}
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
ServerCmd.AddCommand(deleteCmd)
|
||||
}
|
21
cmd/server/server.go
Normal file
21
cmd/server/server.go
Normal file
@@ -0,0 +1,21 @@
|
||||
/*
|
||||
Copyright © 2025 Jake jake.young.dev@gmail.com
|
||||
*/
|
||||
package server
|
||||
|
||||
import (
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var ServerCmd = &cobra.Command{
|
||||
Use: "server",
|
||||
Example: "mctl server <subcommand>",
|
||||
// Short: "A remote console client",
|
||||
// Long: `mctl is a terminal-friendly remote console client made to manage game servers.`,
|
||||
// Version: "v0.3.4",
|
||||
// Run: func(cmd *cobra.Command, args []string) { },
|
||||
}
|
||||
|
||||
func init() {
|
||||
// rootCmd.AddCommand()
|
||||
}
|
48
cmd/server/update.go
Normal file
48
cmd/server/update.go
Normal file
@@ -0,0 +1,48 @@
|
||||
/*
|
||||
Copyright © 2025 Jake jake.young.dev@gmail.com
|
||||
*/
|
||||
package server
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"code.jakeyoungdev.com/jake/mctl/database"
|
||||
"github.com/spf13/cobra"
|
||||
"golang.org/x/term"
|
||||
)
|
||||
|
||||
var updateCmd = &cobra.Command{
|
||||
Use: "update",
|
||||
Example: "mctl server update <name>",
|
||||
// Short: "Saves a new server configuration",
|
||||
// Long: `Saves server address, alias, port, and password.`,
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
//read in password using term to keep it secure/hidden from bash history
|
||||
fmt.Printf("Password: ")
|
||||
ps, err := term.ReadPassword(int(os.Stdin.Fd()))
|
||||
cobra.CheckErr(err)
|
||||
|
||||
db, err := database.New()
|
||||
cobra.CheckErr(err)
|
||||
defer db.Close()
|
||||
|
||||
err = db.UpdateServer(args[0], base64.StdEncoding.EncodeToString(ps))
|
||||
cobra.CheckErr(err)
|
||||
|
||||
fmt.Printf("%s password updated!", args[0])
|
||||
},
|
||||
PreRunE: func(cmd *cobra.Command, args []string) error {
|
||||
if len(args) == 0 {
|
||||
return errors.New("name command required")
|
||||
}
|
||||
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
ServerCmd.AddCommand(updateCmd)
|
||||
}
|
37
cmd/server/view.go
Normal file
37
cmd/server/view.go
Normal file
@@ -0,0 +1,37 @@
|
||||
/*
|
||||
Copyright © 2025 Jake jake.young.dev@gmail.com
|
||||
*/
|
||||
package server
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"code.jakeyoungdev.com/jake/mctl/database"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var viewCmd = &cobra.Command{
|
||||
Use: "view",
|
||||
Example: "mctl server view",
|
||||
Short: "view all servers",
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
db, err := database.New()
|
||||
cobra.CheckErr(err)
|
||||
defer db.Close()
|
||||
|
||||
ts, err := db.GetAllServers()
|
||||
cobra.CheckErr(err)
|
||||
|
||||
for _, s := range ts {
|
||||
fmt.Println("-----")
|
||||
fmt.Printf("Name: %s\n", s.Name)
|
||||
fmt.Printf("Address: %s\n", s.Server)
|
||||
fmt.Printf("Port: %d\n", s.Port)
|
||||
fmt.Printf("Password: %s\n", s.Password)
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
ServerCmd.AddCommand(viewCmd)
|
||||
}
|
Reference in New Issue
Block a user