Add function takes hostname

this allows the use of cobra flags to configure the hostname.
This commit is contained in:
Marvin Steadfast 2020-12-31 10:46:18 +01:00
parent 20df6c559c
commit b748ebd619
2 changed files with 10 additions and 7 deletions

7
add.go
View File

@ -187,17 +187,16 @@ func GetWGPeerTemplate(peerConfType PeerConfType, peer *PeerConfig, conf *DsnetC
} }
// Add prompts for the required information and creates a new peer // Add prompts for the required information and creates a new peer
func Add() { func Add(hostname string) {
if len(os.Args) != 3 { if hostname == "" {
// TODO non-red // TODO non-red
ExitFail("Hostname argument required: dsnet add <hostname>") ExitFail("Hostname required: dsnet add --hostname <hostname>")
} }
// TODO maybe accept flags to avoid prompt and allow programmatic use? // TODO maybe accept flags to avoid prompt and allow programmatic use?
// TODO accept existing pubkey // TODO accept existing pubkey
conf := MustLoadDsnetConfig() conf := MustLoadDsnetConfig()
hostname := os.Args[2]
owner := MustPromptString("owner", true) owner := MustPromptString("owner", true)
description := MustPromptString("Description", true) description := MustPromptString("Description", true)
// publicKey := MustPromptString("PublicKey (optional)", false) // publicKey := MustPromptString("PublicKey (optional)", false)

View File

@ -12,7 +12,7 @@ import (
var ( var (
// Flags. // Flags.
outputType string hostname string
// Commands. // Commands.
rootCmd = &cobra.Command{} rootCmd = &cobra.Command{}
@ -30,7 +30,7 @@ var (
addCmd = &cobra.Command{ addCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
dsnet.Add() dsnet.Add(hostname)
}, },
Use: "add", Use: "add",
Short: "Add a new peer + sync", Short: "Add a new peer + sync",
@ -86,8 +86,11 @@ var (
) )
func main() { func main() {
rootCmd.PersistentFlags().String("output", "wg-quick", "config file format: vyatta/wg-quick/nixos") // Flags.
rootCmd.PersistentFlags().StringP("output", "o", "wg-quick", "config file format: vyatta/wg-quick/nixos")
addCmd.Flags().StringVarP(&hostname, "hostname", "n", "", "hostname of new peer")
// Environment variable handling.
viper.AutomaticEnv() viper.AutomaticEnv()
viper.SetEnvPrefix("DSNET") viper.SetEnvPrefix("DSNET")
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_")) viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
@ -96,6 +99,7 @@ func main() {
log.Fatal(err) log.Fatal(err)
} }
// Adds subcommands.
rootCmd.AddCommand(initCmd) rootCmd.AddCommand(initCmd)
rootCmd.AddCommand(addCmd) rootCmd.AddCommand(addCmd)
rootCmd.AddCommand(upCmd) rootCmd.AddCommand(upCmd)