44 "bytes"
55 "errors"
66 "fmt"
7+ "sort"
78
9+ "github.com/cli/cli/internal/ghinstance"
810 "gopkg.in/yaml.v3"
911)
1012
@@ -14,6 +16,8 @@ const defaultGitProtocol = "https"
1416type Config interface {
1517 Get (string , string ) (string , error )
1618 Set (string , string , string ) error
19+ UnsetHost (string )
20+ Hosts () ([]string , error )
1721 Aliases () (* AliasConfig , error )
1822 Write () error
1923}
@@ -29,7 +33,7 @@ type HostConfig struct {
2933
3034// This type implements a low-level get/set config that is backed by an in-memory tree of Yaml
3135// nodes. It allows us to interact with a yaml-based config programmatically, preserving any
32- // comments that were present when the yaml waas parsed.
36+ // comments that were present when the yaml was parsed.
3337type ConfigMap struct {
3438 Root * yaml.Node
3539}
@@ -236,6 +240,20 @@ func (c *fileConfig) Set(hostname, key, value string) error {
236240 }
237241}
238242
243+ func (c * fileConfig ) UnsetHost (hostname string ) {
244+ if hostname == "" {
245+ return
246+ }
247+
248+ hostsEntry , err := c .FindEntry ("hosts" )
249+ if err != nil {
250+ return
251+ }
252+
253+ cm := ConfigMap {hostsEntry .ValueNode }
254+ cm .RemoveEntry (hostname )
255+ }
256+
239257func (c * fileConfig ) configForHost (hostname string ) (* HostConfig , error ) {
240258 hosts , err := c .hostEntries ()
241259 if err != nil {
@@ -357,6 +375,23 @@ func (c *fileConfig) hostEntries() ([]*HostConfig, error) {
357375 return hostConfigs , nil
358376}
359377
378+ // Hosts returns a list of all known hostnames configred in hosts.yml
379+ func (c * fileConfig ) Hosts () ([]string , error ) {
380+ entries , err := c .hostEntries ()
381+ if err != nil {
382+ return nil , err
383+ }
384+
385+ hostnames := []string {}
386+ for _ , entry := range entries {
387+ hostnames = append (hostnames , entry .Host )
388+ }
389+
390+ sort .SliceStable (hostnames , func (i , j int ) bool { return hostnames [i ] == ghinstance .Default () })
391+
392+ return hostnames , nil
393+ }
394+
360395func (c * fileConfig ) makeConfigForHost (hostname string ) * HostConfig {
361396 hostRoot := & yaml.Node {Kind : yaml .MappingNode }
362397 hostCfg := & HostConfig {
0 commit comments