1
0
mirror of https://github.com/go-micro/go-micro.git synced 2025-05-31 21:59:42 +02:00
2021-11-01 14:34:09 +00:00

48 lines
1023 B
Go

package describe
import (
"encoding/json"
"fmt"
"github.com/urfave/cli/v2"
mcli "go-micro.dev/v4/cmd/micro/cli"
"gopkg.in/yaml.v2"
)
// Service fetches information for a service from the registry and prints it in
// either JSON or YAML, depending on the format flag passed. Exits on error.
func Service(ctx *cli.Context) error {
args := ctx.Args().Slice()
if len(args) < 1 {
return cli.ShowSubcommandHelp(ctx)
}
if ctx.String("format") != "json" && ctx.String("format") != "yaml" {
return cli.ShowSubcommandHelp(ctx)
}
r := *mcli.DefaultOptions().Registry
srvs, err := r.GetService(args[0])
if err != nil {
return err
}
if len(srvs) == 0 {
return fmt.Errorf("service %s not found", args[0])
}
for _, srv := range srvs {
var b []byte
var err error
if ctx.String("format") == "json" {
b, err = json.MarshalIndent(srv, "", " ")
} else if ctx.String("format") == "yaml" {
b, err = yaml.Marshal(srv)
}
if err != nil {
return err
}
fmt.Println(string(b))
}
return nil
}