modified: functions.go

OpenSSH, OpenHTTP functions
modified:   main.go
	Client instantiation work
main
Mike Holloway 3 months ago
parent f8385e173d
commit ef318b10e5
  1. 29
      functions.go
  2. 22
      main.go

@ -2,14 +2,39 @@ package main
import ( import (
"database/sql" "database/sql"
"log"
"net/http"
"github.com/uptrace/bun" "github.com/uptrace/bun"
"github.com/uptrace/bun/dialect/pgdialect" "github.com/uptrace/bun/dialect/pgdialect"
"github.com/uptrace/bun/driver/pgdriver" "github.com/uptrace/bun/driver/pgdriver"
"golang.org/x/crypto/ssh"
"golang.org/x/crypto/ssh/knownhosts"
) )
func OpenDB(dsn string) PostgresClient { func OpenDB(dsn string) *bun.DB {
sqldb := sql.OpenDB(pgdriver.NewConnector(pgdriver.WithDSN(dsn))) sqldb := sql.OpenDB(pgdriver.NewConnector(pgdriver.WithDSN(dsn)))
r := PostgresClient{bun.NewDB(sqldb, pgdialect.New())} r := bun.NewDB(sqldb, pgdialect.New())
return r return r
} }
func OpenHTTP() *http.Client {
var cli *http.Client
return cli
}
func OpenSSH(dsn string) *ssh.Client {
knownHosts, _ := knownhosts.New("/home/m00t/.ssh/known_hosts")
config := &ssh.ClientConfig{
User: "admin",
Auth: []ssh.AuthMethod{
ssh.Password("Initial1"),
},
HostKeyCallback: knownHosts,
}
client, err := ssh.Dial("tcp", dsn, config)
if err != nil {
log.Fatal("Failed to dial: ", err)
}
defer client.Close()
return client
}

@ -1,12 +1,7 @@
package main package main
import ( import (
"database/sql"
"fmt" "fmt"
"github.com/uptrace/bun"
"github.com/uptrace/bun/dialect/pgdialect"
"github.com/uptrace/bun/driver/pgdriver"
) )
// Tofu Provider is the goal // Tofu Provider is the goal
@ -23,14 +18,25 @@ import (
func main() { func main() {
var ( var (
pgdsn = "postgres://postgres:Initial1@localhost:15432/anvil?sslmode=disable" pgdsn = "postgres://postgres:Initial1@localhost:15432/anvil?sslmode=disable"
sshdsn = "192.168.1.165:22"
// webdsn = "http://192.168.1.165"
) )
anvil := new(Anvil) anvil := new(Anvil)
anvil.AddStriker(Striker{}) anvil.AddStriker(Striker{})
// anvil.Strikers[0].SetFQDN("mh-striker01.libre.audio") // anvil.Strikers[0].SetFQDN("mh-striker01.libre.audio")
e := &PostgresClient{bun.NewDB(sql.OpenDB(pgdriver.NewConnector(pgdriver.WithDSN(pgdsn))), pgdialect.New())} pge := &PostgresClient{OpenDB(pgdsn)}
anvil.Strikers[0].AddEndpoint(pge)
webe := &WebClient{OpenHTTP()}
anvil.Strikers[0].AddEndpoint(webe)
sshe := &SSHClient{OpenSSH(sshdsn)}
anvil.Strikers[0].AddEndpoint(sshe)
anvil.Strikers[0].AddEndpoint(e)
fmt.Printf("Strikers[0].Endpoints.PostgresClient = %s.\n", anvil.Strikers[0].Endpoints.PostgresClient) fmt.Printf("Strikers[0].Endpoints.PostgresClient = %s.\n", anvil.Strikers[0].Endpoints.PostgresClient)
fmt.Printf("Strikers[0].Endpoints.WebClient = %s.\n", anvil.Strikers[0].Endpoints.WebClient)
fmt.Printf("Strikers[0].Endpoints.SSHClient = %s.\n", anvil.Strikers[0].Endpoints.SSHClient)
// fmt.Printf("Strikers[0].FQDN = %s.\n", anvil.Strikers[0].FQDN) // fmt.Printf("Strikers[0].FQDN = %s.\n", anvil.Strikers[0].FQDN)
} }

Loading…
Cancel
Save