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 package main
import ( import (
"context"
"database/sql" "database/sql"
"github.com/uptrace/bun" "github.com/uptrace/bun"
@ -13,3 +14,35 @@ func OpenDB(dsn string) PostgresClient {
r := PostgresClient{bun.NewDB(sqldb, pgdialect.New())} r := PostgresClient{bun.NewDB(sqldb, pgdialect.New())}
return r 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 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
@ -18,6 +23,7 @@ import (
func main() { func main() {
var ( var (
<<<<<<< Updated upstream
// ctx = context.Background() // ctx = context.Background()
dsn = "postgres://postgres:Initial1@localhost:15432/anvil?sslmode=disable" dsn = "postgres://postgres:Initial1@localhost:15432/anvil?sslmode=disable"
// dsn := "unix://user:pass@dbname/var/run/postgresql/.s.PGSQL.5432" // dsn := "unix://user:pass@dbname/var/run/postgresql/.s.PGSQL.5432"
@ -41,4 +47,16 @@ func main() {
// for _, val := range users { // for _, val := range users {
// fmt.Printf("%s\n", val.Username) // 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 package main
<<<<<<< Updated upstream
=======
import (
"fmt"
"reflect"
)
>>>>>>> Stashed changes
func (p PostgresClient) IsEndpoint() bool { func (p PostgresClient) IsEndpoint() bool {
return true 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) { func (s *Striker) SetFQDN(fqdn string) {
s.FQDN = fqdn s.FQDN = fqdn
} }
func (s *Striker) AddEndpoint(e Endpoint) { func (s *Striker) AddEndpoint(e Endpoint) {
<<<<<<< Updated upstream
s.Endpoints = append(s.Endpoints, e) 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) { func (a *Anvil) AddStriker(s Striker) {

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

Loading…
Cancel
Save