dev/command-saving #1

Merged
jake merged 3 commits from dev/command-saving into main 2025-04-17 15:40:55 +00:00
7 changed files with 222 additions and 20 deletions
Showing only changes of commit bef3521770 - Show all commits

2
.gitignore vendored
View File

@ -24,4 +24,4 @@ go.work.sum
# env file # env file
.env .env
todo.txt

View File

@ -4,13 +4,12 @@ Copyright © 2025 Jake jake.young.dev@gmail.com
package cmd package cmd
import ( import (
"crypto/aes"
"crypto/cipher"
"crypto/rand" "crypto/rand"
"fmt" "fmt"
"io" "io"
"os" "os"
"code.jakeyoungdev.com/jake/mctl/cryptography"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/viper" "github.com/spf13/viper"
"golang.org/x/term" "golang.org/x/term"
@ -35,23 +34,25 @@ var configCmd = &cobra.Command{
cobra.CheckErr(err) cobra.CheckErr(err)
//setup aes encrypter //setup aes encrypter
block, err := aes.NewCipher([]byte(viper.Get("device").(string))) // block, err := aes.NewCipher([]byte(viper.Get("device").(string)))
cobra.CheckErr(err) // cobra.CheckErr(err)
//generate and apply random nonce //generate and apply random nonce
nonce := make([]byte, 12) nonce := make([]byte, 12)
_, err = io.ReadFull(rand.Reader, nonce) _, err = io.ReadFull(rand.Reader, nonce)
cobra.CheckErr(err) cobra.CheckErr(err)
aesg, err := cipher.NewGCM(block) viper.Set("nonce", string(nonce))
cobra.CheckErr(err) // aesg, err := cipher.NewGCM(block)
// cobra.CheckErr(err)
//encrypt rcon password // //encrypt rcon password
ciphert := aesg.Seal(nil, nonce, ps, nil) // ciphert := aesg.Seal(nil, nonce, ps, nil)
ciphert, err := cryptography.EncryptPassword(ps)
cobra.CheckErr(err)
//update config file with new values //update config file with new values
viper.Set("server", server) viper.Set("server", server)
viper.Set("password", string(ciphert)) viper.Set("password", string(ciphert))
viper.Set("port", port) viper.Set("port", port)
viper.Set("nonce", string(nonce))
viper.WriteConfig() viper.WriteConfig()
fmt.Println() fmt.Println()
fmt.Println("Config file updated!") fmt.Println("Config file updated!")

View File

@ -5,11 +5,10 @@ package cmd
import ( import (
"bufio" "bufio"
"crypto/aes"
"crypto/cipher"
"fmt" "fmt"
"os" "os"
"code.jakeyoungdev.com/jake/mctl/cryptography"
"github.com/jake-young-dev/mcr" "github.com/jake-young-dev/mcr"
"github.com/spf13/cobra" "github.com/spf13/cobra"
"github.com/spf13/viper" "github.com/spf13/viper"
@ -29,16 +28,18 @@ var loginCmd = &cobra.Command{
fmt.Printf("Logging into %s on port %d\n", server, port) fmt.Printf("Logging into %s on port %d\n", server, port)
//setup decrypter //setup decrypter
nonce := viper.Get("nonce") // nonce := viper.Get("nonce")
block, err := aes.NewCipher([]byte(viper.Get("device").(string))) // block, err := aes.NewCipher([]byte(viper.Get("device").(string)))
cobra.CheckErr(err) // cobra.CheckErr(err)
aesg, err := cipher.NewGCM(block) // aesg, err := cipher.NewGCM(block)
cobra.CheckErr(err) // cobra.CheckErr(err)
//decrypt password // //decrypt password
pwd := []byte(password.(string)) pwd := []byte(password.(string))
nn := []byte(nonce.(string)) // nn := []byte(nonce.(string))
pt, err := aesg.Open(nil, nn, pwd, nil) // pt, err := aesg.Open(nil, nn, pwd, nil)
// cobra.CheckErr(err)
pt, err := cryptography.DecryptPassword(pwd)
cobra.CheckErr(err) cobra.CheckErr(err)
//connect to game server //connect to game server

50
cmd/run.go Normal file
View File

@ -0,0 +1,50 @@
/*
Copyright © 2025 NAME HERE <EMAIL ADDRESS>
*/
package cmd
import (
"errors"
"github.com/spf13/cobra"
)
// runCmd represents the run command
var runCmd = &cobra.Command{
Use: "run",
Short: "A brief description of your command",
Long: `A longer description that spans multiple lines and likely contains examples
and usage of using your command. For example:
Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
Run: func(cmd *cobra.Command, args []string) {
// fmt.Println(viper.Get(runname))
// server := viper.Get("server")
// r := viper.Get(args[0])
// cli, err := mcr.NewClient()
},
PreRunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
return errors.New("name argument is required")
}
return nil
},
}
func init() {
rootCmd.AddCommand(runCmd)
// runCmd.Flags().StringVar(&runname, "name", "", "")
// runCmd.MarkFlagRequired("name")
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// runCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// runCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}

67
cmd/save.go Normal file
View File

@ -0,0 +1,67 @@
/*
Copyright © 2025 NAME HERE <EMAIL ADDRESS>
*/
package cmd
import (
"bufio"
"fmt"
"os"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var (
name string
// cmd string
)
// saveCmd represents the save command
var saveCmd = &cobra.Command{
Use: "save",
Short: "A brief description of your command",
Long: `A longer description that spans multiple lines and likely contains examples
and usage of using your command. For example:
Cobra is a CLI library for Go that empowers applications.
This application is a tool to generate the needed files
to quickly create a Cobra application.`,
Run: func(cmd *cobra.Command, args []string) {
// fmt.Println("save called")
// viper.Set("testcmd", "im doin stuff and testing a command idea ' idk")
// viper.WriteConfig()
// fmt.Println(viper.Get("testcmd"))
// fmt.Println(name)
// fmt.Println(cmd)
fmt.Printf("Command: ")
sc := bufio.NewScanner(os.Stdin)
if sc.Scan() {
txt := sc.Text()
if txt != "" {
viper.Set(name, txt)
viper.WriteConfig()
fmt.Println("\nSaved!")
return
}
}
},
}
func init() {
rootCmd.AddCommand(saveCmd)
// Here you will define your flags and configuration settings.
// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// saveCmd.PersistentFlags().String("foo", "", "A help for foo")
// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// saveCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
saveCmd.Flags().StringVar(&name, "name", "", "")
saveCmd.MarkFlagRequired("name")
// saveCmd.Flags().StringVar(&cmd, "command", "", "")
// saveCmd.MarkFlagRequired("command")
}

34
cmd/view.go Normal file
View File

@ -0,0 +1,34 @@
/*
Copyright © 2025 NAME HERE <EMAIL ADDRESS>
*/
package cmd
import (
"errors"
"fmt"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
// viewCmd represents the view command
var viewCmd = &cobra.Command{
Use: "view <name>",
Example: "mctl view test",
Short: "View saved commands",
Long: `Load command using the supplied name and displays it in the terminal`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Printf("\nCommand: %s\n", viper.Get(args[0]))
},
PreRunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
return errors.New("name argument is required")
}
return nil
},
}
func init() {
rootCmd.AddCommand(viewCmd)
}

49
cryptography/aes.go Normal file
View File

@ -0,0 +1,49 @@
package cryptography
import (
"crypto/aes"
"crypto/cipher"
"github.com/spf13/viper"
)
func EncryptPassword(b []byte) ([]byte, error) {
nonce := viper.Get("nonce").(string)
dev := viper.Get("device").(string)
block, err := aes.NewCipher([]byte(dev))
if err != nil {
return nil, err
}
aesg, err := cipher.NewGCM(block)
if err != nil {
return nil, err
}
ct := aesg.Seal(nil, []byte(nonce), []byte(b), nil)
return ct, nil
}
func DecryptPassword(b []byte) (string, error) {
nonce := viper.Get("nonce").(string)
password := viper.Get("password").(string)
dev := viper.Get("device").(string)
block, err := aes.NewCipher([]byte(dev))
if err != nil {
return "", err
}
aesg, err := cipher.NewGCM(block)
if err != nil {
return "", err
}
op, err := aesg.Open(nil, []byte(nonce), []byte(password), nil)
if err != nil {
return "", err
}
return string(op), nil
}