~samwhited/blogsync

ref: 5083f77d6a5a04a6c2e8f065c87351f48870505b blogsync/env.go -rw-r--r-- 641 bytes
5083f77dSam Whited .builds: verify DCO 1 year, 7 months ago
                                                                                
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
// Copyright 2019 The Blog Sync Contributors.
// Use of this source code is governed by the BSD 2-clause
// license that can be found in the LICENSE file.

package main

import (
	"os"
	"strconv"
	"time"
)

func timeOrDef(val, def time.Time) time.Time {
	if val.IsZero() {
		return def
	}
	return val
}

func orDef(val, def string) string {
	if val == "" {
		return def
	}
	return val
}

func envOrDef(key, def string) string {
	e := os.Getenv(key)
	if e == "" {
		return def
	}
	return e
}

func intEnv(key string) int {
	e := os.Getenv(key)
	if e == "" {
		return 0
	}
	i, err := strconv.Atoi(e)
	if err != nil {
		return 0
	}
	return i
}