-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ec56a89
commit 879a7f7
Showing
7 changed files
with
151 additions
and
15 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// Package cmd /* | ||
package cmd | ||
|
||
import ( | ||
"errors" | ||
"github.com/TwiN/go-color" | ||
"github.com/spf13/cobra" | ||
"log" | ||
"ssm-v2/internal/ssh" | ||
) | ||
|
||
var ( | ||
username string | ||
group string | ||
alias string | ||
environment string | ||
allowedEnvironmentValues = []string{"dev", "staging", "prod"} | ||
) | ||
|
||
// addCmd represents the add command | ||
var addCmd = &cobra.Command{ | ||
Use: "add", | ||
Short: "A brief description of your command", | ||
Long: ``, | ||
Args: func(cmd *cobra.Command, args []string) error { | ||
if len(args) < 1 { | ||
return errors.New(color.InRed("Requires hostname of the machine")) | ||
} | ||
return nil | ||
}, | ||
Run: func(cmd *cobra.Command, args []string) { | ||
validEnvironment := false | ||
for _, env := range allowedEnvironmentValues { | ||
if env == environment { | ||
validEnvironment = true | ||
} | ||
} | ||
if !validEnvironment { | ||
log.Fatal(color.InRed("Invalid environment value")) | ||
} | ||
addServer(args[0]) | ||
}, | ||
} | ||
|
||
func addServer(host string) { | ||
password, err := ssh.AskPassword() | ||
if err != nil { | ||
log.Fatal(color.InRed("Error reading password")) | ||
} | ||
ssh.InitSSHConnection(username, password, host) | ||
} | ||
|
||
func init() { | ||
rootCmd.AddCommand(addCmd) | ||
addCmd.Flags().StringVarP(&username, "username", "u", "root", "Username to use") | ||
addCmd.Flags().StringVarP(&group, "group", "g", "", "Group to use") | ||
addCmd.Flags().StringVarP(&alias, "alias", "a", "", "Alias to use") | ||
addCmd.Flags().StringVarP(&environment, "environment", "e", "dev", "Environment to use") | ||
_ = addCmd.MarkFlagRequired("group") | ||
_ = addCmd.MarkFlagRequired("alias") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package ssh | ||
|
||
import ( | ||
"fmt" | ||
"golang.org/x/crypto/ssh/terminal" | ||
"os" | ||
) | ||
|
||
func AskPassword() (string, error) { | ||
fmt.Print("Password: ") | ||
password, err := terminal.ReadPassword(int(os.Stdin.Fd())) | ||
if err != nil { | ||
return "", err | ||
} | ||
fmt.Print("\n") | ||
return string(password), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package ssh | ||
|
||
import ( | ||
"fmt" | ||
"github.com/TwiN/go-color" | ||
"golang.org/x/crypto/ssh" | ||
"log" | ||
"os" | ||
"time" | ||
) | ||
|
||
const SshPort = 22 | ||
|
||
func InitSSHConnection(user, password, host string) { | ||
config := &ssh.ClientConfig{ | ||
User: user, | ||
Auth: []ssh.AuthMethod{ | ||
ssh.Password(password), | ||
}, | ||
HostKeyCallback: ssh.InsecureIgnoreHostKey(), | ||
HostKeyAlgorithms: []string{ssh.KeyAlgoRSA, ssh.KeyAlgoDSA, ssh.KeyAlgoED25519, ssh.KeyAlgoECDSA256, ssh.KeyAlgoECDSA384, ssh.KeyAlgoECDSA521}, | ||
Timeout: 5 * time.Second, | ||
} | ||
client, err := ssh.Dial("tcp", fmt.Sprintf("%s:%d", host, SshPort), config) | ||
if err != nil { | ||
log.Fatal(color.InRed(err.Error())) | ||
} | ||
defer func(client *ssh.Client) { | ||
err := client.Close() | ||
if err != nil { | ||
log.Fatalf(color.InRed("Failed to close SSH connection")) | ||
} | ||
}(client) | ||
|
||
session, err := client.NewSession() | ||
if err != nil { | ||
log.Fatal(color.InRed(err.Error())) | ||
} | ||
|
||
defer func(session *ssh.Session) { | ||
err := session.Close() | ||
if err != nil { | ||
log.Fatalf(color.InRed("Failed to close SSH connection")) | ||
} | ||
}(session) | ||
|
||
session.Stdout = os.Stdout | ||
session.Stderr = os.Stderr | ||
|
||
} |