Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 68b2ec93cb | |||
| f52de3c602 | |||
| ca02aa1da6 | |||
| 88a86bfab1 | |||
| 282e145791 | |||
| f2f8f33335 |
@@ -1 +1,2 @@
|
||||
getenv
|
||||
dist/*
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
VERSION=$(shell git describe)
|
||||
DIST=dist
|
||||
|
||||
all: linux-amd64
|
||||
|
||||
linux-amd64:
|
||||
GOOS=linux GOARCH=amd64 go build -o $(DIST)/amd64/getenv
|
||||
zip $(DIST)/getenv-$(VERSION)-linux-amd64.zip $(DIST)/amd64/getenv
|
||||
@@ -1,9 +1,9 @@
|
||||
## What's this?
|
||||
`getenv` will fetch GCE metadata attributes and print them as K=V pairs ready to be `exported` as environment variables.
|
||||
`getenv` will fetch Google Cloud (Compute Engine) metadata attributes and print them as K=V pairs ready to be `exported` as environment variables.
|
||||
|
||||
Example:
|
||||
```bash
|
||||
./getenv
|
||||
getenv
|
||||
MY_VARIABLE=SOMEVALUE
|
||||
```
|
||||
|
||||
@@ -11,11 +11,11 @@ MY_VARIABLE=SOMEVALUE
|
||||
|
||||
Export all metadata attributes as ENV variables for current shell session and store them in /etc/environment for future sessions:
|
||||
```bash
|
||||
export $(./getenv)
|
||||
./getenv 1>> /etc/environment
|
||||
export $(getenv)
|
||||
getenv 1>> /etc/environment
|
||||
```
|
||||
or
|
||||
```bash
|
||||
./getenv 1>> /etc/environment
|
||||
getenv 1>> /etc/environment
|
||||
source /etc/environment
|
||||
```
|
||||
@@ -11,7 +11,8 @@ import (
|
||||
)
|
||||
|
||||
func main() {
|
||||
prefix := flag.String("prefix", "", "Prefix to append to the ENV var names in the output")
|
||||
prefix := flag.String("prefix", "", "Only fetch metadata items that match this prefix")
|
||||
prepend := flag.String("prepend", "", "Prepend this string to the ENV var names in the output")
|
||||
flag.Parse()
|
||||
|
||||
var client = &http.Client{
|
||||
@@ -47,12 +48,14 @@ func main() {
|
||||
}
|
||||
|
||||
for k, v := range items {
|
||||
vars[strings.ToUpper(k)] = v
|
||||
if strings.HasPrefix(k, *prefix) {
|
||||
vars[strings.ToUpper(k)] = v
|
||||
}
|
||||
}
|
||||
|
||||
for k, v := range vars {
|
||||
if *prefix != "" {
|
||||
fmt.Printf("%s_%s=%s\n", *prefix, k, v)
|
||||
if *prepend != "" {
|
||||
fmt.Printf("%s_%s=%s\n", strings.ToUpper(*prepend), k, v)
|
||||
} else {
|
||||
fmt.Printf("%s=%s\n", k, v)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user