Skip to content

Commit

Permalink
refactor: move logging to dedicated package
Browse files Browse the repository at this point in the history
  • Loading branch information
TheMeier committed Dec 6, 2024
1 parent a96de32 commit 95933de
Show file tree
Hide file tree
Showing 15 changed files with 748 additions and 485 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ external_modules/
tests/github-test-private/
coverage.txt
local.yaml
.vscode/
18 changes: 9 additions & 9 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,36 +8,36 @@ all: test g10k
g10k: g10k.go $(DEPS)
# -race flag is currently removed because of issues in OS X Monterey. Should be solved above go version 1.17.6
ifeq ($(UNAME), Darwin)
GO111MODULE=on CGO_ENABLED=1 GOOS=darwin go build \
CGO_ENABLED=1 GOOS=darwin go build \
-ldflags "-s -w -X main.buildversion=${BUILDVERSION} -X main.buildtime=${BUILDTIME}" \
-o $@
strip -X $@
endif
ifeq ($(UNAME), Linux)
GO111MODULE=on CGO_ENABLED=1 GOOS=linux go build \
CGO_ENABLED=1 GOOS=linux go build \
-race -ldflags "-s -w -X main.buildversion=${BUILDVERSION} -X main.buildtime=${BUILDTIME}" \
-o $@
strip $@
endif

lint:
GO111MODULE=on go install golang.org/x/lint/golint@latest && \
go install golang.org/x/lint/golint@latest && \
golint *.go

vet: g10k.go
GO111MODULE=on go vet
go vet

imports: g10k.go
GO111MODULE=on go install golang.org/x/tools/cmd/goimports@latest && \
go install golang.org/x/tools/cmd/goimports@latest && \
goimports -d *.go tests/

test: lint vet imports
# This is a workaround for Bug https://github.com/golang/go/issues/49138
ifeq ($(UNAME), Darwin)
GO111MODULE=on MallocNanoZone=0 go test -race -coverprofile=coverage.txt -covermode=atomic -v
MallocNanoZone=0 go test -race -coverprofile=coverage.txt -covermode=atomic -v
endif
ifeq ($(UNAME), Linux)
GO111MODULE=on go test -race -coverprofile=coverage.txt -covermode=atomic -v
go test -race -coverprofile=coverage.txt -covermode=atomic -v ./...
endif

clean:
Expand All @@ -47,7 +47,7 @@ build-image:
docker build -t g10k:${BUILDVERSION} .

update-deps:
GO111MODULE=on go get -u
GO111MODULE=on go mod vendor
go get -u
go mod vendor

.PHONY: all lint vet imports test clean
89 changes: 45 additions & 44 deletions config.go

Large diffs are not rendered by default.

245 changes: 123 additions & 122 deletions forge.go

Large diffs are not rendered by default.

43 changes: 20 additions & 23 deletions g10k.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,11 @@ import (
"strings"
"sync"
"time"

"github.com/xorpaul/g10k/internal/logging"
)

