github.com/neilgarb/delve@v1.9.2-nobreaks/pkg/terminal/config.go (about)

     1  package terminal
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"reflect"
     7  	"text/tabwriter"
     8  
     9  	"github.com/go-delve/delve/pkg/config"
    10  )
    11  
    12  func configureCmd(t *Term, ctx callContext, args string) error {
    13  	switch args {
    14  	case "-list":
    15  		return configureList(t)
    16  	case "-save":
    17  		return config.SaveConfig(t.conf)
    18  	case "":
    19  		return fmt.Errorf("wrong number of arguments to \"config\"")
    20  	default:
    21  		err := configureSet(t, args)
    22  		if err != nil {
    23  			return err
    24  		}
    25  		if t.client != nil { // only happens in tests
    26  			lcfg := t.loadConfig()
    27  			t.client.SetReturnValuesLoadConfig(&lcfg)
    28  		}
    29  		return nil
    30  	}
    31  }
    32  
    33  func configureList(t *Term) error {
    34  	w := new(tabwriter.Writer)
    35  	w.Init(os.Stdout, 0, 8, 1, ' ', 0)
    36  	config.ConfigureList(w, t.conf, "yaml")
    37  	return w.Flush()
    38  }
    39  
    40  func configureSet(t *Term, args string) error {
    41  	v := config.Split2PartsBySpace(args)
    42  
    43  	cfgname := v[0]
    44  	var rest string
    45  	if len(v) == 2 {
    46  		rest = v[1]
    47  	}
    48  
    49  	if cfgname == "alias" {
    50  		return configureSetAlias(t, rest)
    51  	}
    52  
    53  	field := config.ConfigureFindFieldByName(t.conf, cfgname, "yaml")
    54  	if !field.CanAddr() {
    55  		return fmt.Errorf("%q is not a configuration parameter", cfgname)
    56  	}
    57  
    58  	if field.Kind() == reflect.Slice && field.Type().Elem().Name() == "SubstitutePathRule" {
    59  		return configureSetSubstitutePath(t, rest)
    60  	}
    61  
    62  	return config.ConfigureSetSimple(rest, cfgname, field)
    63  }
    64  
    65  func configureSetSubstitutePath(t *Term, rest string) error {
    66  	argv := config.SplitQuotedFields(rest, '"')
    67  	switch len(argv) {
    68  	case 1: // delete substitute-path rule
    69  		for i := range t.conf.SubstitutePath {
    70  			if t.conf.SubstitutePath[i].From == argv[0] {
    71  				copy(t.conf.SubstitutePath[i:], t.conf.SubstitutePath[i+1:])
    72  				t.conf.SubstitutePath = t.conf.SubstitutePath[:len(t.conf.SubstitutePath)-1]
    73  				return nil
    74  			}
    75  		}
    76  		return fmt.Errorf("could not find rule for %q", argv[0])
    77  	case 2: // add substitute-path rule
    78  		for i := range t.conf.SubstitutePath {
    79  			if t.conf.SubstitutePath[i].From == argv[0] {
    80  				t.conf.SubstitutePath[i].To = argv[1]
    81  				return nil
    82  			}
    83  		}
    84  		t.conf.SubstitutePath = append(t.conf.SubstitutePath, config.SubstitutePathRule{From: argv[0], To: argv[1]})
    85  	default:
    86  		return fmt.Errorf("too many arguments to \"config substitute-path\"")
    87  	}
    88  	return nil
    89  }
    90  
    91  func configureSetAlias(t *Term, rest string) error {
    92  	argv := config.SplitQuotedFields(rest, '"')
    93  	switch len(argv) {
    94  	case 1: // delete alias rule
    95  		for k := range t.conf.Aliases {
    96  			v := t.conf.Aliases[k]
    97  			for i := range v {
    98  				if v[i] == argv[0] {
    99  					copy(v[i:], v[i+1:])
   100  					t.conf.Aliases[k] = v[:len(v)-1]
   101  				}
   102  			}
   103  		}
   104  	case 2: // add alias rule
   105  		alias, cmd := argv[1], argv[0]
   106  		if t.conf.Aliases == nil {
   107  			t.conf.Aliases = make(map[string][]string)
   108  		}
   109  		t.conf.Aliases[cmd] = append(t.conf.Aliases[cmd], alias)
   110  	}
   111  	t.cmds.Merge(t.conf.Aliases)
   112  	return nil
   113  }