Skip to content

Commit

Permalink
Merge pull request #3 from f100024/config-operations
Browse files Browse the repository at this point in the history
Add parameters --config, --config-show, --config-show-names, --run-cu…
  • Loading branch information
f100024 authored Sep 18, 2024
2 parents dc30bc5 + eb18c80 commit a9233e7
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 9 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,4 @@ dist/
sshgut
*.zip
config.yaml
.vscode/
2 changes: 2 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
20 changes: 13 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ Example of the configuration file `config.yaml.example`

```
---
---
version: "2"
configs:
- name: server_name_1 # Server name id
Expand All @@ -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>]
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.
```
44 changes: 42 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"context"
_ "embed"
"fmt"
"slices"
"strings"
"time"

"os"
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit a9233e7

Please sign in to comment.