Skip to content

Commit b01878f

Browse files
committed
Use hclog instead of stdlib log
1 parent a917245 commit b01878f

File tree

3 files changed

+29
-13
lines changed

3 files changed

+29
-13
lines changed

cachefiller.go

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ package main
22

33
import (
44
"context"
5-
"log"
65
"strings"
76

87
"github.com/MiLk/nsscache-go/cache"
98
"github.com/MiLk/nsscache-go/source"
9+
"github.com/hashicorp/go-hclog"
1010

1111
pb "github.com/netauth/protocol"
1212
"github.com/netauth/netauth/pkg/netauth"
@@ -52,6 +52,8 @@ type NetAuthCacheFiller struct {
5252
DefaultHome string
5353

5454
c *netauth.Client
55+
56+
l hclog.Logger
5557
}
5658

5759
// NewCacheFiller returns an interface that can be used to fill caches
@@ -70,13 +72,15 @@ func NewCacheFiller(minuid, mingid int32, defshell, defhome string, shells []str
7072
AllowedShells: shells,
7173

7274
DefaultHome: defhome,
75+
76+
l: hclog.L().Named("cachefiller"),
7377
}
7478

7579
ctx := context.Background()
7680

7781
c, err := netauth.New()
7882
if err != nil {
79-
log.Println("Error during client initialization")
83+
x.l.Error("Error during client initialization", "error", err)
8084
return nil, err
8185
}
8286
c.SetServiceName("nsscache")
@@ -148,7 +152,10 @@ func (nc *NetAuthCacheFiller) findGroups(ctx context.Context) error {
148152
if grps[i].GetNumber() < nc.MinGID {
149153
// Group number is too low, continue without
150154
// this one.
151-
log.Printf("Ignoring group %s, GID is below cutoff (%d < %d)", grps[i].GetName(), grps[i].GetNumber(), nc.MinGID)
155+
nc.l.Warn("Ignoring group, GID is below cutoff",
156+
"group", grps[i].GetName(),
157+
"limit", nc.MinGID,
158+
"gid", grps[i].GetNumber())
152159
continue
153160
}
154161
nc.groups[grps[i].GetName()] = grps[i]
@@ -173,13 +180,17 @@ func (nc *NetAuthCacheFiller) findEntities(ctx context.Context) error {
173180
if ents[i].GetNumber() < nc.MinUID {
174181
// The uidNumber was too low, continue without
175182
// this one.
176-
log.Printf("Ignoring entity %s, UID is below cutoff (%d < %d)", ents[i].GetID(), ents[i].GetNumber(), nc.MinUID)
183+
nc.l.Warn("Ignoring entity, UID is below cutoff",
184+
"entity", ents[i].GetID(),
185+
"limit", nc.MinUID,
186+
"uid", ents[i].GetNumber())
177187
continue
178188
}
179189
if _, ok := nc.pgroups[ents[i].GetMeta().GetPrimaryGroup()]; !ok {
180190
// The primary group was invalid, continue
181191
// without this one.
182-
log.Printf("Ignoring entity %s, Primary Group is invalid", ents[i].GetID())
192+
nc.l.Warn("Ignoring entity, Primary Group is invalid",
193+
"entity", ents[i].GetID())
183194
continue
184195
}
185196
if nc.hasBadShell(ents[i].GetMeta().GetShell()) {

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ go 1.12
44

55
require (
66
github.com/MiLk/nsscache-go v1.1.0
7+
github.com/hashicorp/go-hclog v0.9.2
78
github.com/netauth/netauth v0.3.1
89
github.com/netauth/protocol v0.0.0-20191124005711-167b58b61c72
910
github.com/spf13/pflag v1.0.3

main.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ package main
33
import (
44
"fmt"
55
"io/ioutil"
6-
"log"
76
"os"
87
"strings"
98

9+
"github.com/hashicorp/go-hclog"
1010
"github.com/spf13/pflag"
1111
"github.com/spf13/viper"
1212

@@ -24,13 +24,15 @@ var (
2424

2525
outDir = pflag.String("out", "/etc", "Output directory for cache files")
2626
cfgfile = pflag.String("config", "", "Config file to use")
27+
28+
log hclog.Logger
2729
)
2830

2931
func initialize() {
3032
// Grab a listing of system shells and add them here
3133
bytes, err := ioutil.ReadFile("/etc/shells")
3234
if err != nil {
33-
log.Printf("Error reading /etc/shells %s", err)
35+
log.Error("Error reading /etc/shells %s", "error", err)
3436
os.Exit(2)
3537
}
3638
shellString := string(bytes[:])
@@ -39,9 +41,9 @@ func initialize() {
3941
systemShells = append(systemShells, s)
4042
}
4143
}
42-
log.Println("The system will accept the following shells")
44+
log.Info("The system will accept the following shells")
4345
for _, s := range systemShells {
44-
log.Printf(" %s", s)
46+
log.Info(fmt.Sprintf(" %s", s))
4547
}
4648
}
4749

@@ -66,19 +68,21 @@ func main() {
6668

6769
filler, err := NewCacheFiller(int32(*minUID), int32(*minGID), *defShell, *defHomeDir, systemShells)
6870
if err != nil {
69-
log.Fatal("Error initializing Cache Filler: ", err)
71+
log.Error("Error initializing Cache Filler: ", "error" ,err)
72+
os.Exit(1)
7073
}
7174

7275
cm := nsscache.NewCaches()
7376
if err := cm.FillCaches(filler); err != nil {
74-
log.Fatal("Unable to fill caches: ", err)
77+
log.Error("Unable to fill caches: ", "error", err)
78+
os.Exit(1)
7579
}
7680

7781
err = cm.WriteFiles(&nsscache.WriteOptions{
7882
Directory: *outDir,
7983
})
8084
if err != nil {
81-
log.Fatal("Error writing updated caches: ", err)
85+
log.Error("Error writing updated caches: ", "error", err)
8286
}
83-
log.Println("Caches Updated")
87+
log.Info("Caches Updated")
8488
}

0 commit comments

Comments
 (0)