Compare commits

..

No commits in common. "09c1bd44c983460cb768ad3f4a616dd9e814018f" and "5bbe08ded5d06ceb45322e407aa4db628b870d6f" have entirely different histories.

9 changed files with 107 additions and 171 deletions

24
anvildb.go Normal file
View File

@ -0,0 +1,24 @@
package main
import (
"database/sql"
"github.com/uptrace/bun"
"github.com/uptrace/bun/dialect/pgdialect"
"github.com/uptrace/bun/driver/pgdriver"
"github.com/uptrace/bun/extra/bundebug"
)
func ConnectAnvilDB(dsn string) *bun.DB {
sqldb := sql.OpenDB(pgdriver.NewConnector(pgdriver.WithDSN(dsn)))
db := bun.NewDB(sqldb, pgdialect.New())
db.AddQueryHook(bundebug.NewQueryHook(
bundebug.WithVerbose(true),
bundebug.FromEnv("BUNDEBUG"),
))
return db
}
//func GetFeeds(db *bun.DB)

1
db.go
View File

@ -1 +0,0 @@
package main

View File

@ -1,14 +0,0 @@
package main
import (
"database/sql"
"github.com/uptrace/bun"
"github.com/uptrace/bun/dialect/pgdialect"
"github.com/uptrace/bun/driver/pgdriver"
)
func OpenDB(dsn string) *sql.DB {
sqldb := sql.OpenDB(pgdriver.NewConnector(pgdriver.WithDSN(dsn)))
return bun.NewDB(sqldb, pgdialect.New())
}

4
go.mod
View File

