Skip to content

Commit

Permalink
code changes (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
therealpaulgg authored Mar 20, 2023
1 parent f8d1236 commit 6fac24e
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 20 deletions.
7 changes: 4 additions & 3 deletions pkg/actions/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ func Download(c *cli.Context) error {
}
if err := utils.WriteConfig(lo.Map(data.SshConfig, func(config dto.SshConfigDto, i int) models.Host {
return models.Host{
Host: config.Host,
Values: config.Values,
IdentityFile: config.IdentityFile,
Host: config.Host,
Values: config.Values,
IdentityFiles: config.IdentityFiles,
}
})); err != nil {
return err
Expand All @@ -76,5 +76,6 @@ func Download(c *cli.Context) error {
return err
}
}
fmt.Println("Successfully downloaded keys.")
return nil
}
6 changes: 3 additions & 3 deletions pkg/dto/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ type KeyDto struct {
}

type SshConfigDto struct {
Host string `json:"host"`
Values map[string]string `json:"values"`
IdentityFile string `json:"identity_file"`
Host string `json:"host"`
Values map[string][]string `json:"values"`
IdentityFiles []string `json:"identity_files"`
}

type MachineDto struct {
Expand Down
6 changes: 3 additions & 3 deletions pkg/models/host.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package models

type Host struct {
Host string `json:"host"`
IdentityFile string `json:"identity_file"`
Values map[string]string `json:"values"`
Host string `json:"host"`
IdentityFiles []string `json:"identity_files"`
Values map[string][]string `json:"values"`
}
8 changes: 4 additions & 4 deletions pkg/utils/parseconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,22 +36,22 @@ func ParseConfig() ([]models.Host, error) {
}
currentHost = &models.Host{
Host: strings.TrimPrefix(line, "Host "),
Values: make(map[string]string),
Values: make(map[string][]string),
}
} else if re.Match([]byte(line)) {
key := re.FindStringSubmatch(line)[1]
value := re.FindStringSubmatch(line)[2]
if key == "IdentityFile" {
if strings.ToLower(key) == "identityfile" {
homeDir := user.HomeDir
if runtime.GOOS == "windows" {
value = strings.ToLower(value)
homeDir = strings.ToLower(user.HomeDir)
}
identityFile := strings.TrimPrefix(value, homeDir)
normalizedIdentityFilePath := filepath.ToSlash(identityFile)
currentHost.IdentityFile = normalizedIdentityFilePath
currentHost.IdentityFiles = append(currentHost.IdentityFiles, normalizedIdentityFilePath)
} else {
currentHost.Values[key] = value
currentHost.Values[key] = append(currentHost.Values[key], value)
}

}
Expand Down
14 changes: 9 additions & 5 deletions pkg/utils/write.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,18 @@ func WriteConfig(hosts []models.Host) error {
if _, err := file.WriteString(fmt.Sprintf("Host %s\n", host.Host)); err != nil {
return err
}
if host.IdentityFile != "" {
if _, err := file.WriteString(fmt.Sprintf("\t%s %s\n", "IdentityFile", filepath.Join(user.HomeDir, host.IdentityFile))); err != nil {
return err
if host.IdentityFiles != nil {
for _, identityFile := range host.IdentityFiles {
if _, err := file.WriteString(fmt.Sprintf("\t%s %s\n", "IdentityFile", filepath.Join(user.HomeDir, identityFile))); err != nil {
return err
}
}
}
for key, value := range host.Values {
if _, err := file.WriteString(fmt.Sprintf("\t%s %s\n", key, value)); err != nil {
return err
for _, item := range value {
if _, err := file.WriteString(fmt.Sprintf("\t%s %s\n", key, item)); err != nil {
return err
}
}
}
}
Expand Down
2 changes: 0 additions & 2 deletions todo.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
MUST HAVES:
- data conflicts - how to resolve merging?
- currently it is possible to upload a duplicate ssh_configs entry where host, identity_file, etc. are the same. Probably need to figure this one out
- Allow users to delete certain keys or config entries
- allow users to obtain a list of their machines
- test delete code more
- if EOF is received, need to have nicer error message. Also cleanup better - what if part way thru setup and EOF happens?
- server seems to sometimes panic when websocket connection gets closed in a strange way.
Expand Down

0 comments on commit 6fac24e

Please sign in to comment.