From 83fec73076ecbb35392f7d67a7b34a9da2e4292a Mon Sep 17 00:00:00 2001 From: jake Date: Sat, 10 May 2025 15:22:33 -0400 Subject: [PATCH 1/5] added clear command - clear command setup - clear command added to docs - config fields saved in model layer - config command code cleanup --- README.md | 17 ++++++++++++++++- cmd/clear.go | 44 ++++++++++++++++++++++++++++++++++++++++++++ cmd/config.go | 3 +-- models/data.go | 4 ++++ 4 files changed, 65 insertions(+), 3 deletions(-) create mode 100644 cmd/clear.go create mode 100644 models/data.go diff --git a/README.md b/README.md index 992bf66..c2b3812 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,13 @@ # mctl mctl is a terminal-friendly remote console client +## Index +1. [Installation](#installation) +2. [Setup](#setup) +3. [Documentation](#documentation) +4. [Security](#security) +5. [Development](#development) + ## Installation Install mctl using golang ```bash @@ -75,6 +82,13 @@ Commands can be deleted with: mctl delete ``` +### Clear configuration file +To clear all fields from the configuration file use: +```bash +#CAUTION: If the config file is cleared all data previously saved will be lost forever +mctl clear +``` + ## Documentation ### Commands |Command|Description| @@ -85,6 +99,7 @@ mctl delete |view \|displays saved command| |delete \|deletes saved command| |run \ args...|runs saved command filling placeholders with supplied args| +|clear|clears config file| ### Flags #### config @@ -102,4 +117,4 @@ RCon is an inherently insecure protocol, passwords are sent in plaintext and, if mctl utilizes [govulncheck](https://pkg.go.dev/golang.org/x/vuln/cmd/govulncheck) and [gosec](https://github.com/securego/gosec) in workflows to ensure quality, secure code is being pushed. These workflow steps must pass before a PR will be accepted ## Development -this repo is currently in heavy development and may encounter breaking changes, use a tag to prevent any surprises \ No newline at end of file +this repo is currently in development and may encounter breaking changes, use a tag to prevent any surprises \ No newline at end of file diff --git a/cmd/clear.go b/cmd/clear.go new file mode 100644 index 0000000..b5125f9 --- /dev/null +++ b/cmd/clear.go @@ -0,0 +1,44 @@ +/* +Copyright © 2025 Jake jake.young.dev@gmail.com +*/ +package cmd + +import ( + "fmt" + "os" + + "code.jakeyoungdev.com/jake/mctl/models" + "github.com/spf13/cobra" + "github.com/spf13/viper" +) + +// clearCmd represents the clear command +var clearCmd = &cobra.Command{ + Use: "clear", + Short: "Clear config file", + Long: `Removes the configuration file from the system. The + config command must be run before use after clearing.`, + Run: func(cmd *cobra.Command, args []string) { + home, err := os.UserHomeDir() + cobra.CheckErr(err) + + viper.AddConfigPath(home) + viper.SetConfigType("yaml") + viper.SetConfigName(".mctl") + + err = viper.ReadInConfig() + if err == nil { + //clear values if file exists + for _, v := range models.ConfigFields { + viper.Set(v, "") + } + err := viper.WriteConfig() + cobra.CheckErr(err) + fmt.Println("Config file cleared, use 'config' command to re-populate it") + } + }, +} + +func init() { + rootCmd.AddCommand(clearCmd) +} diff --git a/cmd/config.go b/cmd/config.go index d5dd723..25f500c 100644 --- a/cmd/config.go +++ b/cmd/config.go @@ -76,9 +76,8 @@ func initConfig() { viper.SetConfigName(".mctl") viper.AutomaticEnv() err = viper.ReadInConfig() - cobra.CheckErr(err) - if err := viper.ReadInConfig(); err != nil { + if err != nil { //file does not exist, create it viper.Set("server", cfgserver) viper.Set("password", "") diff --git a/models/data.go b/models/data.go new file mode 100644 index 0000000..e282c91 --- /dev/null +++ b/models/data.go @@ -0,0 +1,4 @@ +package models + +//list of all fields kept in config file +var ConfigFields = [6]string{"customcmd", "device", "nonce", "port", "server", "password"} -- 2.47.2 From d370d4f33cba183b484928cf507083da72cb873a Mon Sep 17 00:00:00 2001 From: jake Date: Sun, 11 May 2025 00:17:00 -0400 Subject: [PATCH 2/5] readme update --- README.md | 1 - 1 file changed, 1 deletion(-) diff --git a/README.md b/README.md index c2b3812..4899fae 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,6 @@ Install mctl using golang ```bash go install code.jakeyoungdev.com/jake/mctl@main #it is recommended to use a tagged version ``` -
## Setup ### Configuring mctl -- 2.47.2 From 57fc5e0807694eaa3a1fc10a93780a57db39738c Mon Sep 17 00:00:00 2001 From: jake Date: Sun, 11 May 2025 00:22:07 -0400 Subject: [PATCH 3/5] long description update --- cmd/clear.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/cmd/clear.go b/cmd/clear.go index b5125f9..9f160d0 100644 --- a/cmd/clear.go +++ b/cmd/clear.go @@ -16,8 +16,7 @@ import ( var clearCmd = &cobra.Command{ Use: "clear", Short: "Clear config file", - Long: `Removes the configuration file from the system. The - config command must be run before use after clearing.`, + Long: `Clears all configuration values for mctl`, Run: func(cmd *cobra.Command, args []string) { home, err := os.UserHomeDir() cobra.CheckErr(err) -- 2.47.2 From 57e8f3a775e689f4211bba79f3ad6f182d129ba2 Mon Sep 17 00:00:00 2001 From: jake Date: Sun, 11 May 2025 00:25:13 -0400 Subject: [PATCH 4/5] long description update --- cmd/clear.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cmd/clear.go b/cmd/clear.go index 9f160d0..5dd05de 100644 --- a/cmd/clear.go +++ b/cmd/clear.go @@ -16,7 +16,7 @@ import ( var clearCmd = &cobra.Command{ Use: "clear", Short: "Clear config file", - Long: `Clears all configuration values for mctl`, + Long: `Clears all configuration values for mctl, all server configuration will be lost`, Run: func(cmd *cobra.Command, args []string) { home, err := os.UserHomeDir() cobra.CheckErr(err) -- 2.47.2 From f1ab84d5492a3589c36022ae763981c0cbeed45d Mon Sep 17 00:00:00 2001 From: jake Date: Sun, 11 May 2025 00:26:35 -0400 Subject: [PATCH 5/5] workflow trigger update --- .gitea/workflows/security.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitea/workflows/security.yaml b/.gitea/workflows/security.yaml index 2f5dc31..bba8029 100644 --- a/.gitea/workflows/security.yaml +++ b/.gitea/workflows/security.yaml @@ -1,5 +1,5 @@ name: "code scans" -on: [push, pull_request] #runs on pushes to any branch +on: pull_request jobs: scans: -- 2.47.2