mirror of
https://github.com/juanfont/headscale.git
synced 2024-12-20 19:09:07 +01:00
Expand json output to support yaml, make more generic
This commit is contained in:
parent
319237910b
commit
cd34a5d6f3
@ -14,19 +14,16 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/juanfont/headscale"
|
"github.com/juanfont/headscale"
|
||||||
apiV1 "github.com/juanfont/headscale/gen/go/headscale/v1"
|
v1 "github.com/juanfont/headscale/gen/go/headscale/v1"
|
||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
"google.golang.org/grpc"
|
"google.golang.org/grpc"
|
||||||
|
"gopkg.in/yaml.v2"
|
||||||
"inet.af/netaddr"
|
"inet.af/netaddr"
|
||||||
"tailscale.com/tailcfg"
|
"tailscale.com/tailcfg"
|
||||||
"tailscale.com/types/dnstype"
|
"tailscale.com/types/dnstype"
|
||||||
)
|
)
|
||||||
|
|
||||||
type ErrorOutput struct {
|
|
||||||
Error string
|
|
||||||
}
|
|
||||||
|
|
||||||
func LoadConfig(path string) error {
|
func LoadConfig(path string) error {
|
||||||
viper.SetConfigName("config")
|
viper.SetConfigName("config")
|
||||||
if path == "" {
|
if path == "" {
|
||||||
@ -316,7 +313,7 @@ func getHeadscaleApp() (*headscale.Headscale, error) {
|
|||||||
return h, nil
|
return h, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func getHeadscaleGRPCClient(ctx context.Context) (apiV1.HeadscaleServiceClient, *grpc.ClientConn) {
|
func getHeadscaleGRPCClient(ctx context.Context) (v1.HeadscaleServiceClient, *grpc.ClientConn) {
|
||||||
grpcOptions := []grpc.DialOption{
|
grpcOptions := []grpc.DialOption{
|
||||||
grpc.WithBlock(),
|
grpc.WithBlock(),
|
||||||
}
|
}
|
||||||
@ -370,46 +367,49 @@ func getHeadscaleGRPCClient(ctx context.Context) (apiV1.HeadscaleServiceClient,
|
|||||||
log.Fatal().Err(err).Msgf("Could not connect: %v", err)
|
log.Fatal().Err(err).Msgf("Could not connect: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
client := apiV1.NewHeadscaleServiceClient(conn)
|
client := v1.NewHeadscaleServiceClient(conn)
|
||||||
|
|
||||||
return client, conn
|
return client, conn
|
||||||
}
|
}
|
||||||
|
|
||||||
func JsonOutput(result interface{}, errResult error, outputFormat string) {
|
func SuccessOutput(result interface{}, override string, outputFormat string) {
|
||||||
var j []byte
|
var j []byte
|
||||||
var err error
|
var err error
|
||||||
switch outputFormat {
|
switch outputFormat {
|
||||||
case "json":
|
case "json":
|
||||||
if errResult != nil {
|
j, err = json.MarshalIndent(result, "", "\t")
|
||||||
j, err = json.MarshalIndent(ErrorOutput{errResult.Error()}, "", "\t")
|
if err != nil {
|
||||||
if err != nil {
|
log.Fatal().Err(err)
|
||||||
log.Fatal().Err(err)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
j, err = json.MarshalIndent(result, "", "\t")
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal().Err(err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
case "json-line":
|
case "json-line":
|
||||||
if errResult != nil {
|
j, err = json.Marshal(result)
|
||||||
j, err = json.Marshal(ErrorOutput{errResult.Error()})
|
if err != nil {
|
||||||
if err != nil {
|
log.Fatal().Err(err)
|
||||||
log.Fatal().Err(err)
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
j, err = json.Marshal(result)
|
|
||||||
if err != nil {
|
|
||||||
log.Fatal().Err(err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
case "yaml":
|
||||||
|
j, err = yaml.Marshal(result)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal().Err(err)
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
fmt.Println(override)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Println(string(j))
|
fmt.Println(string(j))
|
||||||
}
|
}
|
||||||
|
|
||||||
func HasJsonOutputFlag() bool {
|
func ErrorOutput(errResult error, override string, outputFormat string) {
|
||||||
|
type errOutput struct {
|
||||||
|
Error string `json:"error"`
|
||||||
|
}
|
||||||
|
|
||||||
|
SuccessOutput(errOutput{errResult.Error()}, override, outputFormat)
|
||||||
|
}
|
||||||
|
|
||||||
|
func HasMachineOutputFlag() bool {
|
||||||
for _, arg := range os.Args {
|
for _, arg := range os.Args {
|
||||||
if arg == "json" || arg == "json-line" {
|
if arg == "json" || arg == "json-line" || arg == "yaml" {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user