mirror of
https://github.com/pomerium/pomerium.git
synced 2025-04-29 18:36:30 +02:00
* initial core-zero import implementation * Update /config/import openapi description and use PUT instead of POST * update import ui tests * Add 413 as a possible response for /config/import * Options/Settings type conversion tests and related bugfixes * Fixes for proto type conversion and tests * Update core-zero import client * Update core-zero import client * Update import api and environment detection * update go.mod * remove old testdata * Remove usage of deleted setting after merge * remove extra newline from --version output
71 lines
2.1 KiB
Go
71 lines
2.1 KiB
Go
package cmd
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
|
|
zero "github.com/pomerium/pomerium/internal/zero/api"
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
type zeroClientContextKeyType struct{}
|
|
|
|
var zeroClientContextKey zeroClientContextKeyType
|
|
|
|
func zeroClientFromContext(ctx context.Context) *zero.API {
|
|
return ctx.Value(zeroClientContextKey).(*zero.API)
|
|
}
|
|
|
|
func BuildRootCmd() *cobra.Command {
|
|
cmd := &cobra.Command{
|
|
Use: "zero",
|
|
Short: "Interact with the Pomerium Zero cloud service",
|
|
PersistentPreRunE: func(cmd *cobra.Command, _ []string) error {
|
|
configFlag := cmd.InheritedFlags().Lookup("config")
|
|
var configFile string
|
|
if configFlag != nil {
|
|
configFile = configFlag.Value.String()
|
|
}
|
|
|
|
if err := setupLogger(); err != nil {
|
|
return err
|
|
}
|
|
var token string
|
|
if tokenFlag := cmd.InheritedFlags().Lookup("token"); tokenFlag != nil && tokenFlag.Changed {
|
|
token = tokenFlag.Value.String()
|
|
} else {
|
|
token = getToken(configFile)
|
|
}
|
|
if token == "" {
|
|
return errors.New("no token provided")
|
|
}
|
|
|
|
var clusterAPIEndpoint string
|
|
if endpointFlag := cmd.InheritedFlags().Lookup("cluster-api-endpoint"); endpointFlag != nil && endpointFlag.Changed {
|
|
clusterAPIEndpoint = endpointFlag.Value.String()
|
|
} else {
|
|
clusterAPIEndpoint = getClusterAPIEndpoint()
|
|
}
|
|
|
|
client, err := zero.NewAPI(cmd.Context(),
|
|
zero.WithAPIToken(token),
|
|
zero.WithClusterAPIEndpoint(clusterAPIEndpoint),
|
|
zero.WithConnectAPIEndpoint(getConnectAPIEndpoint()),
|
|
zero.WithOTELEndpoint(getOTELAPIEndpoint()),
|
|
)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
cmd.SetContext(context.WithValue(cmd.Context(), zeroClientContextKey, client))
|
|
return nil
|
|
},
|
|
}
|
|
|
|
cmd.AddCommand(BuildImportCmd())
|
|
cmd.PersistentFlags().String("config", "", "Specify configuration file location")
|
|
cmd.PersistentFlags().String("token", "", "Pomerium Zero Token (default: $POMERIUM_ZERO_TOKEN)")
|
|
cmd.PersistentFlags().String("cluster-api-endpoint", "", "Pomerium Zero Cluster API Endpoint (default: $CLUSTER_API_ENDPOINT)")
|
|
cmd.PersistentFlags().Lookup("cluster-api-endpoint").Hidden = true
|
|
|
|
return cmd
|
|
}
|