Skip to content

Commit

Permalink
Expose sftp configs via methods of Config struct
Browse files Browse the repository at this point in the history
  • Loading branch information
arunvelsriram committed Aug 9, 2020
1 parent 1034c46 commit ee1fd1f
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 13 deletions.
7 changes: 3 additions & 4 deletions pkg/client/sftp_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,11 @@ type defaultSFTPClient struct {
}

func NewSFTPClient(cfg config.Config) (SFTPClient, error) {
sftpConfig := cfg.GetSFTPConfig()
addr := fmt.Sprintf("%s:%d", sftpConfig.Host, sftpConfig.Port)
addr := fmt.Sprintf("%s:%d", cfg.GetSFTPHost(), cfg.GetSFTPPort())
clientConfig := &ssh.ClientConfig{
User: sftpConfig.User,
User: cfg.GetSFTPUser(),
Auth: []ssh.AuthMethod{
ssh.Password(sftpConfig.Pass),
ssh.Password(cfg.GetSFTPPass()),
},
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
}
Expand Down
31 changes: 23 additions & 8 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"github.com/spf13/viper"
)

type SFTPConfig struct {
type sftpConfig struct {
Host string
Port int
User string
Expand All @@ -16,22 +16,25 @@ type Config interface {
GetBindAddress() string
GetPort() int
GetLogLevel() string
GetSFTPConfig() SFTPConfig
GetSFTPHost() string
GetSFTPPort() int
GetSFTPUser() string
GetSFTPPass() string
}

type sftpExporterConfig struct {
BindAddress string
Port int
LogLevel string
SFTPConfig SFTPConfig
SFTPConfig sftpConfig
}

func LoadConfig() Config {
return sftpExporterConfig{
BindAddress: viper.GetString(c.ViperKeyBindAddress),
Port: viper.GetInt(c.ViperKeyPort),
LogLevel: viper.GetString(c.ViperKeyLogLevel),
SFTPConfig: SFTPConfig{
SFTPConfig: sftpConfig{
Host: viper.GetString(c.ViperKeySFTPHost),
Port: viper.GetInt(c.ViperKeySFTPPort),
User: viper.GetString(c.ViperKeySFTPUser),
Expand All @@ -48,10 +51,22 @@ func (c sftpExporterConfig) GetPort() int {
return c.Port
}

func (c sftpExporterConfig) GetSFTPConfig() SFTPConfig {
return c.SFTPConfig
}

func (c sftpExporterConfig) GetLogLevel() string {
return c.LogLevel
}

func (c sftpExporterConfig) GetSFTPHost() string {
return c.SFTPConfig.Host
}

func (c sftpExporterConfig) GetSFTPPort() int {
return c.SFTPConfig.Port
}

func (c sftpExporterConfig) GetSFTPUser() string {
return c.SFTPConfig.User
}

func (c sftpExporterConfig) GetSFTPPass() string {
return c.SFTPConfig.Pass
}
2 changes: 1 addition & 1 deletion pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func TestNewConfig(t *testing.T) {
expected := sftpExporterConfig{
BindAddress: "127.0.0.1",
Port: 8080,
SFTPConfig: SFTPConfig{
SFTPConfig: sftpConfig{
Host: "localhost",
Port: 22,
User: "arun",
Expand Down

0 comments on commit ee1fd1f

Please sign in to comment.