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
This commit is contained in:
parent
a57ffd6ece
commit
a734197c54
33
functions.go
33
functions.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
|
||||
|
18
main.go
18
main.go
@ -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
|
||||
}
|
||||
|
35
methods.go
35
methods.go
@ -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) {
|
||||
|
Loading…
Reference in New Issue
Block a user