mirror of
				https://github.com/gravitl/netmaker.git
				synced 2025-10-31 04:06:37 +08:00 
			
		
		
		
	minor refactor
This commit is contained in:
		| @@ -1,20 +1,12 @@ | ||||
| package context | ||||
|  | ||||
| import ( | ||||
| 	"fmt" | ||||
| 	"log" | ||||
|  | ||||
| 	"github.com/gravitl/netmaker/cli/config" | ||||
| 	"github.com/spf13/cobra" | ||||
| ) | ||||
|  | ||||
| const ( | ||||
| 	FlagEndpoint  = "endpoint" | ||||
| 	FlagUsername  = "username" | ||||
| 	FlagPassword  = "password" | ||||
| 	FlagMasterKey = "master_key" | ||||
| ) | ||||
|  | ||||
| var ( | ||||
| 	endpoint  string | ||||
| 	username  string | ||||
| @@ -24,7 +16,7 @@ var ( | ||||
|  | ||||
| // contextSetCmd creates/updates a context | ||||
| var contextSetCmd = &cobra.Command{ | ||||
| 	Use:   fmt.Sprintf("set [NAME] [--%s=https://api.netmaker.io] [--%s=admin] [--%s=pass] [--%s=secret]", FlagEndpoint, FlagUsername, FlagPassword, FlagMasterKey), | ||||
| 	Use:   "set [NAME]", | ||||
| 	Args:  cobra.ExactArgs(1), | ||||
| 	Short: "Create a context or update an existing one", | ||||
| 	Long:  `Create a context or update an existing one`, | ||||
| @@ -44,11 +36,11 @@ var contextSetCmd = &cobra.Command{ | ||||
| } | ||||
|  | ||||
| func init() { | ||||
| 	contextSetCmd.Flags().StringVar(&endpoint, FlagEndpoint, "", "Endpoint of the API Server") | ||||
| 	contextSetCmd.Flags().StringVar(&username, FlagUsername, "", "Username") | ||||
| 	contextSetCmd.Flags().StringVar(&password, FlagPassword, "", "Password") | ||||
| 	contextSetCmd.MarkFlagsRequiredTogether(FlagUsername, FlagPassword) | ||||
| 	contextSetCmd.Flags().StringVar(&masterKey, FlagMasterKey, "", "Master Key") | ||||
| 	contextSetCmd.Flags().StringVar(&endpoint, "endpoint", "", "Endpoint of the API Server") | ||||
| 	contextSetCmd.Flags().StringVar(&username, "username", "", "Username") | ||||
| 	contextSetCmd.Flags().StringVar(&password, "password", "", "Password") | ||||
| 	contextSetCmd.MarkFlagsRequiredTogether("username", "password") | ||||
| 	contextSetCmd.Flags().StringVar(&masterKey, "master_key", "", "Master Key") | ||||
|  | ||||
| 	rootCmd.AddCommand(contextSetCmd) | ||||
| } | ||||
|   | ||||
							
								
								
									
										33
									
								
								cli/cmd/dns/create.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										33
									
								
								cli/cmd/dns/create.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,33 @@ | ||||
| package dns | ||||
|  | ||||
| import ( | ||||
| 	"log" | ||||
|  | ||||
| 	"github.com/gravitl/netmaker/cli/functions" | ||||
| 	"github.com/gravitl/netmaker/models" | ||||
| 	"github.com/spf13/cobra" | ||||
| ) | ||||
|  | ||||
| var dnsCreateCmd = &cobra.Command{ | ||||
| 	Use:   "create", | ||||
| 	Args:  cobra.NoArgs, | ||||
| 	Short: "Create a DNS entry", | ||||
| 	Long:  `Create a DNS entry`, | ||||
| 	Run: func(cmd *cobra.Command, args []string) { | ||||
| 		if address == "" && address6 == "" { | ||||
| 			log.Fatal("Either IPv4 or IPv6 address is required") | ||||
| 		} | ||||
| 		dnsEntry := &models.DNSEntry{Name: dnsName, Address: address, Address6: address6, Network: networkName} | ||||
| 		functions.PrettyPrint(functions.CreateDNS(networkName, dnsEntry)) | ||||
| 	}, | ||||
| } | ||||
|  | ||||
| func init() { | ||||
| 	dnsCreateCmd.Flags().StringVar(&dnsName, "name", "", "Name of the DNS entry") | ||||
| 	dnsCreateCmd.MarkFlagRequired("name") | ||||
| 	dnsCreateCmd.Flags().StringVar(&networkName, "network", "", "Name of the Network") | ||||
| 	dnsCreateCmd.MarkFlagRequired("network") | ||||
| 	dnsCreateCmd.Flags().StringVar(&address, "ipv4_addr", "", "IPv4 Address") | ||||
| 	dnsCreateCmd.Flags().StringVar(&address6, "ipv6_addr", "", "IPv6 Address") | ||||
| 	rootCmd.AddCommand(dnsCreateCmd) | ||||
| } | ||||
							
								
								
									
										9
									
								
								cli/cmd/dns/flags.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										9
									
								
								cli/cmd/dns/flags.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,9 @@ | ||||
| package dns | ||||
|  | ||||
| var ( | ||||
| 	dnsName     string | ||||
| 	address     string | ||||
| 	address6    string | ||||
| 	networkName string | ||||
| 	dnsType     string | ||||
| ) | ||||
							
								
								
									
										37
									
								
								cli/cmd/dns/list.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										37
									
								
								cli/cmd/dns/list.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,37 @@ | ||||
| package dns | ||||
|  | ||||
| import ( | ||||
| 	"fmt" | ||||
|  | ||||
| 	"github.com/gravitl/netmaker/cli/functions" | ||||
| 	"github.com/spf13/cobra" | ||||
| ) | ||||
|  | ||||
| var dnsListCmd = &cobra.Command{ | ||||
| 	Use:   "list", | ||||
| 	Args:  cobra.NoArgs, | ||||
| 	Short: "List DNS entries", | ||||
| 	Long:  `List DNS entries`, | ||||
| 	Run: func(cmd *cobra.Command, args []string) { | ||||
| 		if networkName != "" { | ||||
| 			switch dnsType { | ||||
| 			case "node": | ||||
| 				functions.PrettyPrint(functions.GetNodeDNS(networkName)) | ||||
| 			case "custom": | ||||
| 				functions.PrettyPrint(functions.GetCustomDNS(networkName)) | ||||
| 			case "network", "": | ||||
| 				functions.PrettyPrint(functions.GetNetworkDNS(networkName)) | ||||
| 			default: | ||||
| 				fmt.Println("Invalid DNS type provided ", dnsType) | ||||
| 			} | ||||
| 		} else { | ||||
| 			functions.PrettyPrint(functions.GetDNS()) | ||||
| 		} | ||||
| 	}, | ||||
| } | ||||
|  | ||||
| func init() { | ||||
| 	dnsListCmd.Flags().StringVar(&networkName, "network", "", "Network name") | ||||
| 	dnsListCmd.Flags().StringVar(&dnsType, "type", "", "Type of DNS records to fetch ENUM(node, custom, network)") | ||||
| 	rootCmd.AddCommand(dnsListCmd) | ||||
| } | ||||
							
								
								
									
										37
									
								
								cli/cmd/dns/root.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										37
									
								
								cli/cmd/dns/root.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,37 @@ | ||||
| package dns | ||||
|  | ||||
| import ( | ||||
| 	"os" | ||||
|  | ||||
| 	"github.com/spf13/cobra" | ||||
| ) | ||||
|  | ||||
| // rootCmd represents the base command when called without any subcommands | ||||
| var rootCmd = &cobra.Command{ | ||||
| 	Use:   "dns", | ||||
| 	Short: "Manage DNS entries associated with a network", | ||||
| 	Long:  `Manage DNS entries associated with a network`, | ||||
| 	// Run: func(cmd *cobra.Command, args []string) { }, | ||||
| } | ||||
|  | ||||
| func GetRoot() *cobra.Command { | ||||
| 	return rootCmd | ||||
| } | ||||
|  | ||||
| // Execute adds all child commands to the root command and sets flags appropriately. | ||||
| // This is called by main.main(). It only needs to happen once to the rootCmd. | ||||
| func Execute() { | ||||
| 	err := rootCmd.Execute() | ||||
| 	if err != nil { | ||||
| 		os.Exit(1) | ||||
| 	} | ||||
| } | ||||
|  | ||||
| func init() { | ||||
| 	// Here you will define your flags and configuration settings. | ||||
| 	// Cobra supports persistent flags, which, if defined here, | ||||
| 	// will be global for your application. | ||||
| 	// Cobra also supports local flags, which will only run | ||||
| 	// when this action is called directly. | ||||
| 	rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle") | ||||
| } | ||||
| @@ -1,7 +1,6 @@ | ||||
| package keys | ||||
|  | ||||
| import ( | ||||
| 	"fmt" | ||||
| 	"log" | ||||
| 	"strconv" | ||||
|  | ||||
| @@ -10,12 +9,10 @@ import ( | ||||
| 	"github.com/spf13/cobra" | ||||
| ) | ||||
|  | ||||
| const FlagKeyName = "name" | ||||
|  | ||||
| var keyName string | ||||
|  | ||||
| var keysCreateCmd = &cobra.Command{ | ||||
| 	Use:   fmt.Sprintf("create [NETWORK NAME] [NUM USES] [--%s=test_key]", FlagKeyName), | ||||
| 	Use:   "create [NETWORK NAME] [NUM USES] [--name=test_key]", | ||||
| 	Args:  cobra.ExactArgs(2), | ||||
| 	Short: "Create an access key", | ||||
| 	Long:  `Create an access key`, | ||||
| @@ -33,6 +30,6 @@ var keysCreateCmd = &cobra.Command{ | ||||
| } | ||||
|  | ||||
| func init() { | ||||
| 	keysCreateCmd.Flags().StringVar(&keyName, FlagKeyName, "", "Name of the key") | ||||
| 	keysCreateCmd.Flags().StringVar(&keyName, "name", "", "Name of the key") | ||||
| 	rootCmd.AddCommand(keysCreateCmd) | ||||
| } | ||||
|   | ||||
| @@ -23,7 +23,7 @@ var ( | ||||
|  | ||||
| // networkCreateCmd represents the networkCreate command | ||||
| var networkCreateCmd = &cobra.Command{ | ||||
| 	Use:   "create [--flags]", | ||||
| 	Use:   "create", | ||||
| 	Short: "Create a Network", | ||||
| 	Long:  `Create a Network`, | ||||
| 	Args:  cobra.NoArgs, | ||||
|   | ||||
| @@ -8,11 +8,6 @@ import ( | ||||
| 	"github.com/spf13/cobra" | ||||
| ) | ||||
|  | ||||
| var ( | ||||
| 	networkInterface string | ||||
| 	natEnabled       bool | ||||
| ) | ||||
|  | ||||
| var nodeCreateEgressCmd = &cobra.Command{ | ||||
| 	Use:   "create_egress [NETWORK NAME] [NODE ID] [EGRESS GATEWAY ADDRESSES (comma separated)]", | ||||
| 	Args:  cobra.ExactArgs(3), | ||||
|   | ||||
| @@ -5,8 +5,6 @@ import ( | ||||
| 	"github.com/spf13/cobra" | ||||
| ) | ||||
|  | ||||
| var failover bool | ||||
|  | ||||
| var nodeCreateIngressCmd = &cobra.Command{ | ||||
| 	Use:   "create_ingress [NETWORK NAME] [NODE ID]", | ||||
| 	Args:  cobra.ExactArgs(2), | ||||
|   | ||||
							
								
								
									
										8
									
								
								cli/cmd/node/flags.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										8
									
								
								cli/cmd/node/flags.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,8 @@ | ||||
| package node | ||||
|  | ||||
| var ( | ||||
| 	networkInterface string | ||||
| 	natEnabled       bool | ||||
| 	failover         bool | ||||
| 	networkName      string | ||||
| ) | ||||
| @@ -5,8 +5,6 @@ import ( | ||||
| 	"github.com/spf13/cobra" | ||||
| ) | ||||
|  | ||||
| var networkName string | ||||
|  | ||||
| // nodeListCmd lists all nodes | ||||
| var nodeListCmd = &cobra.Command{ | ||||
| 	Use:   "list", | ||||
|   | ||||
| @@ -5,6 +5,7 @@ import ( | ||||
|  | ||||
| 	"github.com/gravitl/netmaker/cli/cmd/acl" | ||||
| 	"github.com/gravitl/netmaker/cli/cmd/context" | ||||
| 	"github.com/gravitl/netmaker/cli/cmd/dns" | ||||
| 	"github.com/gravitl/netmaker/cli/cmd/keys" | ||||
| 	"github.com/gravitl/netmaker/cli/cmd/network" | ||||
| 	"github.com/gravitl/netmaker/cli/cmd/node" | ||||
| @@ -51,4 +52,5 @@ func init() { | ||||
| 	rootCmd.AddCommand(keys.GetRoot()) | ||||
| 	rootCmd.AddCommand(acl.GetRoot()) | ||||
| 	rootCmd.AddCommand(node.GetRoot()) | ||||
| 	rootCmd.AddCommand(dns.GetRoot()) | ||||
| } | ||||
|   | ||||
							
								
								
									
										28
									
								
								cli/functions/dns.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										28
									
								
								cli/functions/dns.go
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,28 @@ | ||||
| package functions | ||||
|  | ||||
| import ( | ||||
| 	"fmt" | ||||
| 	"net/http" | ||||
|  | ||||
| 	"github.com/gravitl/netmaker/models" | ||||
| ) | ||||
|  | ||||
| func GetDNS() *[]models.DNSEntry { | ||||
| 	return request[[]models.DNSEntry](http.MethodGet, "/api/dns", nil) | ||||
| } | ||||
|  | ||||
| func GetNodeDNS(networkName string) *[]models.DNSEntry { | ||||
| 	return request[[]models.DNSEntry](http.MethodGet, fmt.Sprintf("/api/dns/adm/%s/nodes", networkName), nil) | ||||
| } | ||||
|  | ||||
| func GetCustomDNS(networkName string) *[]models.DNSEntry { | ||||
| 	return request[[]models.DNSEntry](http.MethodGet, fmt.Sprintf("/api/dns/adm/%s/custom", networkName), nil) | ||||
| } | ||||
|  | ||||
| func GetNetworkDNS(networkName string) *[]models.DNSEntry { | ||||
| 	return request[[]models.DNSEntry](http.MethodGet, "/api/dns/adm/"+networkName, nil) | ||||
| } | ||||
|  | ||||
| func CreateDNS(networkName string, payload *models.DNSEntry) *models.DNSEntry { | ||||
| 	return request[models.DNSEntry](http.MethodPost, "/api/dns/"+networkName, payload) | ||||
| } | ||||
| @@ -2,10 +2,8 @@ package main | ||||
|  | ||||
| import ( | ||||
| 	"github.com/gravitl/netmaker/cli/cmd" | ||||
| 	_ "github.com/gravitl/netmaker/cli/config" | ||||
| ) | ||||
|  | ||||
| func main() { | ||||
|  | ||||
| 	cmd.Execute() | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 Anish Mukherjee
					Anish Mukherjee