Skip to content

Commit

Permalink
overhaul Go code to make it more useful (#39)
Browse files Browse the repository at this point in the history
  • Loading branch information
capnspacehook authored Aug 20, 2023
1 parent 134cf34 commit 7d77efa
Showing 1 changed file with 38 additions and 21 deletions.
59 changes: 38 additions & 21 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package main

import (
"context"
"errors"
"flag"
"fmt"
"log"
"os"
"os/signal"
"runtime/debug"
Expand All @@ -15,12 +15,6 @@ import (

const projectName = "Go Project Template" // REPLACE WITH YOUR PROJECT NAME HERE

var (
debugLogs bool
logPath string
printVersion bool
)

func usage() {
fmt.Fprintf(os.Stderr, `
<Project description>
Expand All @@ -39,23 +33,26 @@ For more information, see https://github.com/<user>/<repo>.
`[1:])
}

func init() {
flag.Usage = usage
flag.BoolVar(&debugLogs, "debug", false, "enable debug logging")
flag.StringVar(&logPath, "l", "stdout", "path to log to")
flag.BoolVar(&printVersion, "version", false, "print version and build information and exit")
}

func main() {
os.Exit(mainRetCode())
}

func mainRetCode() int {
var (
debugLogs bool
logPath string
printVersion bool
)

flag.Usage = usage
flag.BoolVar(&debugLogs, "debug", false, "enable debug logging")
flag.StringVar(&logPath, "l", "stdout", "path to log to")
flag.BoolVar(&printVersion, "version", false, "print version and build information and exit")
flag.Parse()

info, ok := debug.ReadBuildInfo()
if !ok {
log.Println("build information not found")
fmt.Fprintln(os.Stderr, "build information not found")
return 1
}

Expand All @@ -76,14 +73,10 @@ func mainRetCode() int {

logger, err := logCfg.Build()
if err != nil {
log.Printf("error creating logger: %v", err)
fmt.Fprintf(os.Stderr, "error creating logger: %v", err)
return 1
}

// may also want to add syscall.SIGTERM on unix based OSes
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
defer cancel()

// log current version/commit
versionFields := []zap.Field{
zap.String("version", version),
Expand All @@ -96,12 +89,36 @@ func mainRetCode() int {
}
logger.Info("starting "+projectName, versionFields...)

// may also want to add golang.org/x/sys/unix.SIGTERM on unix based OSes
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
defer cancel()

if err := mainErr(ctx, logger); err != nil {
var exitCode *errJustExit
if errors.As(err, &exitCode) {
return int(*exitCode)
}
fmt.Fprintln(os.Stderr, err)
return 1
}
return 0
}

type errJustExit int

func (e errJustExit) Error() string { return fmt.Sprintf("exit: %d", e) }

// this disables a linter warning because nil is unconditionally returned
// here, remove this when adding your own code that can return errors
//
//nolint:unparam
func mainErr(ctx context.Context, logger *zap.Logger) error {
// START MAIN LOGIC HERE

<-ctx.Done()
logger.Info("shutting down")

// STOP MAIN LOGIC HERE

return 0
return nil
}

0 comments on commit 7d77efa

Please sign in to comment.