Skip to content

Commit

Permalink
Merge pull request #27 from lpxxn/config_file
Browse files Browse the repository at this point in the history
feat: config ssh
  • Loading branch information
lpxxn authored Jun 17, 2021
2 parents ee87a45 + f813cda commit 922b744
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 55 deletions.
45 changes: 45 additions & 0 deletions cmd/doraemon/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package main

import (
"fmt"

"github.com/lpxxn/doraemon/config"
"github.com/lpxxn/doraemon/utils"
)

func runGlobalCmd(cmdName string) (ran bool, needExist bool) {
if _, ok := existCommand[cmdName]; ok {
return true, true
}
if openConfigDir == cmdName {
if err := config.OpenConfDir(); err != nil {
fmt.Println(err)
}
return true, false
}
return false, false
}

func runCustomCmd(cmdName string) error {
item, err := config.CustomConfigByName(cmdName)
if err != nil {
return err
}
return utils.RunCmd(item.Cmd)
}

func startSSHShell(sshName string) error {
sshConfig, err := config.SSHConfigByName(sshName)
if err != nil {
return err
}
client, err := utils.NewSSHClient(sshConfig)
if err != nil {
return err
}
session, err := client.CreateSession()
if err != nil {
return err
}
return client.Shell(session)
}
41 changes: 0 additions & 41 deletions cmd/doraemon/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,47 +150,6 @@ func RootCMD(param cmdParam) *cobra.Command {
return rootCmd
}

func runGlobalCmd(cmdName string) (ran bool, needExist bool) {
if _, ok := existCommand[cmdName]; ok {
return true, true
}
if openConfigDir == cmdName {
if err := config.OpenConfDir(); err != nil {
fmt.Println(err)
}
return true, false
}
return false, false
}

func startSSHShell(sshName string) error {
sshConfig, err := config.SSHConfigByName(sshName)
if err != nil {
return err
}
client, err := utils.NewSSHClient(sshConfig)
if err != nil {
return err

}
// Create Session
session, err := client.CreateSession()
if err != nil {
return err
}

// Start ssh shell
return client.Shell(session)
}

func runCustomCmd(cmdName string) error {
item, err := config.CustomConfigByName(cmdName)
if err != nil {
return err
}
return utils.RunCmd(item.Cmd)
}

func customCmd(rootCmd *cobra.Command, param cmdParam) {
cmd := &cobra.Command{
Use: "cmd",
Expand Down
26 changes: 12 additions & 14 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ func (s *sshInfo) ToSSHConfig() (utils.SSHConfig, error) {
}

func (s *sshInfo) sshPwdConf() (utils.SSHConfig, error) {
sshConf := &utils.SSHPasswordConfig{SSHBaseConfig: &utils.SSHBaseConfig{
return &utils.SSHPasswordConfig{SSHBaseConfig: &utils.SSHBaseConfig{
MethodName: utils.Password,
URI: s.URI,
User: s.User,
Expand All @@ -183,20 +183,10 @@ func (s *sshInfo) sshPwdConf() (utils.SSHConfig, error) {
Passphrase: s.Passphrase,
StartCommand: s.StartCommand,
},
}
return sshConf, nil
}, nil
}

func (s *sshInfo) sshPublicKeyConf() (utils.SSHConfig, error) {
sshConf := &utils.SSHPrivateKeyConfig{SSHBaseConfig: &utils.SSHBaseConfig{
MethodName: utils.PublicKey,
URI: s.URI,
User: s.User,
Timout: s.Timout,
Passphrase: s.Passphrase,
StartCommand: s.StartCommand,
},
}
pemBytes, err := ioutil.ReadFile(s.PublicKeyPath)
if err != nil {
log.Fatal(err)
Expand All @@ -212,8 +202,16 @@ func (s *sshInfo) sshPublicKeyConf() (utils.SSHConfig, error) {
log.Printf("parse key failed:%v", err)
return nil, err
}
sshConf.AuthMethods = []ssh.AuthMethod{ssh.PublicKeys(signer)}
return sshConf, nil
return &utils.SSHPrivateKeyConfig{SSHBaseConfig: &utils.SSHBaseConfig{
MethodName: utils.PublicKey,
URI: s.URI,
User: s.User,
AuthMethods: []ssh.AuthMethod{ssh.PublicKeys(signer)},
Timout: s.Timout,
Passphrase: s.Passphrase,
StartCommand: s.StartCommand,
},
}, nil
}

func (s *sshInfo) HaveProxy() bool {
Expand Down

0 comments on commit 922b744

Please sign in to comment.