|
| 1 | +// Copyright © 2024 Meroxa, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package root |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "fmt" |
| 20 | + "reflect" |
| 21 | + |
| 22 | + "github.com/conduitio/ecdysis" |
| 23 | +) |
| 24 | + |
| 25 | +var ( |
| 26 | + _ ecdysis.CommandWithExecute = (*ConfigCommand)(nil) |
| 27 | + _ ecdysis.CommandWithDocs = (*ConfigCommand)(nil) |
| 28 | + _ ecdysis.CommandWithFlags = (*ConfigCommand)(nil) |
| 29 | + _ ecdysis.CommandWithConfig = (*ConfigCommand)(nil) |
| 30 | +) |
| 31 | + |
| 32 | +type ConfigCommand struct { |
| 33 | + rootCmd *RootCommand |
| 34 | +} |
| 35 | + |
| 36 | +func (c *ConfigCommand) Config() ecdysis.Config { |
| 37 | + return c.rootCmd.Config() |
| 38 | +} |
| 39 | + |
| 40 | +func (c *ConfigCommand) Flags() []ecdysis.Flag { |
| 41 | + return c.rootCmd.Flags() |
| 42 | +} |
| 43 | + |
| 44 | +func (c *ConfigCommand) Docs() ecdysis.Docs { |
| 45 | + return ecdysis.Docs{ |
| 46 | + Short: "Shows the configuration to be used when running Conduit.", |
| 47 | + Long: `Conduit will run based on the default configuration jointly with a provided configuration file (optional), |
| 48 | +the set environment variables, and the flags used. This command will show the configuration that will be used.`, |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +func printStruct(v reflect.Value, parentPath string) { |
| 53 | + if v.Kind() == reflect.Ptr { |
| 54 | + v = v.Elem() |
| 55 | + } |
| 56 | + |
| 57 | + t := v.Type() |
| 58 | + for i := 0; i < v.NumField(); i++ { |
| 59 | + field := t.Field(i) |
| 60 | + fieldValue := v.Field(i) |
| 61 | + longName := field.Tag.Get("long") |
| 62 | + |
| 63 | + fullPath := longName |
| 64 | + if parentPath != "" && longName != "" { |
| 65 | + fullPath = parentPath + "." + longName |
| 66 | + } |
| 67 | + |
| 68 | + if fieldValue.Kind() == reflect.Struct || |
| 69 | + (fieldValue.Kind() == reflect.Ptr && !fieldValue.IsNil() && fieldValue.Elem().Kind() == reflect.Struct) { |
| 70 | + printStruct(fieldValue, fullPath) |
| 71 | + continue |
| 72 | + } |
| 73 | + |
| 74 | + if longName != "" { |
| 75 | + value := fmt.Sprintf("%v", fieldValue.Interface()) |
| 76 | + if value != "" { |
| 77 | + fmt.Printf("%s: %s\n", fullPath, value) |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +func (c *ConfigCommand) Usage() string { return "config" } |
| 84 | + |
| 85 | +func (c ConfigCommand) Execute(_ context.Context) error { |
| 86 | + printStruct(reflect.ValueOf(c.rootCmd.cfg), "") |
| 87 | + return nil |
| 88 | +} |
0 commit comments