sh-admin: new command
This commit is contained in:
parent
69cf99e367
commit
69a9e20a0a
2 changed files with 62 additions and 2 deletions
7
Makefile
7
Makefile
|
@ -1,11 +1,14 @@
|
||||||
.POSIX:
|
.POSIX:
|
||||||
.SUFFIXES:
|
.SUFFIXES:
|
||||||
|
|
||||||
all: sh-api sh-index sh-search sh-web
|
all: sh-admin sh-api sh-index sh-search sh-web
|
||||||
|
|
||||||
generate:
|
generate:
|
||||||
go generate ./...
|
go generate ./...
|
||||||
|
|
||||||
|
sh-admin: generate
|
||||||
|
go build -o sh-admin cmd/sh-admin/main.go
|
||||||
|
|
||||||
sh-api: generate
|
sh-api: generate
|
||||||
go build -o sh-api cmd/sh-api/main.go
|
go build -o sh-api cmd/sh-api/main.go
|
||||||
|
|
||||||
|
@ -18,4 +21,4 @@ sh-search: generate
|
||||||
sh-web: generate
|
sh-web: generate
|
||||||
go build -o sh-web cmd/sh-web/main.go
|
go build -o sh-web cmd/sh-web/main.go
|
||||||
|
|
||||||
.PHONY: all sh-api sh-index sh-search sh-web generate
|
.PHONY: all sh-admin sh-api sh-index sh-search sh-web generate
|
||||||
|
|
57
cmd/sh-admin/main.go
Normal file
57
cmd/sh-admin/main.go
Normal file
|
@ -0,0 +1,57 @@
|
||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"database/sql"
|
||||||
|
"log"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"git.sr.ht/~sircmpwn/getopt"
|
||||||
|
"github.com/lib/pq"
|
||||||
|
|
||||||
|
"git.sr.ht/~sircmpwn/searchhut/config"
|
||||||
|
)
|
||||||
|
|
||||||
|
func main() {
|
||||||
|
var (
|
||||||
|
authoritative bool
|
||||||
|
tags []string
|
||||||
|
)
|
||||||
|
opts, optind, err := getopt.Getopts(os.Args, "at:")
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
for _, opt := range opts {
|
||||||
|
switch opt.Option {
|
||||||
|
case 'a':
|
||||||
|
authoritative = true
|
||||||
|
case 't':
|
||||||
|
tags = append(tags, opt.Value)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
args := os.Args[optind:]
|
||||||
|
domain := args[0]
|
||||||
|
|
||||||
|
conf := config.Load()
|
||||||
|
connstr, ok := conf.Get("searchhut", "connection-string")
|
||||||
|
if !ok {
|
||||||
|
log.Fatal("Configuration missing connection string")
|
||||||
|
}
|
||||||
|
|
||||||
|
db, err := sql.Open("postgres", connstr)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
var id int
|
||||||
|
row := db.QueryRow(`
|
||||||
|
INSERT INTO domain (
|
||||||
|
hostname,
|
||||||
|
authoritative,
|
||||||
|
tags
|
||||||
|
) VALUES ($1, $2, coalesce($3, '{}'::text[])) RETURNING id;
|
||||||
|
`, domain, authoritative, pq.Array(tags))
|
||||||
|
if err := row.Scan(&id); err != nil {
|
||||||
|
log.Fatal(err)
|
||||||
|
}
|
||||||
|
log.Printf("Added domain %s with ID %d", domain, id)
|
||||||
|
}
|
Loading…
Reference in a new issue