ddns/cmd/main.go

104 lines
2.5 KiB
Go

package main
import (
"context"
"flag"
"fmt"
"log/slog"
"os"
"github.com/maximotejeda/ddns/internal/application/core/api"
)
var (
tok string
zid string
name string
ip string
tipe string
operation string
proxied bool
helpMsg string
provider string
rID string
comment string
)
func init() {
flag.BoolVar(&proxied, "p", false, "Proxie request (shothand)")
flag.BoolVar(&proxied, "proxied", false, "Proxie request")
flag.StringVar(&name, "n", "", "REQUIRED: name subdomain (shorthand)")
flag.StringVar(&name, "name", "", "REQUIRED: name subdomain")
flag.StringVar(&ip, "ip", "", "ip to update record")
flag.StringVar(&tipe, "t", "", "type of record (shorthand)")
flag.StringVar(&tipe, "type", "", "type of record ")
flag.StringVar(&operation, "o", "show", "operation to execute (shorthand)")
flag.StringVar(&operation, "operation", "show", "operation to execute")
flag.StringVar(&tok, "tk", "", "authorization token on service (shorthand)")
flag.StringVar(&tok, "token", "", "authorization token on service")
flag.StringVar(&zid, "zid", "", "zone id for record (shorthand)")
flag.StringVar(&zid, "zone-id", "", "zone id for record")
flag.StringVar(&provider, "pv", "cf", "Dns records provider")
flag.StringVar(&rID, "rid", "", "Select record id (shorthand)")
flag.StringVar(&rID, "record-id", "", "Select record id")
flag.StringVar(&comment, "comment", "", "comment")
flag.StringVar(&comment, "c", "", "comment operation")
}
func main() {
flag.Parse()
lvl := new(slog.LevelVar)
switch os.Getenv("ENV") {
case "debug", "dev":
lvl.Set(slog.LevelDebug)
case "prod":
lvl.Set(slog.LevelInfo)
}
log := slog.New(slog.NewJSONHandler(os.Stderr, &slog.HandlerOptions{Level: lvl}))
ctx := context.Background()
helpmsg := "required flag not provided "
if tok == "" {
tok = os.Getenv("TOKEN")
if tok == "" {
fmt.Print(printHelp())
fmt.Print(helpmsg + "[Token]\n")
flag.Usage()
os.Exit(1)
}
}
if zid == "" {
zid = os.Getenv("ZONEID")
if zid == "" {
fmt.Print(printHelp())
fmt.Print(helpmsg + "[zone id]\n")
flag.Usage()
os.Exit(1)
}
}
if name == "" {
name = os.Getenv("NAME")
if name == "" {
fmt.Print(printHelp())
fmt.Print(helpmsg + "[domain name]\n")
flag.Usage()
os.Exit(1)
}
}
app, err := api.NewApplication(ctx, log, provider, tok, zid)
if err != nil {
fmt.Println(zid)
panic(err)
}
app.Operation(operation, name, tipe, ip, rID, comment, proxied)
}
func printHelp() string {
return `
Ddnser Client
Service client for cloudflare API
`
}