modified: functions.go

Added some structure to client requests
modified:   main.go
	AddEndpoint method properly using interface
	See https://www.ardanlabs.com/blog/2014/05/methods-interfaces-and-embedded-types.html
modified:   methods.go
	AddEndpoint using reflect.TypeOf
modified:   types.go
data-work
Mike Holloway 3 months ago
parent a57ffd6ece
commit a734197c54
  1. 33
      functions.go
  2. 18
      main.go
  3. 35
      methods.go
  4. 4
      types.go

@ -1,6 +1,7 @@
package main
import (
"context"
"database/sql"
"github.com/uptrace/bun"
@ -13,3 +14,35 @@ func OpenDB(dsn string) PostgresClient {
r := PostgresClient{bun.NewDB(sqldb, pgdialect.New())}
return r
}
<<<<<<< Updated upstream
=======
func SelectUsers(c *PostgresClient) {
ctx := context.Background()
users := new(UserModel)
c.NewSelect().
Model(&UserModel{}).
Scan(ctx, &users)
}
/*
Prepared for development of the ssh client procedures
*/
//func OpenSSH(dsn string) *SSHClient {
// sshconn := new(SSHClient)
// sshconn.NewSession()
// return sshconn
//}
/*
Prepared for development of the web client procedures
*/
//func OpenWeb(dsn string) WebClient {
// webconn := new(http.Client)
// webreq := NewRequest(MethodGet, dsn,
//
// webconn.Do
// return webconn
//}
>>>>>>> Stashed changes

@ -1,7 +1,12 @@
package main
import (
"database/sql"
"fmt"
"github.com/uptrace/bun"
"github.com/uptrace/bun/dialect/pgdialect"
"github.com/uptrace/bun/driver/pgdriver"
)
// Tofu Provider is the goal
@ -18,6 +23,7 @@ import (
func main() {
var (
<<<<<<< Updated upstream
// ctx = context.Background()
dsn = "postgres://postgres:Initial1@localhost:15432/anvil?sslmode=disable"
// dsn := "unix://user:pass@dbname/var/run/postgresql/.s.PGSQL.5432"
@ -41,4 +47,16 @@ func main() {
// for _, val := range users {
// fmt.Printf("%s\n", val.Username)
// }
=======
pgdsn = "postgres://postgres:Initial1@localhost:15432/anvil?sslmode=disable"
)
anvil := new(Anvil)
anvil.AddStriker(Striker{})
// anvil.Strikers[0].SetFQDN("mh-striker01.libre.audio")
e := &PostgresClient{bun.NewDB(sql.OpenDB(pgdriver.NewConnector(pgdriver.WithDSN(pgdsn))), pgdialect.New())}
anvil.Strikers[0].AddEndpoint(e)
fmt.Printf("Strikers[0].Endpoints.PostgresClient = %s.\n", anvil.Strikers[0].Endpoints.PostgresClient)
// fmt.Printf("Strikers[0].FQDN = %s.\n", anvil.Strikers[0].FQDN)
>>>>>>> Stashed changes
}

@ -1,15 +1,50 @@
package main
<<<<<<< Updated upstream
=======
import (
"fmt"
"reflect"
)
>>>>>>> Stashed changes
func (p PostgresClient) IsEndpoint() bool {
return true
}
<<<<<<< Updated upstream
=======
func (w WebClient) IsEndpoint() bool {
return true
}
func (s SSHClient) IsEndpoint() bool {
return true
}
>>>>>>> Stashed changes
func (s *Striker) SetFQDN(fqdn string) {
s.FQDN = fqdn
}
func (s *Striker) AddEndpoint(e Endpoint) {
<<<<<<< Updated upstream
s.Endpoints = append(s.Endpoints, e)
=======
// type switch statement
// https://go.dev/tour/methods/16
fmt.Printf("e value = %s.\n", reflect.TypeOf(e))
switch t := e.(type) {
case *PostgresClient:
s.Endpoints.PostgresClient = e
case *WebClient:
s.Endpoints.WebClient = e
case *SSHClient:
s.Endpoints.SSHClient = e
default:
fmt.Println("Undefined behavior for type %s.\n", t)
}
>>>>>>> Stashed changes
}
func (a *Anvil) AddStriker(s Striker) {

@ -34,8 +34,12 @@ type Striker struct {
}
type Host struct {
<<<<<<< Updated upstream
FQDN string
Endpoints []Endpoint
=======
FQDN string
>>>>>>> Stashed changes
}
type BMC struct {

Loading…
Cancel
Save