@ -3,13 +3,10 @@ module anvil-client
go 1.21.10 go 1.21.10
require ( require (
github.com/google/uuid v1.6.0
github.com/uptrace/bun v1.2.1 github.com/uptrace/bun v1.2.1
github.com/uptrace/bun/dialect/pgdialect v1.2.1 github.com/uptrace/bun/dialect/pgdialect v1.2.1
github.com/uptrace/bun/driver/pgdriver v1.2.1 github.com/uptrace/bun/driver/pgdriver v1.2.1
github.com/uptrace/bun/extra/bundebug v1.2.1 github.com/uptrace/bun/extra/bundebug v1.2.1
golang.org/x/crypto v0.21.0
golang.org/x/term v0.18.0
) )
require ( require (
@ -20,6 +17,7 @@ require (
github.com/tmthrgd/go-hex v0.0.0-20190904060850-447a3041c3bc // indirect github.com/tmthrgd/go-hex v0.0.0-20190904060850-447a3041c3bc // indirect
github.com/vmihailenco/msgpack/v5 v5.4.1 // indirect github.com/vmihailenco/msgpack/v5 v5.4.1 // indirect
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect
golang.org/x/crypto v0.21.0 // indirect
golang.org/x/sys v0.18.0 // indirect golang.org/x/sys v0.18.0 // indirect
mellium.im/sasl v0.3.1 // indirect mellium.im/sasl v0.3.1 // indirect
) )

4
go.sum
View File

@ -2,8 +2,6 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fatih/color v1.16.0 h1:zmkK9Ngbjj+K0yRhTVONQh1p/HknKYSlNT+vZCzyokM= github.com/fatih/color v1.16.0 h1:zmkK9Ngbjj+K0yRhTVONQh1p/HknKYSlNT+vZCzyokM=
github.com/fatih/color v1.16.0/go.mod h1:fL2Sau1YI5c0pdGEVCbKQbLXB6edEj1ZgiY4NijnWvE= github.com/fatih/color v1.16.0/go.mod h1:fL2Sau1YI5c0pdGEVCbKQbLXB6edEj1ZgiY4NijnWvE=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E= github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc= github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
@ -35,8 +33,6 @@ golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4= golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4=
golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.18.0 h1:FcHjZXDMxI8mM3nwhX9HlKop4C0YQvCVCdwYl2wOtE8=
golang.org/x/term v0.18.0/go.mod h1:ILwASektA3OnRv7amZ1xhE/KTR+u50pbXfZ03+6Nx58=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
mellium.im/sasl v0.3.1 h1:wE0LW6g7U83vhvxjC1IY8DnXM+EU095yeo8XClvCdfo= mellium.im/sasl v0.3.1 h1:wE0LW6g7U83vhvxjC1IY8DnXM+EU095yeo8XClvCdfo=

93
main.go
View File

@ -2,42 +2,71 @@ package main
import ( import (
"context" "context"
"errors"
"fmt"
) )
// Tofu Provider is the goal var feeds []Feed
// Data Sources first var categories []Category
// Resources later var entries []Entry
var users []User
var users []UserModel
var ssh_keys []SSHKeyModel
//var servers []Server
//var server_definitions []ServerDefinition
//var manifests []Manifest
//var jobs []Job
//var hosts []Host
func main() { func main() {
var ( var err error
ctx = context.Background() ctx := context.Background()
dsn = "postgres://postgres:Initial1@localhost:15432/anvil?sslmode=disable" dsn := "postgres://apache:@localhost:5432/miniflux?sslmode=disable"
// dsn := "unix://user:pass@dbname/var/run/postgresql/.s.PGSQL.5432" // dsn := "unix://user:pass@dbname/var/run/postgresql/.s.PGSQL.5432"
)
// Calling PostgresClient as an instance method as per:
// https://stackoverflow.com/questions/38666404/not-enough-arguments-in-call-to-method-expression
anvildb := OpenDB(dsn)
anvildb := ConnectAnvilDB(dsn)
// Turn these into funcs with error handling, logging, return vals etc. // Turn these into funcs with error handling, logging, return vals etc.
// if usererr := anvildb.NewSelect(). feederr := anvildb.NewSelect().
// // Turning *User into an interface &User{} Model(&Feed{}).
// Model(&UserModel{}). OrderExpr("title ASC").
// OrderExpr("user_uuid ASC"). Limit(10).
// Limit(10). Scan(ctx, &feeds)
// Scan(ctx, &users); usererr != nil {
// errors.Join(usererr, err) if feederr != nil {
// } errors.Join(feederr, err)
// fmt.Printf("##\n## Users\n###\n\n") }
// for _, val := range users { categoryerr := anvildb.NewSelect().
// fmt.Printf("%s\n", val.Username) Model(&Category{}).
// } OrderExpr("title ASC").
Scan(ctx, &categories)
if categoryerr != nil {
errors.Join(categoryerr, err)
}
entryerr := anvildb.NewSelect().
Model(&Entry{}).
OrderExpr("published_at ASC").
Limit(10).
Scan(ctx, &entries)
if entryerr != nil {
errors.Join(entryerr, err)
}
usererr := anvildb.NewSelect().
Model(&User{}).
OrderExpr("id ASC").
Scan(ctx, &users)
if usererr != nil {
errors.Join(usererr, err)
}
for _, val := range entries {
fmt.Printf("val = %s\n", val)
}
for _, val := range categories {
fmt.Printf("val = %s\n", val)
}
for _, val := range feeds {
fmt.Printf("val = %s\n", val)
}
for _, val := range users {
fmt.Printf("val = %s\n", val)
}
// fmt.Printf("%+v\n", entries)
// fmt.Printf("%+v\n", categories)
// fmt.Printf("%+v\n", feeds)
// fmt.Printf("%+v\n", users)
} }

View File

@ -1,73 +1,37 @@
package main package main
import ( import (
"github.com/google/uuid"
"github.com/uptrace/bun" "github.com/uptrace/bun"
) )
type UserModel struct { type User struct {
bun.BaseModel `bun:"table:users"` bun.BaseModel `bun:"table:users"`
UUID uuid.UUID `bun:"user_uuid,pk"` ID int64 `bun:"id,pk,autoincrement"`
Username string `bun:"user_name"` Username string `bun:"username"`
IsExperienced bool `bun:"user_is_experienced"`
IsTrusted bool `bun:"user_is_trusted"`
// https://pkg.go.dev/github.com/google/uuid#UUID
// SELECT user_uuid,user_name,user_is_experienced,user_is_trusted FROM \
// users ORDER BY user_uuid;
} }
type SSHKeyModel struct { type Feed struct {
bun.BaseModel `bun:"table:ssh_keys"` bun.BaseModel `bun:"table:feeds"`
UUID uuid.UUID `bun:"ssh_key_uuid,pk"` ID int64 `bun:"id,pk,autoincrement"`
Username string `bun:"ssh_key_user_name"` CategoryID int64 `bun:"category_id"`
PublicKey []byte `bun:"ssh_key_public_key"` Title string `bun:"title"`
// https://pkg.go.dev/golang.org/x/crypto/ssh#PublicKey // SELECT id,category_id,title FROM feeds ORDER BY id;
// SELECT ssh_key_uuid,ssh_key_user_name FROM ssh_keys ORDER BY ssh_key_user_name;
} }
type Entry struct {
bun.BaseModel `bun:"table:entries"`
type ServerModel struct { ID int64 `bun:"id,pk,autoincrement"`
Title string `bun:"title"`
Author string `bun:"author"`
// SELECT id,title,author FROM entries ORDER BY published_at LIMIT 10;
} }
type Category struct {
bun.BaseModel `bun:"table:categories"`
ID int64 `bun:"id,pk,autoincrement"`
Title string `bun:"title"`
//SELECT id,title from categories;
type ServerDefinitionModel struct {
}
type ManifestModel struct {
bun.BaseModel `bun:"table:manifests"`
UUID uuid.UUID `bun:"mainifest_uuid,pk"`
Name string `bun:"manifest_name"`
LastRan string `bun:"manifest_last_ran"`
XML string `bun:"manifest_xml"`
Note string `bun:"manifest_note"`
// https://pkg.go.dev/encoding/xml#CharData
}
type JobModel struct {
bun.BaseModel `bun:"table:jobs"`
UUID uuid.UUID `bun:"job_uuid,pk"`
HostUUID uuid.UUID `bun:"host_uuid"`
Command string `bun:"job_command"`
Data string `bun:"job_data"`
PickedUpBy string `bun:"job_picked_up_by"`
PickedUpAt string `bun:"job_picked_up_at"`
Updated string `bun:"job_updated"`
Name string `bun:"job_name"`
Progress int64 `bun:"job_progress"`
Title string `bun:"job_title"`
Description string `bun:"job_description"`
Status string `bun:"job_status"`
}
type HostModel struct {
bun.BaseModel `bun:"table:hosts"`
UUID uuid.UUID `bun:"host_uuid,pk"`
HostName string `bun:"host_name"`
HostType string `bun:"host_type"`
HostKey string `bun:"host_key"`
HostIPMI string `bun:"host_ipmi"`
HostStatus string `bun:"host_status"`
} }

11
ssh.go
View File

@ -1,11 +0,0 @@
package main
import (
"golang.org/x/crypto/ssh"
"golang.org/x/term"
)
type AnvilSSHConnection struct {
thing ssh.Client
otherthing term.Terminal
}

View File

@ -1,49 +0,0 @@
package main
import (
"database/sql"
"net/http"
"golang.org/x/crypto/ssh"
)
type Anvil struct {
Strikers []Striker
Hosts []Host
BMCs []BMC
Switches []Switch
PDUs []PDU
UPSes []UPS
}
type WebClient struct {
*http.Client
}
type SSHClient struct {
*ssh.Client
}
type PostgresClient struct {
*sql.DB
}
type Striker struct {
FQDN string
}
type Host struct {
FQDN string
}
type BMC struct {
}
type Switch struct {
}
type PDU struct {
}
type UPS struct {
}