github.com/turbot/steampipe@v1.7.0-rc.0.0.20240517123944-7cef272d4458/pkg/steampipeconfig/options/terminal.go (about)

     1  package options
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"github.com/turbot/steampipe/pkg/constants"
     8  )
     9  
    10  type Terminal struct {
    11  	Output           *string `hcl:"output"`
    12  	Separator        *string `hcl:"separator"`
    13  	Header           *bool   `hcl:"header"`
    14  	Multi            *bool   `hcl:"multi"`
    15  	Timing           *bool   `hcl:"timing"`
    16  	SearchPath       *string `hcl:"search_path"`
    17  	SearchPathPrefix *string `hcl:"search_path_prefix"`
    18  	Watch            *bool   `hcl:"watch"`
    19  	AutoComplete     *bool   `hcl:"autocomplete"`
    20  }
    21  
    22  // ConfigMap creates a config map that can be merged with viper
    23  func (t *Terminal) ConfigMap() map[string]interface{} {
    24  	// only add keys which are non null
    25  	res := map[string]interface{}{}
    26  	if t.Output != nil {
    27  		res[constants.ArgOutput] = t.Output
    28  	}
    29  	if t.Separator != nil {
    30  		res[constants.ArgSeparator] = t.Separator
    31  	}
    32  	if t.Header != nil {
    33  		res[constants.ArgHeader] = t.Header
    34  	}
    35  	if t.Multi != nil {
    36  		res[constants.ArgMultiLine] = t.Multi
    37  	}
    38  	if t.Timing != nil {
    39  		res[constants.ArgTiming] = t.Timing
    40  	}
    41  	if t.SearchPath != nil {
    42  		// convert from string to array
    43  		res[constants.ArgSearchPath] = searchPathToArray(*t.SearchPath)
    44  	}
    45  	if t.SearchPathPrefix != nil {
    46  		// convert from string to array
    47  		res[constants.ArgSearchPathPrefix] = searchPathToArray(*t.SearchPathPrefix)
    48  	}
    49  	if t.Watch != nil {
    50  		res[constants.ArgWatch] = t.Watch
    51  	}
    52  	if t.AutoComplete != nil {
    53  		res[constants.ArgAutoComplete] = t.AutoComplete
    54  	}
    55  	return res
    56  }
    57  
    58  // merge other options over the top of this options object
    59  // i.e. if a property is set in otherOptions, it takes precedence
    60  func (t *Terminal) Merge(otherOptions Options) {
    61  	switch o := otherOptions.(type) {
    62  	case *Terminal:
    63  		if o.Output != nil {
    64  			t.Output = o.Output
    65  		}
    66  		if o.Separator != nil {
    67  			t.Separator = o.Separator
    68  		}
    69  		if o.Header != nil {
    70  			t.Header = o.Header
    71  		}
    72  		if o.Multi != nil {
    73  			t.Multi = o.Multi
    74  		}
    75  		if o.Timing != nil {
    76  			t.Timing = o.Timing
    77  		}
    78  		if o.SearchPath != nil {
    79  			t.SearchPath = o.SearchPath
    80  		}
    81  		if o.SearchPathPrefix != nil {
    82  			t.SearchPathPrefix = o.SearchPathPrefix
    83  		}
    84  		if o.Watch != nil {
    85  			t.Watch = o.Watch
    86  		}
    87  		if o.AutoComplete != nil {
    88  			t.AutoComplete = o.AutoComplete
    89  		}
    90  	}
    91  }
    92  
    93  func (t *Terminal) String() string {
    94  	if t == nil {
    95  		return ""
    96  	}
    97  	var str []string
    98  	if t.Output == nil {
    99  		str = append(str, "  LogLevel: nil")
   100  	} else {
   101  		str = append(str, fmt.Sprintf("  Output: %s", *t.Output))
   102  	}
   103  	if t.Separator == nil {
   104  		str = append(str, "  Separator: nil")
   105  	} else {
   106  		str = append(str, fmt.Sprintf("  Separator: %s", *t.Separator))
   107  	}
   108  	if t.Header == nil {
   109  		str = append(str, "  Header: nil")
   110  	} else {
   111  		str = append(str, fmt.Sprintf("  Header: %v", *t.Header))
   112  	}
   113  	if t.Multi == nil {
   114  		str = append(str, "  Multi: nil")
   115  	} else {
   116  		str = append(str, fmt.Sprintf("  Multi: %v", *t.Multi))
   117  	}
   118  	if t.Timing == nil {
   119  		str = append(str, "  Timing: nil")
   120  	} else {
   121  		str = append(str, fmt.Sprintf("  Timing: %v", *t.Timing))
   122  	}
   123  	if t.SearchPath == nil {
   124  		str = append(str, "  SearchPath: nil")
   125  	} else {
   126  		str = append(str, fmt.Sprintf("  SearchPath: %s", *t.SearchPath))
   127  	}
   128  	if t.SearchPathPrefix == nil {
   129  		str = append(str, "  SearchPathPrefix: nil")
   130  	} else {
   131  		str = append(str, fmt.Sprintf("  SearchPathPrefix: %s", *t.SearchPathPrefix))
   132  	}
   133  	if t.Watch == nil {
   134  		str = append(str, "  Watch: nil")
   135  	} else {
   136  		str = append(str, fmt.Sprintf("  Watch: %v", *t.Watch))
   137  	}
   138  	if t.AutoComplete == nil {
   139  		str = append(str, "  AutoComplete: nil")
   140  	} else {
   141  		str = append(str, fmt.Sprintf("  AutoComplete: %v", *t.AutoComplete))
   142  	}
   143  	return strings.Join(str, "\n")
   144  }
   145  
   146  func searchPathToArray(searchPathString string) []string {
   147  	// convert comma separated list to array
   148  	searchPath := strings.Split(searchPathString, ",")
   149  	// strip whitespace
   150  	for i, s := range searchPath {
   151  		searchPath[i] = strings.TrimSpace(s)
   152  	}
   153  	return searchPath
   154  }