Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move business logic to cmd package #47

Merged
merged 4 commits into from
Dec 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 0 additions & 9 deletions cmd/common.go

This file was deleted.

12 changes: 6 additions & 6 deletions cmd/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/spf13/cobra"
)

func newDeployCmd(app *App) *cobra.Command {
func newDeployCmd(provider dns.Provider) *cobra.Command {
var shouldCreate bool
var shouldDelete bool

Expand All @@ -20,7 +20,7 @@ func newDeployCmd(app *App) *cobra.Command {
creating new records.`,
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
return deploy(app, args[0], shouldCreate, shouldDelete)
return deploy(provider, args[0], shouldCreate, shouldDelete)
},
}

Expand All @@ -30,13 +30,13 @@ creating new records.`,
return deploy
}

func deploy(app *App, configFile string, shouldCreate bool, shouldDelete bool) error {
func deploy(provider dns.Provider, configFile string, shouldCreate bool, shouldDelete bool) error {
config, err := config.ReadFile(configFile)
if err != nil {
return fmt.Errorf("reading config: %v", err)
}

from, err := app.Provider.AllRecords(config.Domain)
from, err := provider.AllRecords(config.Domain)
if err != nil {
return fmt.Errorf("fetching existing records: %v", err)
}
Expand All @@ -51,7 +51,7 @@ func deploy(app *App, configFile string, shouldCreate bool, shouldDelete bool) e
if shouldDelete {
fmt.Println("Deleting", len(removed), "records...")
for _, record := range removed {
err := app.Provider.DeleteRecord(config.Domain, record)
err := provider.DeleteRecord(config.Domain, record)
if err != nil {
return fmt.Errorf("couldn't delete record: %v", err)
}
Expand All @@ -66,7 +66,7 @@ func deploy(app *App, configFile string, shouldCreate bool, shouldDelete bool) e
if shouldCreate {
fmt.Println("Creating", len(added), "records...")
for _, record := range added {
err := app.Provider.CreateRecord(config.Domain, record)
err := provider.CreateRecord(config.Domain, record)
if err != nil {
return fmt.Errorf("couldn't create record: %v", err)
}
Expand Down
9 changes: 3 additions & 6 deletions cmd/deploy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,14 @@ records:
t.Fatal("could not seed config to temp file", err)
}

provider := console.NewMockProvider()
mockApp := &App{
Provider: provider,
}
mockProvider := console.NewMockProvider()

err = deploy(mockApp, configFile, true, true)
err = deploy(mockProvider, configFile, true, true)
if err != nil {
t.Fatal("did not deploy records", err)
}

records, err := provider.AllRecords("plantbasedbacon.xyz")
records, err := mockProvider.AllRecords("plantbasedbacon.xyz")
if err != nil {
t.Fatal("could not fetch records after deployment", err)
}
Expand Down
9 changes: 5 additions & 4 deletions cmd/ping.go
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
package cmd

import (
"bacon/pkg/dns"
"fmt"

"github.com/spf13/cobra"
)

func newPingCmd(app *App) *cobra.Command {
func newPingCmd(provider dns.Provider) *cobra.Command {
ping := &cobra.Command{
Use: "ping",
Short: "Check authentication status",
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
return ping(app)
return ping(provider)
},
}
return ping
}

func ping(app *App) error {
err := app.Provider.CheckAuth()
func ping(provider dns.Provider) error {
err := provider.CheckAuth()
if err != nil {
return err
}
Expand Down
9 changes: 5 additions & 4 deletions cmd/print.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,28 @@ package cmd

import (
"bacon/pkg/config"
"bacon/pkg/dns"
"fmt"
"strconv"

"github.com/spf13/cobra"
"gopkg.in/yaml.v3"
)

func newPrintCmd(app *App) *cobra.Command {
func newPrintCmd(provider dns.Provider) *cobra.Command {
print := &cobra.Command{
Use: "print <domain>",
Short: "Print existing records",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
return print(app, args[0])
return print(provider, args[0])
},
}
return print
}

func print(app *App, domain string) error {
records, err := app.Provider.AllRecords(domain)
func print(provider dns.Provider, domain string) error {
records, err := provider.AllRecords(domain)
if err != nil {
return err
}
Expand Down
27 changes: 17 additions & 10 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,31 +1,38 @@
package cmd

import (
"fmt"
"bacon/pkg/dns"
"bacon/pkg/providers/porkbun"
"os"

"github.com/spf13/cobra"
"github.com/subosito/gotenv"
)

func Execute(app *App) {
root := newRootCmd(app)
func Execute() {
// Loads .env in the current directory
gotenv.Load()

if err := root.Execute(); err != nil {
fmt.Println(err)
var porkbunApiKey = os.Getenv("PORKBUN_API_KEY")
var porkbunSecretApiKey = os.Getenv("PORKBUN_SECRET_KEY")
var porkbunProvider = porkbun.NewPorkbunProvider(porkbunApiKey, porkbunSecretApiKey)

root := newRootCmd(porkbunProvider)
err := root.Execute()
if err != nil {
os.Exit(1)
}
}

func newRootCmd(app *App) *cobra.Command {
func newRootCmd(provider dns.Provider) *cobra.Command {
root := &cobra.Command{
Use: "bacon",
Short: "Bacon is a tasty DNS manager for Porkbun",
}

// Use command constructors to share one app
root.AddCommand(newPingCmd(app))
root.AddCommand(newDeployCmd(app))
root.AddCommand(newPrintCmd(app))
root.AddCommand(newPingCmd(provider))
root.AddCommand(newDeployCmd(provider))
root.AddCommand(newPrintCmd(provider))

return root
}
16 changes: 1 addition & 15 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,8 @@ package main

import (
"bacon/cmd"
"bacon/pkg/providers/porkbun"
"os"

"github.com/subosito/gotenv"
)

func main() {
// Loads .env in the current directory
gotenv.Load()

var porkbunApiKey = os.Getenv("PORKBUN_API_KEY")
var porkbunSecretApiKey = os.Getenv("PORKBUN_SECRET_KEY")

app := cmd.App{
Provider: porkbun.NewPorkbunProvider(porkbunApiKey, porkbunSecretApiKey),
}

cmd.Execute(&app)
cmd.Execute()
}
Loading