Skip to content

Commit f0b072d

Browse files
committed
v2.0.1
1 parent 3e3d112 commit f0b072d

8 files changed

Lines changed: 305 additions & 193 deletions

File tree

env.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"log"
66
"os"
7+
"os/user"
78
"path/filepath"
89
"reflect"
910
"runtime"
@@ -45,6 +46,7 @@ type Options struct {
4546
Silent bool // silence log configuration output
4647
NoHelp bool // silence help output
4748
SetENV bool // set KEY=VALUE in environment
49+
NoExit bool // return nil instead of os.Exit(0) for version,help
4850
}
4951

5052
// Configure sets up the basic environment and returns environment paths;
@@ -108,6 +110,9 @@ func Configure(cfg ...interface{}) (path *Path) {
108110

109111
fmt.Printf("\n %-s\n%s\n version %s\n build %s\n\n",
110112
name, strings.Repeat("-", n+2), Version, Build)
113+
if opt.NoExit {
114+
return nil
115+
}
111116
os.Exit(0)
112117

113118
case "help":
@@ -174,6 +179,9 @@ func Configure(cfg ...interface{}) (path *Path) {
174179
}
175180
}
176181
fmt.Println()
182+
if opt.NoExit {
183+
return nil
184+
}
177185
os.Exit(0)
178186
}
179187
}
@@ -184,12 +192,14 @@ func Configure(cfg ...interface{}) (path *Path) {
184192

185193
if !opt.Silent {
186194

195+
usr, _ := user.Current()
196+
187197
log.Printf("|%s|", strings.Repeat("-", 40))
188198
log.Printf("| %s %s event log |", strings.ToUpper(filepath.Base(os.Args[0])), strings.Repeat(":", 27-len(filepath.Base(os.Args[0]))))
189199
log.Printf("|-----//o%s|", strings.Repeat("-", 32))
190200
log.Printf("%s%s version", strings.Repeat(" ", 31-len(Version)), Version)
191201
log.Printf("%s%s build", strings.Repeat(" ", 31-len(Build)), Build)
192-
log.Printf("%spid %d", strings.Repeat(" ", 28), os.Getpid())
202+
log.Printf(" %s%spid %d", usr.Username, strings.Repeat(" ", 27-len(usr.Username)), os.Getpid())
193203
log.Printf("|-----//o%s|", strings.Repeat("-", 32))
194204

195205
var tag string

env_test.go

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
package env_test
2+
3+
import (
4+
"context"
5+
"log"
6+
"os"
7+
"sync"
8+
"testing"
9+
10+
"github.com/zxdev/env"
11+
)
12+
13+
func TestEnv(t *testing.T) {
14+
15+
type Action struct {
16+
Action string `env:"a,order,require" default:"pull" help:"action chain[@path pull|process|expire|export]"`
17+
Secret string `env:"hidden" help:"the shared secret"`
18+
Show bool `default:"on" help:"show the processing values"`
19+
20+
Seg []string `env:"-"` // args segments
21+
Path *env.Path `env:"-"` // path params
22+
}
23+
24+
var a Action
25+
a.Path = env.NewEnv(&a)
26+
27+
}
28+
29+
func TestHelp(t *testing.T) {
30+
31+
type Action struct {
32+
Action string `env:"a,order,require" default:"pull" help:"action chain[@path pull|process|expire|export]"`
33+
Secret string `env:"hidden" help:"the shared secret"`
34+
Show bool `default:"on" help:"show the processing values"`
35+
36+
Seg []string `env:"-"` // args segments
37+
Path *env.Path `env:"-"` // path params
38+
}
39+
40+
// spoof help request
41+
os.Args = []string{"test", "help"}
42+
43+
// we have to set opt.NoExit so this test will operate
44+
var a Action
45+
a.Path = env.NewEnv(&env.Options{NoExit: true}, &a)
46+
47+
}
48+
49+
func TestVersion(t *testing.T) {
50+
51+
type Action struct {
52+
Action string `env:"a,order,require" default:"pull" help:"action chain[@path pull|process|expire|export]"`
53+
Secret string `env:"hidden" help:"the shared secret"`
54+
Show bool `default:"on" help:"show the processing values"`
55+
56+
Seg []string `env:"-"` // args segments
57+
Path *env.Path `env:"-"` // path params
58+
}
59+
60+
// spoof version request
61+
os.Args = []string{"test", "version"}
62+
env.Version = "test.0.0.0"
63+
env.Build = "abc"
64+
65+
// we have to set opt.NoExit so this test will operate
66+
var a Action
67+
a.Path = env.NewEnv(&env.Options{NoExit: true}, &a)
68+
69+
}
70+
71+
type Action struct{}
72+
73+
func (a *Action) Start(ctx context.Context) {
74+
log.Println("action: start entry")
75+
defer log.Println("action: start exit")
76+
<-ctx.Done()
77+
}
78+
79+
func (a *Action) Init00() {
80+
log.Println("action: init00 entry")
81+
}
82+
83+
func (a *Action) Init01(ctx context.Context, init *sync.WaitGroup) {
84+
log.Println("action: init01 entry")
85+
defer log.Println("action: init01 exit")
86+
defer init.Done()
87+
<-ctx.Done()
88+
}
89+
90+
func (a *Action) Init02(ctx context.Context) func() {
91+
log.Println("action: init02 entry")
92+
return func() {
93+
defer log.Println("action: init92 exit")
94+
<-ctx.Done()
95+
}
96+
}
97+
98+
func (a *Action) Init03(ctx context.Context) func() {
99+
log.Println("action: init03 entry")
100+
return func() {
101+
go func() {
102+
defer log.Println("action: init03 exit")
103+
<-ctx.Done()
104+
}()
105+
}
106+
}
107+
108+
func TestGraceInit(t *testing.T) {
109+
110+
var a Action
111+
grace := env.GraceInit(nil, a.Init00)
112+
defer grace.Wait()
113+
114+
grace.Done()
115+
116+
}
117+
118+
func TestGraceInitContext(t *testing.T) {
119+
120+
var a Action
121+
grace := env.GraceInitContext(nil, a.Init01)
122+
defer grace.SetExit(-1).Wait()
123+
124+
grace.Done()
125+
126+
}

example/main.go

Lines changed: 0 additions & 32 deletions
This file was deleted.

expire.go

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,35 +18,52 @@ import (
1818
1919
*/
2020

21+
type expire struct {
22+
Path string
23+
TTL time.Duration
24+
}
25+
2126
// Expire struct
2227
type Expire struct {
2328
CheckOn time.Duration // frequency of checks (default: hourly)
24-
item []struct { // directory targets
25-
Path string
26-
TTL time.Duration
27-
}
28-
silent bool
29+
item []expire
30+
silent bool
2931
}
3032

3133
// Silent flag toggle for env.Expire, writes logs on os.Stderr (default: on)
3234
func (ex *Expire) Silent() *Expire { ex.silent = !ex.silent; return ex }
3335

34-
// Add will register a directory/path with customized age timeframe (default: 24hr expiration)
35-
func (ex *Expire) Add(ttl *time.Duration, path ...string) *Expire {
36-
37-
if ttl == nil || *ttl == 0 {
38-
ttl24hr := time.Hour
39-
ttl = &ttl24hr // default
36+
// Add will register a directory/path with customized age timeframe
37+
// and supports various ttl inputs
38+
//
39+
// nil default 24h
40+
// string "24h"
41+
// int hour * n
42+
// time.Duration
43+
func (ex *Expire) Add(ttl interface{}, path ...string) *Expire {
44+
45+
var exp time.Duration
46+
switch d := ttl.(type) {
47+
case nil:
48+
exp = time.Hour * 24
49+
case string:
50+
exp, _ = time.ParseDuration(d)
51+
if exp == 0 {
52+
exp = time.Hour * 24
53+
}
54+
case int:
55+
exp = time.Hour * time.Duration(d)
56+
case *time.Duration:
57+
exp = *d
58+
case time.Duration:
59+
exp = d
4060
}
4161

4262
for i := range path {
4363
if len(path[i]) > 0 {
44-
ex.item = append(ex.item, struct {
45-
Path string
46-
TTL time.Duration
47-
}{path[i], *ttl})
64+
ex.item = append(ex.item, expire{path[i], exp})
4865
if !ex.silent {
49-
log.Printf("expire: add %s ttl[%s]", filepath.Base(path[i]), *ttl)
66+
log.Printf("expire: add %s ttl[%s]", filepath.Base(path[i]), exp)
5067
}
5168
}
5269
}

go.mod

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,3 @@ module github.com/zxdev/env
22

33
go 1.15
44

5-
retract [v2.0.0, v2.9.9]

0 commit comments

Comments
 (0)