-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
69 lines (61 loc) · 1.78 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package main
import (
"fmt"
"os"
"github.com/spf13/cobra"
)
var config struct {
Debug bool
APIURL string
APIKey string
Zone string
Filter string
ExcludePattern string
}
func main() {
rootCmd := &cobra.Command{
Use: "pdnsutil",
Short: "PowerDNS CLI utility",
Long: `pdnsutil is a CLI utility for managing PowerDNS records.
It allows listing and deleting DNS records based on filters.`,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
// Read from environment variables first
if env := os.Getenv("PDNS_API_URL"); env != "" {
config.APIURL = env
}
if env := os.Getenv("PDNS_API_KEY"); env != "" {
config.APIKey = env
}
if env := os.Getenv("PDNS_ZONE"); env != "" {
config.Zone = env
}
// Check if required values are set (either via flags or env vars)
if config.APIURL == "" {
return fmt.Errorf("API URL is required (use --api-url flag or PDNS_API_URL env var)")
}
if config.APIKey == "" {
return fmt.Errorf("API key is required (use --api-key flag or PDNS_API_KEY env var)")
}
if config.Zone == "" {
return fmt.Errorf("Zone is required (use --zone flag or PDNS_ZONE env var)")
}
return nil
},
}
// Global flags
rootCmd.PersistentFlags().BoolVarP(&config.Debug, "debug", "d", false,
"enable debug mode")
rootCmd.PersistentFlags().StringVarP(&config.APIURL, "api-url", "a", "",
"PowerDNS API URL")
rootCmd.PersistentFlags().StringVarP(&config.APIKey, "api-key", "k", "",
"PowerDNS API key")
rootCmd.PersistentFlags().StringVarP(&config.Zone, "zone", "z", "",
"zone name")
// Add subcommands
rootCmd.AddCommand(newListRecordsCmd())
rootCmd.AddCommand(newDeleteRecordsCmd())
if err := rootCmd.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}