Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
grishy committed Jul 25, 2023
1 parent de929fa commit 8c2c49d
Show file tree
Hide file tree
Showing 6 changed files with 675 additions and 1 deletion.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
# go-avahi-cname
# go-avahi-cname

## Source of inspiration

- https://gist.github.com/gdamjan/3168336
30 changes: 30 additions & 0 deletions avahi/avahi.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package avahi

import (
"fmt"
"os"

"github.com/godbus/dbus/v5"
)

const (
// From Avahi Python bindings
DBUS_NAME = "org.freedesktop.Avahi"
DBUS_INTERFACE_SERVER = DBUS_NAME + ".Server"
DBUS_PATH_SERVER = "/"
)

func PublishCNAME(cname string) {
fmt.Println("Publishing CNAME:", cname)

fmt.Println("Connecting to session bus")
conn, err := dbus.ConnectSessionBus()
if err != nil {
fmt.Fprintln(os.Stderr, "Failed to connect to session bus:", err)
os.Exit(1)
}
defer conn.Close()

fmt.Println("Connecting to Avahi server")

}
115 changes: 115 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package cmd

import (
"fmt"
"os"
"strings"

"github.com/spf13/cobra"
"github.com/spf13/viper"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)

// Env and params from: https://carolynvanslyck.com/blog/2020/08/sting-of-the-viper/
// Code https://github.com/carolynvs/stingoftheviper/blob/main/main.go#L11

const (
// The environment variable prefix of all environment variables bound to our command line flags.
// For example, --number is bound to AVAHI_CNAME_NUMBER.
envPrefix = "AVAHI_CNAME"
)

var (
logger *zap.Logger
verbose bool
)

func init() {
rootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "Enables debug logging")

cobra.OnInitialize(onInitialize)
}

func onInitialize() {
zapOptions := []zap.Option{
zap.AddStacktrace(zapcore.FatalLevel),
zap.AddCallerSkip(1),
}
if !verbose {
zapOptions = append(zapOptions,
zap.IncreaseLevel(zap.LevelEnablerFunc(func(l zapcore.Level) bool { return l != zapcore.DebugLevel })),
)
}

var err error
logger, err = zap.NewDevelopment(zapOptions...)
if err != nil {
fmt.Printf("Failed to initialize logger: %v\n", err)
os.Exit(-1)
}
}

var rootCmd = &cobra.Command{
Use: "go-avahi-cname",
Short: "Register a mDNS/DNS-SD alias name for your computer using the Avahi daemon",
// TODO: Write long
Long: ``,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
// You can bind cobra and viper in a few locations, but PersistencePreRunE on the root command works well
return initializeConfig(cmd)
},
Run: func(cmd *cobra.Command, args []string) {
fmt.Println(123)
},
}

func Execute() {
if err := rootCmd.Execute(); err != nil {
logger.Fatal("We bowled a googly", zap.Error(err))
os.Exit(-1)
}
}

func initializeConfig(cmd *cobra.Command) error {
v := viper.New()

// When we bind flags to environment variables expect that the
// environment variables are prefixed, e.g. a flag like --number
// binds to an environment variable STING_NUMBER. This helps
// avoid conflicts.
v.SetEnvPrefix(envPrefix)

// Environment variables can't have dashes in them, so bind them to their equivalent
// keys with underscores, e.g. --favorite-color to STING_FAVORITE_COLOR
v.SetEnvKeyReplacer(strings.NewReplacer("-", "_"))

// Bind to environment variables
// Works great for simple config names, but needs help for names
// like --favorite-color which we fix in the bindFlags function
v.AutomaticEnv()

// Bind the current command's flags to viper
// bindFlags(cmd, v)

return nil
}

// Bind each cobra flag to its associated viper configuration (config file and environment variable)
// func bindFlags(cmd *cobra.Command, v *viper.Viper) {
// cmd.Flags().VisitAll(func(f *pflag.Flag) {
// // Determine the naming convention of the flags when represented in the config file
// configName := f.Name
// // If using camelCase in the config file, replace hyphens with a camelCased string.
// // Since viper does case-insensitive comparisons, we don't need to bother fixing the case, and only need to remove the hyphens.
// if replaceHyphenWithCamelCase {
// configName = strings.ReplaceAll(f.Name, "-", "")
// }

// // Apply the viper config value to the flag when the flag is not set and viper has a value
// if !f.Changed && v.IsSet(configName) {
// val := v.Get(configName)
// cmd.Flags().Set(f.Name, fmt.Sprintf("%v", val))
// }
// })
// }
27 changes: 27 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
module github.com/grishy/go-avahi-cname

go 1.20

require (
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/godbus/dbus/v5 v5.1.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/pelletier/go-toml/v2 v2.0.8 // indirect
github.com/spf13/afero v1.9.5 // indirect
github.com/spf13/cast v1.5.1 // indirect
github.com/spf13/cobra v1.7.0 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/spf13/viper v1.16.0 // indirect
github.com/subosito/gotenv v1.4.2 // indirect
go.uber.org/atomic v1.11.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.24.0 // indirect
golang.org/x/sys v0.8.0 // indirect
golang.org/x/text v0.9.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading

0 comments on commit 8c2c49d

Please sign in to comment.