@@ -84,9 +84,6 @@ func ensureConfig(configDir string) (bool, error) {
8484 if err := os .WriteFile (configFile , data , 0o600 ); err != nil {
8585 return false , err
8686 }
87- if err := tightenFilePermissions (configFile , 0o600 ); err != nil {
88- fmt .Fprintf (os .Stderr , "warning: unable to tighten config permissions for %s: %v\n " , configFile , err )
89- }
9087
9188 return true , nil
9289}
@@ -363,6 +360,15 @@ var apiExamples = []string{
363360 " dci query body.query:\" SELECT * FROM aws_cur_2_0 LIMIT 10\" " ,
364361}
365362
363+ func findDCICommand () * cobra.Command {
364+ for _ , cmd := range cli .Root .Commands () {
365+ if cmd .Name () == "dci" {
366+ return cmd
367+ }
368+ }
369+ return nil
370+ }
371+
366372func customizeDCIUsage () {
367373 cobra .AddTemplateFunc ("hasVisibleCommandsInGroup" , func (cmds []* cobra.Command , groupID string ) bool {
368374 for _ , cmd := range cmds {
@@ -373,13 +379,7 @@ func customizeDCIUsage() {
373379 return false
374380 })
375381
376- var dciCmd * cobra.Command
377- for _ , cmd := range cli .Root .Commands () {
378- if cmd .Name () == "dci" {
379- dciCmd = cmd
380- break
381- }
382- }
382+ dciCmd := findDCICommand ()
383383 if dciCmd == nil {
384384 return
385385 }
@@ -489,13 +489,7 @@ func registerCustomerContextCommands(configDir string) {
489489}
490490
491491func brandDCIRootCommand () {
492- for _ , cmd := range cli .Root .Commands () {
493- if cmd .Name () != "dci" {
494- continue
495- }
496- applyCommandBranding (cmd , "DoiT Cloud Intelligence API CLI" , apiExamples )
497- return
498- }
492+ applyCommandBranding (findDCICommand (), "DoiT Cloud Intelligence API CLI" , apiExamples )
499493}
500494
501495func registerStatusCommands (configDir string ) {
@@ -631,13 +625,7 @@ func applyCustomerContext(configDir string) {
631625}
632626
633627func addOutputFlag () {
634- var dciCmd * cobra.Command
635- for _ , cmd := range cli .Root .Commands () {
636- if cmd .Name () == "dci" {
637- dciCmd = cmd
638- break
639- }
640- }
628+ dciCmd := findDCICommand ()
641629 if dciCmd == nil {
642630 return
643631 }
@@ -660,21 +648,16 @@ func addOutputFlag() {
660648 outFlag := cmd .Flags ().Lookup ("output" )
661649 if outFlag == nil || ! outFlag .Changed {
662650 viper .Set ("rsh-output-format" , "table" )
663- if err := defaultToBodyOutput (); err != nil {
664- return err
665- }
666651 } else {
667652 out := strings .TrimSpace (outFlag .Value .String ())
668653 switch out {
669654 case "table" , "json" , "yaml" , "auto" :
670655 viper .Set ("rsh-output-format" , out )
671- if err := defaultToBodyOutput (); err != nil {
672- return err
673- }
674656 default :
675657 return fmt .Errorf ("invalid --output %q (supported: table, json, yaml, auto)" , out )
676658 }
677659 }
660+ defaultToBodyOutput ()
678661
679662 if flag := cmd .Flags ().Lookup ("table-mode" ); flag != nil {
680663 v := strings .TrimSpace (flag .Value .String ())
@@ -687,37 +670,31 @@ func addOutputFlag() {
687670 v := strings .TrimSpace (flag .Value .String ())
688671 viper .Set ("table-columns" , v )
689672 }
690- if flag := cmd .Flags ().Lookup ("table-width" ); flag != nil {
691- width , _ := strconv .Atoi (flag .Value .String ())
692- if width < 0 {
693- width = 0
694- }
695- viper .Set ("table-width" , width )
696- }
697- if flag := cmd .Flags ().Lookup ("table-max-col-width" ); flag != nil {
698- maxw , _ := strconv .Atoi (flag .Value .String ())
699- if maxw < 0 {
700- maxw = 0
701- }
702- viper .Set ("table-max-col-width" , maxw )
703- }
673+ bindNonNegativeIntFlag (cmd , "table-width" )
674+ bindNonNegativeIntFlag (cmd , "table-max-col-width" )
704675
705676 return nil
706677 }
707678}
708679
709- func defaultToBodyOutput () error {
680+ func bindNonNegativeIntFlag (cmd * cobra.Command , name string ) {
681+ if flag := cmd .Flags ().Lookup (name ); flag != nil {
682+ v , _ := strconv .Atoi (flag .Value .String ())
683+ if v < 0 {
684+ v = 0
685+ }
686+ viper .Set (name , v )
687+ }
688+ }
689+
690+ func defaultToBodyOutput () {
710691 // By default restish prints response status + headers for TTY output when no
711692 // filter is specified. This CLI is primarily focused on the response body,
712693 // so default to `body` unless the user explicitly requested raw output or a
713694 // filter was already set.
714- if viper .GetBool ("rsh-raw" ) {
715- return nil
716- }
717- if viper .GetString ("rsh-filter" ) == "" {
695+ if ! viper .GetBool ("rsh-raw" ) && viper .GetString ("rsh-filter" ) == "" {
718696 viper .Set ("rsh-filter" , "body" )
719697 }
720- return nil
721698}
722699
723700type dciTableContentType struct {}
@@ -1000,8 +977,6 @@ func tableDisplayWidth(s string) int {
1000977 return max
1001978}
1002979
1003- var tableOverheadCache = map [int ]int {}
1004-
1005980func computeColumnWidths (cols int , terminalWidth int , maxColWidth int ) []int {
1006981 if cols <= 0 {
1007982 return nil
@@ -1039,29 +1014,8 @@ func tableOverhead(cols int) int {
10391014 if cols <= 0 {
10401015 return 0
10411016 }
1042- if v , ok := tableOverheadCache [cols ]; ok {
1043- return v
1044- }
1045-
1046- keys := make ([]string , cols )
1047- rows := []map [string ]interface {}{{}}
1048- for i := 0 ; i < cols ; i ++ {
1049- keys [i ] = fmt .Sprintf ("c%d" , i )
1050- rows [0 ][keys [i ]] = "a"
1051- }
1052-
1053- widths := make ([]int , cols )
1054- for i := 0 ; i < cols ; i ++ {
1055- widths [i ] = 1
1056- }
1057-
1058- s , _ := buildTableString (rows , keys , widths , "fit" , 0 )
1059- overhead := tableDisplayWidth (s ) - cols
1060- if overhead < 0 {
1061- overhead = 0
1062- }
1063- tableOverheadCache [cols ] = overhead
1064- return overhead
1017+ // simpletable StyleUnicode: 2 outer borders + 1 left pad + 2 separator per column = 1 + 4*cols
1018+ return 1 + 4 * cols
10651019}
10661020
10671021func buildTableString (rows []map [string ]interface {}, keys []string , colWidths []int , mode string , targetWidth int ) (string , error ) {
@@ -1146,13 +1100,6 @@ func padCell(s string, width int) string {
11461100 return s + strings .Repeat (" " , width - cur )
11471101}
11481102
1149- func max (a , b int ) int {
1150- if a > b {
1151- return a
1152- }
1153- return b
1154- }
1155-
11561103func collectKeys (rows []map [string ]interface {}, preferred []string ) []string {
11571104 if len (preferred ) > 0 {
11581105 return preferred
0 commit comments