From 52868d7aa8e15ca3623695fb509f280017b979d3 Mon Sep 17 00:00:00 2001 From: Dan Anglin Date: Wed, 8 Mar 2023 00:42:04 +0000 Subject: [PATCH] fix: add version and print with --version Add a version variable that is printed to screen with the --version flag. Add a basic Makefile for building and installing spruce. --- Makefile | 21 +++++++++++++++++++++ main.go | 26 +++++++++++++++++++++----- 2 files changed, 42 insertions(+), 5 deletions(-) create mode 100644 Makefile diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..d1790d7 --- /dev/null +++ b/Makefile @@ -0,0 +1,21 @@ +BINARY = spruce +INSTALL_PREFIX ?= /usr/local +CGO_ENABLED ?= 0 +GOOS ?= linux +GOARCH ?= amd64 +VERSION = 0.1.0 + +LDFLAGS = "-s -w -X main.version=$(VERSION) -X main.installPrefix=$(INSTALL_PREFIX)" + +$(BINARY): + go build -ldflags=$(LDFLAGS) -v -a -o $(BINARY) + +install: spruce + cp -f $(BINARY) $(INSTALL_PREFIX)/bin + chmod 0755 $(INSTALL_PREFIX)/bin/$(BINARY) + +uninstall: + rm -f $(INSTALL_PREFIX)/bin/$(BINARY) + +clean: + go clean diff --git a/main.go b/main.go index f5286d1..03e058f 100644 --- a/main.go +++ b/main.go @@ -19,15 +19,27 @@ import ( //go:embed templates/tex/* var templates embed.FS +var version string + func main() { - var input, output string - var employmentHistory int + var ( + input string + output string + employmentHistory int + printVersion bool + ) flag.StringVar(&input, "input", "", "specify the CV JSON file that you want to input to the builder.") flag.StringVar(&output, "output", "", "specify the name of the output CV file.") flag.IntVar(&employmentHistory, "employment-history", 10, "show employment history within these number of years.") + flag.BoolVar(&printVersion, "version", false, "print the application version and exit.") flag.Parse() + if printVersion { + Version() + os.Exit(0) + } + if err := run(input, output, employmentHistory); err != nil { log.Fatalf("ERROR: %v", err) } @@ -79,7 +91,7 @@ func tex(input, tempDir string, historyLimit time.Time) (string, error) { "notLastElement": tf.NotLastElement, "join": tf.JoinSentences, "durationToString": tf.FormatDuration, - "withinTimePeriod": tf.WithinTimePeriod(historyLimit), + "withinTimePeriod": tf.WithinTimePeriod(historyLimit), } t := template.Must(template.New("cv.tmpl.tex"). @@ -114,14 +126,14 @@ func pdf(tempDir, texFile, output string) error { output = "./cv.pdf" } - if err := copy(filepath.Join(tempDir, "cv.pdf"), output); err != nil { + if err := copyfile(filepath.Join(tempDir, "cv.pdf"), output); err != nil { return err } return nil } -func copy(input, output string) error { +func copyfile(input, output string) error { inputFile, err := os.Open(input) if err != nil { return fmt.Errorf("unable to open %s; %w", input, err) @@ -141,3 +153,7 @@ func copy(input, output string) error { return nil } + +func Version() { + fmt.Printf("Spruce version %s\n", version) +}