1
|
|
|
package cmd |
2
|
|
|
|
3
|
|
|
import ( |
4
|
|
|
"fmt" |
5
|
|
|
"os" |
6
|
|
|
"strings" |
7
|
|
|
|
8
|
|
|
"github.com/gookit/color" |
9
|
|
|
"github.com/olekukonko/tablewriter" |
10
|
|
|
"github.com/spf13/cobra" |
11
|
|
|
"github.com/spf13/viper" |
12
|
|
|
|
13
|
|
|
"github.com/Permify/permify/internal/config" |
14
|
|
|
"github.com/Permify/permify/pkg/cmd/flags" |
15
|
|
|
) |
16
|
|
|
|
17
|
|
|
func NewConfigCommand() *cobra.Command { |
18
|
|
|
command := &cobra.Command{ |
19
|
|
|
Use: "config", |
20
|
|
|
Short: "inspect permify configuration and environment variables", |
21
|
|
|
RunE: conf(), |
22
|
|
|
Args: cobra.NoArgs, |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
conf := config.DefaultConfig() |
26
|
|
|
f := command.Flags() |
27
|
|
|
f.StringP("config", "c", "", "config file (default is $HOME/.permify.yaml)") |
28
|
|
|
f.Bool("http-enabled", conf.Server.HTTP.Enabled, "switch option for HTTP server") |
29
|
|
|
f.String("account-id", conf.AccountID, "account id") |
30
|
|
|
f.Int64("server-rate-limit", conf.Server.RateLimit, "the maximum number of requests the server should handle per second") |
31
|
|
|
f.String("grpc-port", conf.Server.GRPC.Port, "port that GRPC server run on") |
32
|
|
|
f.Bool("grpc-tls-enabled", conf.Server.GRPC.TLSConfig.Enabled, "switch option for GRPC tls server") |
33
|
|
|
f.String("grpc-tls-key-path", conf.Server.GRPC.TLSConfig.KeyPath, "GRPC tls key path") |
34
|
|
|
f.String("grpc-tls-cert-path", conf.Server.GRPC.TLSConfig.CertPath, "GRPC tls certificate path") |
35
|
|
|
f.String("http-port", conf.Server.HTTP.Port, "HTTP port address") |
36
|
|
|
f.Bool("http-tls-enabled", conf.Server.HTTP.TLSConfig.Enabled, "switch option for HTTP tls server") |
37
|
|
|
f.String("http-tls-key-path", conf.Server.HTTP.TLSConfig.KeyPath, "HTTP tls key path") |
38
|
|
|
f.String("http-tls-cert-path", conf.Server.HTTP.TLSConfig.CertPath, "HTTP tls certificate path") |
39
|
|
|
f.StringSlice("http-cors-allowed-origins", conf.Server.HTTP.CORSAllowedOrigins, "CORS allowed origins for http gateway") |
40
|
|
|
f.StringSlice("http-cors-allowed-headers", conf.Server.HTTP.CORSAllowedHeaders, "CORS allowed headers for http gateway") |
41
|
|
|
f.Bool("profiler-enabled", conf.Profiler.Enabled, "switch option for profiler") |
42
|
|
|
f.String("profiler-port", conf.Profiler.Port, "profiler port address") |
43
|
|
|
f.String("log-level", conf.Log.Level, "real time logs of authorization. Permify uses zerolog as a logger") |
44
|
|
|
f.String("log-output", conf.Log.Output, "logger output valid values json, text") |
45
|
|
|
f.Bool("authn-enabled", conf.Authn.Enabled, "enable server authentication") |
46
|
|
|
f.String("authn-method", conf.Authn.Method, "server authentication method") |
47
|
|
|
f.StringSlice("authn-preshared-keys", conf.Authn.Preshared.Keys, "preshared key/keys for server authentication") |
48
|
|
|
f.String("authn-oidc-issuer", conf.Authn.Oidc.Issuer, "issuer identifier of the OpenID Connect Provider") |
49
|
|
|
f.String("authn-oidc-audience", conf.Authn.Oidc.Audience, "intended audience of the OpenID Connect token") |
50
|
|
|
f.Duration("authn-oidc-refresh-interval", conf.Authn.Oidc.RefreshInterval, "refresh interval for the OpenID Connect configuration") |
51
|
|
|
f.StringSlice("authn-oidc-valid-methods", conf.Authn.Oidc.ValidMethods, "list of valid JWT signing methods for OpenID Connect") |
52
|
|
|
f.Bool("tracer-enabled", conf.Tracer.Enabled, "switch option for tracing") |
53
|
|
|
f.String("tracer-exporter", conf.Tracer.Exporter, "can be; jaeger, signoz, zipkin or otlp. (integrated tracing tools)") |
54
|
|
|
f.String("tracer-endpoint", conf.Tracer.Endpoint, "export uri for tracing data") |
55
|
|
|
f.Bool("tracer-insecure", conf.Tracer.Insecure, "use https or http for tracer data, only used for otlp exporter or signoz") |
56
|
|
|
f.String("tracer-urlpath", conf.Tracer.URLPath, "allow to set url path for otlp exporter") |
57
|
|
|
f.Bool("meter-enabled", conf.Meter.Enabled, "switch option for metric") |
58
|
|
|
f.String("meter-exporter", conf.Meter.Exporter, "can be; otlp. (integrated metric tools)") |
59
|
|
|
f.String("meter-endpoint", conf.Meter.Endpoint, "export uri for metric data") |
60
|
|
|
f.Bool("meter-insecure", conf.Meter.Insecure, "use https or http for metric data") |
61
|
|
|
f.String("meter-urlpath", conf.Meter.URLPath, "allow to set url path for otlp exporter") |
62
|
|
|
f.Bool("service-circuit-breaker", conf.Service.CircuitBreaker, "switch option for service circuit breaker") |
63
|
|
|
f.Bool("service-watch-enabled", conf.Service.Watch.Enabled, "switch option for watch service") |
64
|
|
|
f.Int64("service-schema-cache-number-of-counters", conf.Service.Schema.Cache.NumberOfCounters, "schema service cache number of counters") |
65
|
|
|
f.String("service-schema-cache-max-cost", conf.Service.Schema.Cache.MaxCost, "schema service cache max cost") |
66
|
|
|
f.Int("service-permission-bulk-limit", conf.Service.Permission.BulkLimit, "bulk operations limit") |
67
|
|
|
f.Int("service-permission-concurrency-limit", conf.Service.Permission.ConcurrencyLimit, "concurrency limit") |
68
|
|
|
f.Int64("service-permission-cache-number-of-counters", conf.Service.Permission.Cache.NumberOfCounters, "permission service cache number of counters") |
69
|
|
|
f.String("service-permission-cache-max-cost", conf.Service.Permission.Cache.MaxCost, "permission service cache max cost") |
70
|
|
|
f.String("database-engine", conf.Database.Engine, "data source. e.g. postgres, memory") |
71
|
|
|
f.String("database-uri", conf.Database.URI, "uri of your data source to store relation tuples and schema") |
72
|
|
|
f.Bool("database-auto-migrate", conf.Database.AutoMigrate, "auto migrate database tables") |
73
|
|
|
f.Int("database-max-open-connections", conf.Database.MaxOpenConnections, "maximum number of parallel connections that can be made to the database at any time") |
74
|
|
|
f.Int("database-max-idle-connections", conf.Database.MaxIdleConnections, "maximum number of idle connections that can be made to the database at any time") |
75
|
|
|
f.Duration("database-max-connection-lifetime", conf.Database.MaxConnectionLifetime, "maximum amount of time a connection may be reused") |
76
|
|
|
f.Duration("database-max-connection-idle-time", conf.Database.MaxConnectionIdleTime, "maximum amount of time a connection may be idle") |
77
|
|
|
f.Int("database-max-data-per-write", conf.Database.MaxDataPerWrite, "sets the maximum amount of data per write operation to the database") |
78
|
|
|
f.Int("database-max-retries", conf.Database.MaxRetries, "defines the maximum number of retries for database operations in case of failure") |
79
|
|
|
f.Int("database-watch-buffer-size", conf.Database.WatchBufferSize, "specifies the buffer size for database watch operations, impacting how many changes can be queued") |
80
|
|
|
f.Bool("database-garbage-collection-enabled", conf.Database.GarbageCollection.Enabled, "use database garbage collection for expired relationships and attributes") |
81
|
|
|
f.Duration("database-garbage-collection-interval", conf.Database.GarbageCollection.Interval, "interval for database garbage collection") |
82
|
|
|
f.Duration("database-garbage-collection-timeout", conf.Database.GarbageCollection.Timeout, "timeout for database garbage collection") |
83
|
|
|
f.Duration("database-garbage-collection-window", conf.Database.GarbageCollection.Window, "window for database garbage collection") |
84
|
|
|
f.Bool("distributed-enabled", conf.Distributed.Enabled, "enable distributed") |
85
|
|
|
f.String("distributed-address", conf.Distributed.Address, "distributed address") |
86
|
|
|
f.String("distributed-port", conf.Distributed.Port, "distributed port") |
87
|
|
|
|
88
|
|
|
command.PreRun = func(cmd *cobra.Command, args []string) { |
89
|
|
|
flags.RegisterServeFlags(f) |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return command |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
func conf() func(cmd *cobra.Command, args []string) error { |
96
|
|
|
return func(cmd *cobra.Command, args []string) error { |
97
|
|
|
var cfg *config.Config |
98
|
|
|
var err error |
99
|
|
|
cfgFile := viper.GetString("config.file") |
100
|
|
|
if cfgFile != "" { |
101
|
|
|
cfg, err = config.NewConfigWithFile(cfgFile) |
102
|
|
|
if err != nil { |
103
|
|
|
return fmt.Errorf("failed to create new config: %w", err) |
|
|
|
|
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
if err = viper.Unmarshal(cfg); err != nil { |
107
|
|
|
return fmt.Errorf("failed to unmarshal config: %w", err) |
|
|
|
|
108
|
|
|
} |
109
|
|
|
} else { |
110
|
|
|
// Load configuration |
111
|
|
|
cfg, err = config.NewConfig() |
112
|
|
|
if err != nil { |
113
|
|
|
return fmt.Errorf("failed to create new config: %w", err) |
|
|
|
|
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
if err = viper.Unmarshal(cfg); err != nil { |
117
|
|
|
return fmt.Errorf("failed to unmarshal config: %w", err) |
|
|
|
|
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
var data [][]string |
122
|
|
|
|
123
|
|
|
data = append(data, |
124
|
|
|
[]string{"account_id", cfg.AccountID, getKeyOrigin(cmd, "account-id", "PERMIFY_ACCOUNT_ID")}, |
125
|
|
|
// SERVER |
126
|
|
|
[]string{"server.rate_limit", fmt.Sprintf("%v", cfg.Server.RateLimit), getKeyOrigin(cmd, "server-rate-limit", "PERMIFY_RATE_LIMIT")}, |
127
|
|
|
[]string{"server.grpc.port", cfg.Server.GRPC.Port, getKeyOrigin(cmd, "grpc-port", "PERMIFY_GRPC_PORT")}, |
128
|
|
|
[]string{"server.grpc.tls.enabled", fmt.Sprintf("%v", cfg.Server.GRPC.TLSConfig.Enabled), getKeyOrigin(cmd, "grpc-tls-enabled", "PERMIFY_GRPC_TLS_ENABLED")}, |
129
|
|
|
[]string{"server.grpc.tls.cert", cfg.Server.GRPC.TLSConfig.CertPath, getKeyOrigin(cmd, "grpc-tls-cert-path", "PERMIFY_GRPC_TLS_CERT_PATH")}, |
130
|
|
|
[]string{"server.http.enabled", fmt.Sprintf("%v", cfg.Server.HTTP.Enabled), getKeyOrigin(cmd, "http-enabled", "PERMIFY_HTTP_ENABLED")}, |
131
|
|
|
[]string{"server.http.tls.enabled", fmt.Sprintf("%v", cfg.Server.HTTP.TLSConfig.Enabled), getKeyOrigin(cmd, "http-tls-enabled", "PERMIFY_HTTP_TLS_ENABLED")}, |
132
|
|
|
[]string{"server.http.tls.key", HideSecret(cfg.Server.HTTP.TLSConfig.KeyPath), getKeyOrigin(cmd, "http-tls-key-path", "PERMIFY_HTTP_TLS_KEY_PATH")}, |
133
|
|
|
[]string{"server.http.tls.cert", HideSecret(cfg.Server.HTTP.TLSConfig.CertPath), getKeyOrigin(cmd, "http-tls-cert-path", "PERMIFY_HTTP_TLS_CERT_PATH")}, |
134
|
|
|
[]string{"server.http.cors_allowed_origins", fmt.Sprintf("%v", cfg.Server.HTTP.CORSAllowedOrigins), getKeyOrigin(cmd, "http-cors-allowed-origins", "PERMIFY_HTTP_CORS_ALLOWED_ORIGINS")}, |
135
|
|
|
[]string{"server.http.cors_allowed_headers", fmt.Sprintf("%v", cfg.Server.HTTP.CORSAllowedHeaders), getKeyOrigin(cmd, "http-cors-allowed-headers", "PERMIFY_HTTP_CORS_ALLOWED_HEADERS")}, |
136
|
|
|
// PROFILER |
137
|
|
|
[]string{"profiler.enabled", fmt.Sprintf("%v", cfg.Profiler.Enabled), getKeyOrigin(cmd, "profiler-enabled", "PERMIFY_PROFILER_ENABLED")}, |
138
|
|
|
[]string{"profiler.port", cfg.Profiler.Port, getKeyOrigin(cmd, "profiler-port", "PERMIFY_PROFILER_PORT")}, |
139
|
|
|
// LOG |
140
|
|
|
[]string{"logger.level", cfg.Log.Level, getKeyOrigin(cmd, "log-level", "PERMIFY_LOG_LEVEL")}, |
141
|
|
|
[]string{"logger.output", cfg.Log.Level, getKeyOrigin(cmd, "log-output", "PERMIFY_LOG_OUTPUT")}, |
142
|
|
|
// AUTHN |
143
|
|
|
[]string{"authn.enabled", fmt.Sprintf("%v", cfg.Authn.Enabled), getKeyOrigin(cmd, "authn-enabled", "PERMIFY_AUTHN_ENABLED")}, |
144
|
|
|
[]string{"authn.method", cfg.Authn.Method, getKeyOrigin(cmd, "authn-method", "PERMIFY_AUTHN_METHOD")}, |
145
|
|
|
[]string{"authn.preshared.keys", fmt.Sprintf("%v", HideSecrets(cfg.Authn.Preshared.Keys...)), getKeyOrigin(cmd, "authn-preshared-keys", "PERMIFY_AUTHN_PRESHARED_KEYS")}, |
146
|
|
|
[]string{"authn.oidc.issuer", HideSecret(cfg.Authn.Oidc.Issuer), getKeyOrigin(cmd, "authn-oidc-issuer", "PERMIFY_AUTHN_OIDC_ISSUER")}, |
147
|
|
|
[]string{"authn.oidc.audience", HideSecret(cfg.Authn.Oidc.Audience), getKeyOrigin(cmd, "authn-oidc-audience", "PERMIFY_AUTHN_OIDC_AUDIENCE")}, |
148
|
|
|
[]string{"authn.oidc.valid_methods", fmt.Sprintf("%v", cfg.Authn.Oidc.ValidMethods), getKeyOrigin(cmd, "authn-oidc-valid-methods", "PERMIFY_AUTHN_OIDC_VALID_METHODS")}, |
149
|
|
|
[]string{"authn.oidc.refresh_interval", fmt.Sprintf("%v", cfg.Authn.Oidc.RefreshInterval), getKeyOrigin(cmd, "authn-oidc-refresh-interval", "PERMIFY_AUTHN_OIDC_REFRESH_INTERVAL")}, |
150
|
|
|
// TRACER |
151
|
|
|
[]string{"tracer.enabled", fmt.Sprintf("%v", cfg.Tracer.Enabled), getKeyOrigin(cmd, "tracer-enabled", "PERMIFY_TRACER_ENABLED")}, |
152
|
|
|
[]string{"tracer.exporter", cfg.Tracer.Exporter, getKeyOrigin(cmd, "tracer-exporter", "PERMIFY_TRACER_EXPORTER")}, |
153
|
|
|
[]string{"tracer.endpoint", HideSecret(cfg.Tracer.Exporter), getKeyOrigin(cmd, "tracer-endpoint", "PERMIFY_TRACER_ENDPOINT")}, |
154
|
|
|
[]string{"tracer.insecure", fmt.Sprintf("%v", cfg.Tracer.Insecure), getKeyOrigin(cmd, "tracer-insecure", "PERMIFY_TRACER_INSECURE")}, |
155
|
|
|
[]string{"tracer.urlpath", cfg.Tracer.URLPath, getKeyOrigin(cmd, "tracer-urlpath", "PERMIFY_TRACER_URL_PATH")}, |
156
|
|
|
// METER |
157
|
|
|
[]string{"meter.enabled", fmt.Sprintf("%v", cfg.Meter.Enabled), getKeyOrigin(cmd, "meter-enabled", "PERMIFY_METER_ENABLED")}, |
158
|
|
|
[]string{"meter.exporter", cfg.Meter.Exporter, getKeyOrigin(cmd, "meter-exporter", "PERMIFY_METER_EXPORTER")}, |
159
|
|
|
[]string{"meter.endpoint", HideSecret(cfg.Meter.Exporter), getKeyOrigin(cmd, "meter-endpoint", "PERMIFY_METER_ENDPOINT")}, |
160
|
|
|
[]string{"meter.insecure", fmt.Sprintf("%v", cfg.Meter.Insecure), getKeyOrigin(cmd, "meter-insecure", "PERMIFY_METER_INSECURE")}, |
161
|
|
|
[]string{"meter.urlpath", cfg.Meter.URLPath, getKeyOrigin(cmd, "meter-urlpath", "PERMIFY_METER_URL_PATH")}, |
162
|
|
|
// SERVICE |
163
|
|
|
[]string{"service.circuit_breaker", fmt.Sprintf("%v", cfg.Service.CircuitBreaker), getKeyOrigin(cmd, "service-circuit-breaker", "PERMIFY_SERVICE_CIRCUIT_BREAKER")}, |
164
|
|
|
[]string{"service.schema.cache.number_of_counters", fmt.Sprintf("%v", cfg.Service.Schema.Cache.NumberOfCounters), getKeyOrigin(cmd, "service-schema-cache-number-of-counters", "PERMIFY_SERVICE_WATCH_ENABLED")}, |
165
|
|
|
[]string{"service.schema.cache.max_cost", cfg.Service.Schema.Cache.MaxCost, getKeyOrigin(cmd, "service-schema-cache-max-cost", "PERMIFY_SERVICE_SCHEMA_CACHE_MAX_COST")}, |
166
|
|
|
[]string{"service.permission.bulk_limit", fmt.Sprintf("%v", cfg.Service.Permission.BulkLimit), getKeyOrigin(cmd, "service-permission-bulk-limit", "PERMIFY_SERVICE_PERMISSION_BULK_LIMIT")}, |
167
|
|
|
[]string{"service.permission.concurrency_limit", fmt.Sprintf("%v", cfg.Service.Permission.ConcurrencyLimit), getKeyOrigin(cmd, "service-permission-concurrency-limit", "PERMIFY_SERVICE_PERMISSION_CONCURRENCY_LIMIT")}, |
168
|
|
|
[]string{"service.permission.cache.number_of_counters", fmt.Sprintf("%v", cfg.Service.Permission.Cache.NumberOfCounters), getKeyOrigin(cmd, "service-permission-cache-number-of-counters", "PERMIFY_SERVICE_PERMISSION_CACHE_NUMBER_OF_COUNTERS")}, |
169
|
|
|
[]string{"service.permission.cache.max_cost", fmt.Sprintf("%v", cfg.Service.Permission.Cache.MaxCost), getKeyOrigin(cmd, "service-permission-cache-max-cost", "PERMIFY_SERVICE_PERMISSION_CACHE_MAX_COST")}, |
170
|
|
|
// DATABASE |
171
|
|
|
[]string{"database.engine", cfg.Database.Engine, getKeyOrigin(cmd, "database-engine", "PERMIFY_DATABASE_ENGINE")}, |
172
|
|
|
[]string{"database.uri", HideSecret(cfg.Database.URI), getKeyOrigin(cmd, "database-uri", "PERMIFY_DATABASE_URI")}, |
173
|
|
|
[]string{"database.auto_migrate", fmt.Sprintf("%v", cfg.Database.AutoMigrate), getKeyOrigin(cmd, "database-auto-migrate", "PERMIFY_DATABASE_AUTO_MIGRATE")}, |
174
|
|
|
[]string{"database.max_open_connections", fmt.Sprintf("%v", cfg.Database.MaxOpenConnections), getKeyOrigin(cmd, "database-max-open-connections", "PERMIFY_DATABASE_MAX_OPEN_CONNECTIONS")}, |
175
|
|
|
[]string{"database.max_idle_connections", fmt.Sprintf("%v", cfg.Database.MaxIdleConnections), getKeyOrigin(cmd, "database-max-idle-connections", "PERMIFY_DATABASE_MAX_IDLE_CONNECTIONS")}, |
176
|
|
|
[]string{"database.max_connection_lifetime", fmt.Sprintf("%v", cfg.Database.MaxConnectionLifetime), getKeyOrigin(cmd, "database-max-connection-lifetime", "PERMIFY_DATABASE_MAX_CONNECTION_LIFETIME")}, |
177
|
|
|
[]string{"database.max_connection_idle_time", fmt.Sprintf("%v", cfg.Database.MaxConnectionIdleTime), getKeyOrigin(cmd, "database-max-connection-idle-time", "PERMIFY_DATABASE_MAX_CONNECTION_IDLE_TIME")}, |
178
|
|
|
[]string{"database.max_data_per_write", fmt.Sprintf("%v", cfg.Database.MaxDataPerWrite), getKeyOrigin(cmd, "database-max-data-per-write", "PERMIFY_DATABASE_MAX_DATA_PER_WRITE")}, |
179
|
|
|
[]string{"database.max_retries", fmt.Sprintf("%v", cfg.Database.MaxRetries), getKeyOrigin(cmd, "database-max-retries", "PERMIFY_DATABASE_MAX_RETRIES")}, |
180
|
|
|
[]string{"database.watch_buffer_size", fmt.Sprintf("%v", cfg.Database.WatchBufferSize), getKeyOrigin(cmd, "database-watch-buffer-size", "PERMIFY_DATABASE_WATCH_BUFFER_SIZE")}, |
181
|
|
|
[]string{"database.garbage_collection.enabled", fmt.Sprintf("%v", cfg.Database.GarbageCollection.Enabled), getKeyOrigin(cmd, "database-garbage-collection-enabled", "PERMIFY_DATABASE_GARBAGE_COLLECTION_ENABLED")}, |
182
|
|
|
[]string{"database.garbage_collection.interval", fmt.Sprintf("%v", cfg.Database.GarbageCollection.Interval), getKeyOrigin(cmd, "database-garbage-collection-interval", "PERMIFY_DATABASE_GARBAGE_COLLECTION_INTERVAL")}, |
183
|
|
|
[]string{"database.garbage_collection.timeout", fmt.Sprintf("%v", cfg.Database.GarbageCollection.Timeout), getKeyOrigin(cmd, "database-garbage-collection-timeout", "PERMIFY_DATABASE_GARBAGE_COLLECTION_TIMEOUT")}, |
184
|
|
|
[]string{"database.garbage_collection.window", fmt.Sprintf("%v", cfg.Database.GarbageCollection.Window), getKeyOrigin(cmd, "database-garbage-collection-window", "PERMIFY_DATABASE_GARBAGE_COLLECTION_WINDOW")}, |
185
|
|
|
// DISTRIBUTED |
186
|
|
|
[]string{"distributed.enabled", fmt.Sprintf("%v", cfg.Distributed.Enabled), getKeyOrigin(cmd, "distributed-enabled", "PERMIFY_DISTRIBUTED_ENABLED")}, |
187
|
|
|
[]string{"distributed.address", cfg.Distributed.Address, getKeyOrigin(cmd, "distributed-address", "PERMIFY_DISTRIBUTED_ADDRESS")}, |
188
|
|
|
[]string{"distributed.port", cfg.Distributed.Port, getKeyOrigin(cmd, "distributed-port", "PERMIFY_DISTRIBUTED_PORT")}, |
189
|
|
|
) |
190
|
|
|
|
191
|
|
|
renderConfigTable(data) |
192
|
|
|
return nil |
193
|
|
|
} |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
// getKeyOrigin determines the source of a configuration value. |
197
|
|
|
// It checks whether a value was set via a command-line flag, an environment variable, or defaults to file. |
198
|
|
|
func getKeyOrigin(cmd *cobra.Command, flagKey, envKey string) string { |
199
|
|
|
// Check if the command-line flag (specified by flagKey) was explicitly set by the user. |
200
|
|
|
if cmd.Flags().Changed(flagKey) { |
201
|
|
|
// If the flag was set, return "FLAG" with light green color. |
202
|
|
|
return color.FgLightGreen.Render("FLAG") |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
// Check if the environment variable (specified by envKey) exists. |
206
|
|
|
_, exists := os.LookupEnv(envKey) |
207
|
|
|
if exists { |
208
|
|
|
// If the environment variable exists, return "ENV" with light blue color. |
209
|
|
|
return color.FgLightBlue.Render("ENV") |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
// If neither the command-line flag nor the environment variable was set, |
213
|
|
|
// assume the value came from a configuration file. |
214
|
|
|
return color.FgYellow.Render("FILE") |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
// renderConfigTable displays configuration data in a formatted table on the console. |
218
|
|
|
// It takes a 2D slice of strings where each inner slice represents a row in the table. |
219
|
|
|
func renderConfigTable(data [][]string) { |
220
|
|
|
// Create a new table writer object, writing to standard output. |
221
|
|
|
table := tablewriter.NewWriter(os.Stdout) |
222
|
|
|
|
223
|
|
|
// Set the headers of the table. Each header cell is a column title. |
224
|
|
|
table.SetHeader([]string{"Key", "Value", "Source"}) |
225
|
|
|
|
226
|
|
|
// Align the columns of the table: left-aligned for keys, centered for values and sources. |
227
|
|
|
table.SetColumnAlignment([]int{tablewriter.ALIGN_LEFT, tablewriter.ALIGN_CENTER, tablewriter.ALIGN_CENTER}) |
228
|
|
|
|
229
|
|
|
// Set the center separator character for the table, which appears between columns. |
230
|
|
|
table.SetCenterSeparator("|") |
231
|
|
|
|
232
|
|
|
// Loop through the data and add each row to the table. |
233
|
|
|
for _, v := range data { |
234
|
|
|
table.Append(v) |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
// Render the table to standard output, displaying it to the user. |
238
|
|
|
table.Render() |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
// HideSecret replaces all but the first and last characters of a string with asterisks |
242
|
|
|
func HideSecret(secret string) string { |
243
|
|
|
if len(secret) <= 2 { |
244
|
|
|
// If the secret is too short, just return asterisks |
245
|
|
|
return strings.Repeat("*", len(secret)) |
246
|
|
|
} |
247
|
|
|
// Keep first and last character visible; replace the rest with asterisks |
248
|
|
|
return string(secret[0]) + strings.Repeat("*", len(secret)-2) + string(secret[len(secret)-1]) |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
// HideSecrets obscures each string in a given list. |
252
|
|
|
func HideSecrets(secrets ...string) (rv []string) { |
253
|
|
|
// Convert each secret to its hidden version and collect them. |
254
|
|
|
for _, secret := range secrets { |
255
|
|
|
rv = append(rv, HideSecret(secret)) // Hide each secret. |
256
|
|
|
} |
257
|
|
|
return |
258
|
|
|
} |
259
|
|
|
|