add scaffolding for configuration file

This commit is contained in:
2024-04-17 19:12:01 -06:00
parent a0d118b987
commit e95b4972da
4 changed files with 111 additions and 19 deletions

33
hypd/cmd/defaultconfig.go Normal file
View File

@ -0,0 +1,33 @@
/*
Copyright © 2024 Steven Polley <himself@stevenpolley.net>
*/
package cmd
import (
"encoding/json"
"fmt"
"deadbeef.codes/steven/hyp/hypd/configuration"
"github.com/spf13/cobra"
)
// defaultconfigCmd represents the defaultconfig command
var defaultconfigCmd = &cobra.Command{
Use: "defaultconfig",
Short: "Prints the default configuration to stdout",
Long: `The default configuration is used if one is not set. The default configuration
can be used as a reference to build your own. `,
Run: func(cmd *cobra.Command, args []string) {
config := configuration.DefaultConfig()
b, err := json.MarshalIndent(config, "", " ")
if err != nil {
panic(fmt.Errorf("failed to marshal default configuration to json (this should never happen): %v", err))
}
fmt.Println(string(b))
},
}
func init() {
generateCmd.AddCommand(defaultconfigCmd)
}

View File

@ -6,6 +6,7 @@ package cmd
import (
"fmt"
"deadbeef.codes/steven/hyp/hypd/configuration"
"deadbeef.codes/steven/hyp/hypd/server"
"github.com/spf13/cobra"
)
@ -30,7 +31,16 @@ Example Usage:
`,
Run: func(cmd *cobra.Command, args []string) {
err := server.PacketServer(args[0])
configFile, err := cmd.Flags().GetString("configfile")
if err != nil {
panic(fmt.Errorf("failed to get configfile flag: %w", err))
}
hypdConfiguration, err := configuration.LoadConfiguration(configFile)
if err != nil {
panic(fmt.Errorf("failed to start packet server: %w", err))
}
err = server.PacketServer(args[0], hypdConfiguration)
if err != nil {
panic(fmt.Errorf("failed to start packet server: %w", err))
}
@ -40,22 +50,6 @@ Example Usage:
func init() {
rootCmd.AddCommand(serverCmd)
/*
viper.SetConfigName("hypconfig")
viper.SetConfigType("yaml")
viper.AddConfigPath("/etc/hyp/")
viper.AddConfigPath("$HOME/.hyp")
viper.AddConfigPath(".")
viper.SetDefault("RefreshInterval", 7200)
if err := viper.ReadInConfig(); err != nil {
if _, ok := err.(viper.ConfigFileNotFoundError); ok {
// Config file not found
// TBD: Implement
} else {
// Config file was found, but another error was produced
panic(fmt.Errorf("failed reading existing config file: %w", err))
}
}*/
serverCmd.PersistentFlags().String("configfile", "", "Path to the file containing the hypd configuration.")
}