Skip to content

Commit

Permalink
feat(inventory_type.go): adds default ssh port to 22, reads ssh file …
Browse files Browse the repository at this point in the history
…from config
  • Loading branch information
struckchure committed Aug 21, 2024
1 parent 3a1b5d5 commit 5c43a1b
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions inventory_type.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package storm

import (
"fmt"
"os"
)

type InventoryConfig struct {
Servers []Server `yaml:"servers"`
}
Expand All @@ -15,4 +20,47 @@ type Server struct {
User string `yaml:"user"`
SudoPassword string `yaml:"sudo-pass"`
SshPassword string `yaml:"ssh-pass"`

// File path to the SSH private key
PrivateSshKey string `yaml:"private-ssh-key"`
}

// Custom UnmarshalYAML to read the private SSH key file
func (s *Server) UnmarshalYAML(unmarshal func(interface{}) error) error {
type rawServer Server // Create a new type to avoid recursion
raw := rawServer{
Port: 22, // Set default SSH port
}

if err := unmarshal(&raw); err != nil {
return err
}

if raw.PrivateSshKey != "" {
// Check if the file exists
if _, err := os.Stat(raw.PrivateSshKey); os.IsNotExist(err) {
err = fmt.Errorf("SSH private key file %s does not exist", raw.PrivateSshKey)

fmt.Println(err)

return err
}

// Now read the private SSH key file content
keyContent, err := os.ReadFile(raw.PrivateSshKey)
if err != nil {
err = fmt.Errorf("failed to read SSH private key file %s: %w", raw.PrivateSshKey, err)

fmt.Println(err)

return err
}

raw.PrivateSshKey = string(keyContent)
}

// Assign unmarshaled and processed values back to the original struct
*s = Server(raw)

return nil
}

0 comments on commit 5c43a1b

Please sign in to comment.