github.com/cli/cli@v1.14.1-0.20210902173923-1af6a669e342/internal/config/alias_config.go (about)

     1  package config
     2  
     3  import (
     4  	"fmt"
     5  )
     6  
     7  type AliasConfig struct {
     8  	ConfigMap
     9  	Parent Config
    10  }
    11  
    12  func (a *AliasConfig) Get(alias string) (string, bool) {
    13  	if a.Empty() {
    14  		return "", false
    15  	}
    16  	value, _ := a.GetStringValue(alias)
    17  
    18  	return value, value != ""
    19  }
    20  
    21  func (a *AliasConfig) Add(alias, expansion string) error {
    22  	err := a.SetStringValue(alias, expansion)
    23  	if err != nil {
    24  		return fmt.Errorf("failed to update config: %w", err)
    25  	}
    26  
    27  	err = a.Parent.Write()
    28  	if err != nil {
    29  		return fmt.Errorf("failed to write config: %w", err)
    30  	}
    31  
    32  	return nil
    33  }
    34  
    35  func (a *AliasConfig) Delete(alias string) error {
    36  	a.RemoveEntry(alias)
    37  
    38  	err := a.Parent.Write()
    39  	if err != nil {
    40  		return fmt.Errorf("failed to write config: %w", err)
    41  	}
    42  
    43  	return nil
    44  }
    45  
    46  func (a *AliasConfig) All() map[string]string {
    47  	out := map[string]string{}
    48  
    49  	if a.Empty() {
    50  		return out
    51  	}
    52  
    53  	for i := 0; i < len(a.Root.Content)-1; i += 2 {
    54  		key := a.Root.Content[i].Value
    55  		value := a.Root.Content[i+1].Value
    56  		out[key] = value
    57  	}
    58  
    59  	return out
    60  }