var (
debug bool
verbose bool
info bool
quiet bool
force bool
usemove bool
usecacheFallback bool
Expand All @@ -25,7 +23,6 @@ var (
pfLocation string
clonegit bool
dryRun bool
validate bool
check4update bool
checkSum bool
gitObjectSyntaxNotSupported bool
Expand Down Expand Up @@ -238,14 +235,14 @@ func main() {
flag.BoolVar(&clonegit, "clonegit", false, "populate the Puppet environment with a git clone of each git Puppet module. Helpful when developing locally with -puppetfile")
flag.BoolVar(&force, "force", false, "purge the Puppet environment directory and do a full sync")
flag.BoolVar(&dryRun, "dryrun", false, "do not modify anything, just print what would be changed")
flag.BoolVar(&validate, "validate", false, "only validate given configuration and exit")
flag.BoolVar(&logging.Validate, "validate", false, "only validate given configuration and exit")
flag.BoolVar(&usemove, "usemove", false, "do not use hardlinks to populate your Puppet environments with Puppetlabs Forge modules. Instead uses simple move commands and purges the Forge cache directory after each run! (Useful for g10k runs inside a Docker container)")
flag.BoolVar(&check4update, "check4update", false, "only check if the is newer version of the Puppet module avaialable. Does implicitly set dryrun to true")
flag.BoolVar(&checkSum, "checksum", false, "get the md5 check sum for each Puppetlabs Forge module and verify the integrity of the downloaded archive. Increases g10k run time!")
flag.BoolVar(&debug, "debug", false, "log debug output, defaults to false")
flag.BoolVar(&verbose, "verbose", false, "log verbose output, defaults to false")
flag.BoolVar(&info, "info", false, "log info output, defaults to false")
flag.BoolVar(&quiet, "quiet", false, "no output, defaults to false")
flag.BoolVar(&logging.Debug, "debug", false, "log debug output, defaults to false")
flag.BoolVar(&logging.Verbose, "verbose", false, "log verbose output, defaults to false")
flag.BoolVar(&logging.Info, "info", false, "log info output, defaults to false")
flag.BoolVar(&logging.Quiet, "quiet", false, "no output, defaults to false")
flag.BoolVar(&usecacheFallback, "usecachefallback", false, "if g10k should try to use its cache for sources and modules instead of failing")
flag.BoolVar(&retryGitCommands, "retrygitcommands", false, "if g10k should purge the local repository and retry a failed git command (clone or remote update) instead of failing")
flag.BoolVar(&gitObjectSyntaxNotSupported, "gitobjectsyntaxnotsupported", false, "if your git version is too old to support reference syntax like master^{object} use this setting to revert to the older syntax")
Expand All @@ -265,25 +262,25 @@ func main() {

// check for git executable dependency
if _, err := exec.LookPath("git"); err != nil {
Fatalf("Error: could not find 'git' executable in PATH")
logging.Fatalf("Error: could not find 'git' executable in PATH")
}

target := ""
before := time.Now()
if len(configFile) > 0 {
if usemove {
Fatalf("Error: -usemove parameter is only allowed in -puppetfile mode!")
logging.Fatalf("Error: -usemove parameter is only allowed in -puppetfile mode!")
}
if pfMode {
Fatalf("Error: -puppetfile parameter is not allowed with -config parameter!")
logging.Fatalf("Error: -puppetfile parameter is not allowed with -config parameter!")
}
if (len(outputNameParam) > 0) && (len(branchParam) == 0) {
Fatalf("Error: -outputname specified without -branch!")
logging.Fatalf("Error: -outputname specified without -branch!")
}
if usecacheFallback {
config.UseCacheFallback = true
}
Debugf("Using as config file: " + configFile)
logging.Debugf("Using as config file: " + configFile)
config = readConfigfile(configFile)
checkDirAndCreate(config.CacheDir, "cachedir configured value")
target = configFile
Expand All @@ -296,16 +293,16 @@ func main() {
}
} else {
if pfMode {
Debugf("Trying to use as Puppetfile: " + pfLocation)
logging.Debugf("Trying to use as Puppetfile: " + pfLocation)
sm := make(map[string]Source)
sm["cmdlineparam"] = Source{Basedir: "./"}
cachedir := "/tmp/g10k"
if len(os.Getenv("g10k_cachedir")) > 0 {
cachedir = os.Getenv("g10k_cachedir")
cachedir = checkDirAndCreate(cachedir, "cachedir environment variable g10k_cachedir")
Debugf("Found environment variable g10k_cachedir set to: " + cachedir)
logging.Debugf("Found environment variable g10k_cachedir set to: " + cachedir)
} else if len(cacheDirParam) > 0 {
Debugf("Using -cachedir parameter set to : " + cacheDirParam)
logging.Debugf("Using -cachedir parameter set to : " + cacheDirParam)
cachedir = checkDirAndCreate(cacheDirParam, "cachedir CLI param")
} else {
cachedir = checkDirAndCreate(cachedir, "cachedir default value")
Expand All @@ -326,7 +323,7 @@ func main() {
pfm["cmdlineparam"] = puppetfile
resolvePuppetfile(pfm)
} else {
Fatalf("Error: you need to specify at least a config file or use the Puppetfile mode\nExample call: " + os.Args[0] + " -config test.yaml or " + os.Args[0] + " -puppetfile\n")
logging.Fatalf("Error: you need to specify at least a config file or use the Puppetfile mode\nExample call: " + os.Args[0] + " -config test.yaml or " + os.Args[0] + " -puppetfile\n")
}
}

Expand All @@ -335,12 +332,12 @@ func main() {
defer purgeDir(config.ForgeCacheDir, "main() -puppetfile mode with -usemove parameter")
}

Debugf("Forge response JSON parsing took " + strconv.FormatFloat(forgeJSONParseTime, 'f', 4, 64) + " seconds")
Debugf("Forge modules metadata.json parsing took " + strconv.FormatFloat(metadataJSONParseTime, 'f', 4, 64) + " seconds")
logging.Debugf("Forge response JSON parsing took " + strconv.FormatFloat(forgeJSONParseTime, 'f', 4, 64) + " seconds")
logging.Debugf("Forge modules metadata.json parsing took " + strconv.FormatFloat(metadataJSONParseTime, 'f', 4, 64) + " seconds")

if !check4update && !quiet {
if !check4update && !logging.Quiet {
if len(forgeModuleDeprecationNotice) > 0 {
Warnf(strings.TrimSuffix(forgeModuleDeprecationNotice, "\n"))
logging.Warnf(strings.TrimSuffix(forgeModuleDeprecationNotice, "\n"))
}
fmt.Println("Synced", target, "with", syncGitCount, "git repositories and", syncForgeCount, "Forge modules in "+strconv.FormatFloat(time.Since(before).Seconds(), 'f', 1, 64)+"s with git ("+strconv.FormatFloat(syncGitTime, 'f', 1, 64)+"s sync, I/O", strconv.FormatFloat(ioGitTime, 'f', 1, 64)+"s) and Forge ("+strconv.FormatFloat(syncForgeTime, 'f', 1, 64)+"s query+download, I/O", strconv.FormatFloat(ioForgeTime, 'f', 1, 64)+"s) using", strconv.Itoa(config.Maxworker), "resolve and", strconv.Itoa(config.MaxExtractworker), "extract workers")
}
Expand Down
29 changes: 15 additions & 14 deletions g10k_puppetfile_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"time"

"github.com/davecgh/go-spew/spew"
"github.com/xorpaul/g10k/internal/logging"
)

func equalPuppetfile(a, b Puppetfile) bool {
Expand All @@ -23,35 +24,35 @@ func equalPuppetfile(a, b Puppetfile) bool {
a.privateKey != b.privateKey ||
a.controlRepoBranch != b.controlRepoBranch ||
a.source != b.source {
Debugf("forgeBaseURL, forgeCacheTTL, privateKey, controlRepoBranch or source isn't equal!")
logging.Debugf("forgeBaseURL, forgeCacheTTL, privateKey, controlRepoBranch or source isn't equal!")
return false
}

if len(a.gitModules) != len(b.gitModules) ||
len(a.forgeModules) != len(b.forgeModules) {
Debugf("size of gitModules or forgeModules isn't equal!")
logging.Debugf("size of gitModules or forgeModules isn't equal!")
return false
}

for gitModuleName, gm := range a.gitModules {
if _, ok := b.gitModules[gitModuleName]; !ok {
Debugf("git module " + gitModuleName + " missing!")
logging.Debugf("git module " + gitModuleName + " missing!")
return false
}
if !equalGitModule(gm, b.gitModules[gitModuleName]) {
Debugf("git module " + gitModuleName + " isn't equal!")
logging.Debugf("git module " + gitModuleName + " isn't equal!")
return false
}
}

for forgeModuleName, fm := range a.forgeModules {
if _, ok := b.forgeModules[forgeModuleName]; !ok {
Debugf("forge module " + forgeModuleName + " missing!")
logging.Debugf("forge module " + forgeModuleName + " missing!")
return false
}
//fmt.Println("checking Forge module: ", forgeModuleName, fm)
if !equalForgeModule(fm, b.forgeModules[forgeModuleName]) {
Debugf("forge module " + forgeModuleName + " isn't equal!")
logging.Debugf("forge module " + forgeModuleName + " isn't equal!")
return false
}
}
Expand Down Expand Up @@ -125,7 +126,7 @@ func checkExitCodeAndOutputOfReadPuppetfileSubprocess(t *testing.T, forceForgeVe
cmd := exec.Command(os.Args[0], "-test.run="+testFunctionName+"$")
cmd.Env = append(os.Environ(), "TEST_FOR_CRASH_"+testFunctionName+"=1")
out, err := cmd.CombinedOutput()
if debug {
if logging.Debug {
fmt.Print(string(out))
}

Expand Down Expand Up @@ -364,7 +365,7 @@ func TestReadPuppetfileForgeDash(t *testing.T) {
}

func TestReadPuppetfileInstallPath(t *testing.T) {
quiet = true
logging.Quiet = true
funcName := strings.Split(funcName(), ".")[len(strings.Split(funcName(), "."))-1]
got := readPuppetfile("tests/"+funcName, "", "test", "test", false, false)

Expand All @@ -382,7 +383,7 @@ func TestReadPuppetfileInstallPath(t *testing.T) {
}

func TestReadPuppetfileLocalModule(t *testing.T) {
quiet = true
logging.Quiet = true
funcName := strings.Split(funcName(), ".")[len(strings.Split(funcName(), "."))-1]
got := readPuppetfile("tests/"+funcName, "", "test", "test", false, false)

Expand Down Expand Up @@ -411,7 +412,7 @@ func TestReadPuppetfileMissingTrailingComma2(t *testing.T) {
}

func TestReadPuppetfileForgeNotationGitModule(t *testing.T) {
quiet = true
logging.Quiet = true
funcName := strings.Split(funcName(), ".")[len(strings.Split(funcName(), "."))-1]
got := readPuppetfile("tests/"+funcName, "", "test", "test", false, false)

Expand All @@ -429,7 +430,7 @@ func TestReadPuppetfileForgeNotationGitModule(t *testing.T) {
}

func TestReadPuppetfileGitSlashNotation(t *testing.T) {
quiet = true
logging.Quiet = true
funcName := strings.Split(funcName(), ".")[len(strings.Split(funcName(), "."))-1]
got := readPuppetfile("tests/"+funcName, "", "test", "test", false, false)

Expand All @@ -454,7 +455,7 @@ func TestReadPuppetfileGitSlashNotation(t *testing.T) {
}

func TestReadPuppetfileGitDashNotation(t *testing.T) {
quiet = true
logging.Quiet = true
funcName := strings.Split(funcName(), ".")[len(strings.Split(funcName(), "."))-1]
got := readPuppetfile("tests/"+funcName, "", "test", "test", false, false)

Expand All @@ -479,7 +480,7 @@ func TestReadPuppetfileGitDashNotation(t *testing.T) {
}

func TestReadPuppetfileGitDashNSlashNotation(t *testing.T) {
quiet = true
logging.Quiet = true
funcName := strings.Split(funcName(), ".")[len(strings.Split(funcName(), "."))-1]
got := readPuppetfile("tests/"+funcName, "", "test", "test", false, false)

Expand All @@ -504,7 +505,7 @@ func TestReadPuppetfileGitDashNSlashNotation(t *testing.T) {
}

func TestReadPuppetfileSSHKeyAlreadyLoaded(t *testing.T) {
quiet = true
logging.Quiet = true
funcName := strings.Split(funcName(), ".")[len(strings.Split(funcName(), "."))-1]
got := readPuppetfile("tests/"+funcName, "", "test", "test", false, false)

Expand Down
Loading

0 comments on commit 95933de

Please sign in to comment.