File: os_fetcher.go

package info (click to toggle)
git-lfs 3.6.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,808 kB
  • sloc: sh: 21,256; makefile: 507; ruby: 417
file content (68 lines) | stat: -rw-r--r-- 1,693 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package config

import (
	"os"
	"sync"
)

// OsFetcher is an implementation of the Fetcher type for communicating with
// the system's environment.
//
// It is safe to use across multiple goroutines.
type OsFetcher struct {
	// vmu guards read/write access to vals
	vmu sync.Mutex
	// vals maintains a local cache of the system's environment variables
	// for fast repeat lookups of a given key.
	vals map[string]*string
}

// NewOsFetcher returns a new *OsFetcher.
func NewOsFetcher() *OsFetcher {
	return &OsFetcher{
		vals: make(map[string]*string),
	}
}

// Get returns the value associated with the given key as stored in the local
// cache, or in the operating system's environment variables.
//
// If there was a cache-hit, the value will be returned from the cache, skipping
// a check against os.Getenv. Otherwise, the value will be fetched from the
// system, stored in the cache, and then returned. If no value was present in
// the cache or in the system, an empty string will be returned.
//
// Get is safe to call across multiple goroutines.
func (o *OsFetcher) Get(key string) (val string, ok bool) {
	o.vmu.Lock()
	defer o.vmu.Unlock()

	if i, ok := o.vals[key]; ok {
		if i == nil {
			return "", false
		}
		return *i, true
	}

	v, ok := os.LookupEnv(key)
	if ok {
		o.vals[key] = &v
	} else {
		o.vals[key] = nil
	}

	return v, ok
}

// GetAll implements the `config.Fetcher.GetAll` method by returning, at most, a
// 1-ary set containing the result of `config.OsFetcher.Get()`.
func (o *OsFetcher) GetAll(key string) []string {
	if v, ok := o.Get(key); ok {
		return []string{v}
	}
	return make([]string, 0)
}

func (o *OsFetcher) All() map[string][]string {
	return nil
}