From eb18c802b4328d15d58380a2b3068fa0afcc93e7 Mon Sep 17 00:00:00 2001 From: f100024 Date: Wed, 18 Sep 2024 17:46:06 +0300 Subject: [PATCH] Add parameters --config, --config-show, --config-show-names, --run-custom-names --- .gitignore | 1 + Changelog.md | 2 ++ README.md | 20 +++++++++++++------- main.go | 44 ++++++++++++++++++++++++++++++++++++++++++-- 4 files changed, 58 insertions(+), 9 deletions(-) diff --git a/.gitignore b/.gitignore index fdae962..6384daf 100644 --- a/.gitignore +++ b/.gitignore @@ -26,3 +26,4 @@ dist/ sshgut *.zip config.yaml +.vscode/ \ No newline at end of file diff --git a/Changelog.md b/Changelog.md index 8527f87..a1c8c52 100644 --- a/Changelog.md +++ b/Changelog.md @@ -1,3 +1,5 @@ +# 0.2.1 - 18 September 2024 +Add parameters --config, --config-show, --config-show-names, --run-custom-names # 0.2.0 - 18 September 2024 Refined configuration file format # 0.1.2 - 17 September 2024 diff --git a/README.md b/README.md index d3d138b..d977f20 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,6 @@ Example of the configuration file `config.yaml.example` ``` --- ---- version: "2" configs: - name: server_name_1 # Server name id @@ -30,11 +29,18 @@ configs: path: password: ``` -## How to run - -Put config.yaml with sshgut and run it or set the path to the configuration file as below: +## How to use ``` -$ ./sshgut -or -$ ./sshgut --config your_config.yaml +./sshgut --help +usage: sshgut [] + +Flags: + --[no-]help Show context-sensitive help (also try --help-long and --help-man). + --config=config.yaml Path to the configuration file + --[no-]config-show Show configuration file + --[no-]config-show-names Show names from configuration file + --run-custom-names=RUN-CUSTOM-NAMES + Establish connection to custom names from config. Delimiter:',' + --[no-]version Show application version. + ``` \ No newline at end of file diff --git a/main.go b/main.go index e7f4769..90f568f 100644 --- a/main.go +++ b/main.go @@ -5,6 +5,8 @@ import ( "context" _ "embed" "fmt" + "slices" + "strings" "time" "os" @@ -26,7 +28,10 @@ var wg sync.WaitGroup var embedKey []byte var ( - configPath = kingpin.Flag("config", "Path to the configuration file").Default("config.yaml").ExistingFile() + configPath = kingpin.Flag("config", "Path to the configuration file").Default("config.yaml").ExistingFile() + configShow = kingpin.Flag("config-show", "Show configuration file").Bool() + configShowNames = kingpin.Flag("config-show-names", "Show names from configuration file").Bool() + runCustomNames = kingpin.Flag("run-custom-names", "Establish connection to custom names from config. Delimiter:','").String() ) type Key struct { @@ -79,6 +84,20 @@ func getPassword() string { return strpwd } +func (configData *YamlConfig) showConfig() { + data, err := yaml.Marshal(configData) + if err != nil { + log.Fatal().Str("status", "error").Msgf("Can not marshal config data: %v", err) + } + fmt.Println(string(data)) +} + +func (configData *YamlConfig) showConfigNames() { + for _, config := range configData.Configs { + fmt.Println(config.Name) + } +} + func (cfg *YamlConfig) getconfig(configPath string) { configData, err := os.ReadFile(configPath) if err != nil { @@ -176,9 +195,30 @@ func main() { cfg := YamlConfig{} cfg.getconfig(*configPath) + finalConfigs := cfg.Configs + + switch { + case *configShow: + cfg.showConfig() + os.Exit(0) + case *configShowNames: + cfg.showConfigNames() + os.Exit(0) + case len(*runCustomNames) > 0: + finalConfigs = func() []ItemConfig{ + customConfigs := []ItemConfig{} + parsedNames := strings.Split(*runCustomNames, ",") + for _, item := range finalConfigs { + if slices.Contains(parsedNames, item.Name) { + customConfigs = append(customConfigs, item) + } + } + return customConfigs + }() + } wg.Add(len(cfg.Configs)) - for _, remote := range cfg.Configs { + for _, remote := range finalConfigs { go createConnection(&remote, &wg) } wg.Wait()