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
func Add() {
if len(os.Args) != 3 {
func Add(hostname string) {
if hostname == "" {
// 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 accept existing pubkey
conf := MustLoadDsnetConfig()
hostname := os.Args[2]
owner := MustPromptString("owner", true)
description := MustPromptString("Description", true)
// publicKey := MustPromptString("PublicKey (optional)", false)

View File

@ -12,7 +12,7 @@ import (
var (
// Flags.
outputType string
hostname string
// Commands.
rootCmd = &cobra.Command{}
@ -30,7 +30,7 @@ var (
addCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
dsnet.Add()
dsnet.Add(hostname)
},
Use: "add",
Short: "Add a new peer + sync",
@ -86,8 +86,11 @@ var (
)
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.SetEnvPrefix("DSNET")
viper.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
@ -96,6 +99,7 @@ func main() {
log.Fatal(err)
}
// Adds subcommands.
rootCmd.AddCommand(initCmd)
rootCmd.AddCommand(addCmd)
rootCmd.AddCommand(upCmd)