Skip to content

Commit

Permalink
Add comments
Browse files Browse the repository at this point in the history
  • Loading branch information
samcoe committed May 26, 2022
1 parent faad1a1 commit ae1157f
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
14 changes: 14 additions & 0 deletions pkg/auth/auth.go
@@ -1,3 +1,5 @@
// Package auth is a set of functions for retrieving authentication tokens
// and authenticated hosts.
package auth

import (
Expand All @@ -19,6 +21,10 @@ const (
hostsKey = "hosts"
)

// TokenForHost retrieves an authentication token and the source of
// that token for the specified host. The source can be either an
// environment variable or the configuration file.
// Returns blank strings if no applicable token is found.
func TokenForHost(host string) (string, string) {
cfg, _ := config.Read()
return tokenForHost(cfg, host)
Expand Down Expand Up @@ -51,6 +57,10 @@ func tokenForHost(cfg *config.Config, host string) (string, string) {
return "", ""
}

// KnownHosts retrieves a list of hosts that have corresponding
// authentication tokens, either from environment variables
// or the configuration file.
// Returns an empty string slice if no hosts are found.
func KnownHosts() []string {
cfg, _ := config.Read()
return knownHosts(cfg)
Expand All @@ -73,6 +83,10 @@ func knownHosts(cfg *config.Config) []string {
return hosts.ToSlice()
}

// DefaultHost retrieves an authenticated host and the source of host.
// The source can be either an environment variable or the
// configuration file.
// Returns "github.com", "default" if no viable host is found.
func DefaultHost() (string, string) {
cfg, _ := config.Read()
return defaultHost(cfg)
Expand Down
29 changes: 28 additions & 1 deletion pkg/config/config.go
@@ -1,5 +1,5 @@
// Package config is a set of types for interacting with the gh configuration files.
// Note: This package is intended for use only in `gh`, any other use cases are subject
// Note: This package is intended for use only in gh, any other use cases are subject
// to breakage and non-backwards compatible updates.
package config

Expand All @@ -22,10 +22,19 @@ const (
xdgStateHome = "XDG_STATE_HOME"
)

// Config is a in memory representation of the gh configuration files.
// It can be thought of as map where entries consist of a key that
// correspond to either a string value or a map value, allowing for
// multi-level maps.
type Config struct {
entries *yamlmap.Map
}

// Get a string value from a Config.
// The keys argument is a sequence of key values so that nested
// entries can be retrieved. A undefined string will be returned
// if trying to retrieve a key that corresponds to a map value.
// Returns "", KeyNotFoundError if any of the keys can not be found.
func Get(c *Config, keys []string) (string, error) {
m := c.entries
for _, key := range keys {
Expand All @@ -38,6 +47,10 @@ func Get(c *Config, keys []string) (string, error) {
return m.Value, nil
}

// Keys enumerates a Configs keys.
// The keys argument is a sequence of key values so that nested
// map values can be have their keys enumerated.
// Returns nil, KeyNotFoundError if any of the keys can not be found.
func Keys(c *Config, keys []string) ([]string, error) {
m := c.entries
for _, key := range keys {
Expand All @@ -50,6 +63,11 @@ func Keys(c *Config, keys []string) ([]string, error) {
return m.Keys(), nil
}

// Remove an entry from a Config.
// The keys argument is a sequence of key values so that nested
// entries can be removed. Removing an entry that has nested
// entries removes those also.
// Returns "", KeyNotFoundError if any of the keys can not be found.
func Remove(c *Config, keys []string) error {
m := c.entries
for i := 0; i < len(keys)-1; i++ {
Expand All @@ -67,6 +85,10 @@ func Remove(c *Config, keys []string) error {
return nil
}

// Set an string value in a Config.
// The keys argument is a sequence of key values so that nested
// entries can be set. If any of the keys do not exist they will
// be created.
func Set(c *Config, keys []string, value string) {
m := c.entries
for i := 0; i < len(keys)-1; i++ {
Expand All @@ -81,6 +103,8 @@ func Set(c *Config, keys []string, value string) {
m.SetEntry(keys[len(keys)-1], yamlmap.StringValue(value))
}

// Read gh configuration files from the local file system and
// return a Config.
func Read() (*Config, error) {
// TODO: Make global config singleton using sync.Once
// so as not to read from file every time.
Expand All @@ -98,6 +122,9 @@ func ReadFromString(str string) *Config {
return &Config{m}
}

// Write gh configuration files to the local file system.
// It will only write gh configuration files that have been modified
// since last being read.
func Write(config *Config) error {
hosts, err := config.entries.FindEntry("hosts")
if err == nil && hosts.IsModified() {
Expand Down

0 comments on commit ae1157f

Please sign in to comment.