github.phpd.cn/hashicorp/consul@v1.4.5/command/services/register/register.go (about) 1 package register 2 3 import ( 4 "flag" 5 "fmt" 6 7 "github.com/hashicorp/consul/api" 8 "github.com/hashicorp/consul/command/flags" 9 "github.com/hashicorp/consul/command/services" 10 "github.com/mitchellh/cli" 11 ) 12 13 func New(ui cli.Ui) *cmd { 14 c := &cmd{UI: ui} 15 c.init() 16 return c 17 } 18 19 type cmd struct { 20 UI cli.Ui 21 flags *flag.FlagSet 22 http *flags.HTTPFlags 23 help string 24 25 // flags 26 flagId string 27 flagName string 28 flagAddress string 29 flagPort int 30 flagTags []string 31 flagMeta map[string]string 32 } 33 34 func (c *cmd) init() { 35 c.flags = flag.NewFlagSet("", flag.ContinueOnError) 36 c.flags.StringVar(&c.flagId, "id", "", 37 "ID of the service to register for arg-based registration. If this "+ 38 "isn't set, it will default to the -name value.") 39 c.flags.StringVar(&c.flagName, "name", "", 40 "Name of the service to register for arg-based registration.") 41 c.flags.StringVar(&c.flagAddress, "address", "", 42 "Address of the service to register for arg-based registration.") 43 c.flags.IntVar(&c.flagPort, "port", 0, 44 "Port of the service to register for arg-based registration.") 45 c.flags.Var((*flags.FlagMapValue)(&c.flagMeta), "meta", 46 "Metadata to set on the intention, formatted as key=value. This flag "+ 47 "may be specified multiple times to set multiple meta fields.") 48 c.flags.Var((*flags.AppendSliceValue)(&c.flagTags), "tag", 49 "Tag to add to the service. This flag can be specified multiple "+ 50 "times to set multiple tags.") 51 52 c.http = &flags.HTTPFlags{} 53 flags.Merge(c.flags, c.http.ClientFlags()) 54 flags.Merge(c.flags, c.http.ServerFlags()) 55 c.help = flags.Usage(help, c.flags) 56 } 57 58 func (c *cmd) Run(args []string) int { 59 if err := c.flags.Parse(args); err != nil { 60 return 1 61 } 62 63 svcs := []*api.AgentServiceRegistration{&api.AgentServiceRegistration{ 64 ID: c.flagId, 65 Name: c.flagName, 66 Address: c.flagAddress, 67 Port: c.flagPort, 68 Tags: c.flagTags, 69 Meta: c.flagMeta, 70 }} 71 72 // Check for arg validation 73 args = c.flags.Args() 74 if len(args) == 0 && c.flagName == "" { 75 c.UI.Error("Service registration requires at least one argument or flags.") 76 return 1 77 } else if len(args) > 0 && c.flagName != "" { 78 c.UI.Error("Service registration requires arguments or -id, not both.") 79 return 1 80 } 81 82 if len(args) > 0 { 83 var err error 84 svcs, err = services.ServicesFromFiles(args) 85 if err != nil { 86 c.UI.Error(fmt.Sprintf("Error: %s", err)) 87 return 1 88 } 89 } 90 91 // Create and test the HTTP client 92 client, err := c.http.APIClient() 93 if err != nil { 94 c.UI.Error(fmt.Sprintf("Error connecting to Consul agent: %s", err)) 95 return 1 96 } 97 98 // Create all the services 99 for _, svc := range svcs { 100 if err := client.Agent().ServiceRegister(svc); err != nil { 101 c.UI.Error(fmt.Sprintf("Error registering service %q: %s", 102 svc.Name, err)) 103 return 1 104 } 105 106 c.UI.Output(fmt.Sprintf("Registered service: %s", svc.Name)) 107 } 108 109 return 0 110 } 111 112 func (c *cmd) Synopsis() string { 113 return synopsis 114 } 115 116 func (c *cmd) Help() string { 117 return c.help 118 } 119 120 const synopsis = "Register services with the local agent" 121 const help = ` 122 Usage: consul services register [options] [FILE...] 123 124 Register one or more services using the local agent API. Services can 125 be registered from standard Consul configuration files (HCL or JSON) or 126 using flags. The service is registered and the command returns. The caller 127 must remember to call "consul services deregister" or a similar API to 128 deregister the service when complete. 129 130 $ consul services register web.json 131 132 Additional flags and more advanced use cases are detailed below. 133 `