From b267ca70e14f64ebdc738a20212b2d55af10350c Mon Sep 17 00:00:00 2001 From: gilex-dev Date: Fri, 15 Sep 2023 23:45:28 +0200 Subject: [PATCH] add build version, Makefile & install instructions --- INSTALL.md | 68 ++++++++++++++++++++++++++++++++++++++++++++ Makefile | 71 ++++++++++++++++++++++++++++++++++++++++++++++ cmd/root.go | 19 +++++++++++-- cmd/server.go | 2 +- globals/globals.go | 20 +++++++++++++ server/main.go | 5 ++++ 6 files changed, 181 insertions(+), 4 deletions(-) create mode 100644 INSTALL.md create mode 100644 Makefile create mode 100644 globals/globals.go diff --git a/INSTALL.md b/INSTALL.md new file mode 100644 index 0000000..bcf54fe --- /dev/null +++ b/INSTALL.md @@ -0,0 +1,68 @@ +# Install YetAnotherToDoList + +## Install via `go install` + +> ⚠️ no commit & version information provided + +```bash +go install somepi.ddns.net/gitea/gilex-dev/YetAnotherToDoList@latest +``` + +...or specify the installation location by adding `GOBIN=` + +## Install from git repository + +Requirements: + +- go 1.20 +- git +- GNU make + +```bash +git clone https://somepi.ddns.net/gitea/gilex-dev/YetAnotherToDoList.git +cd YetAnotherToDoList +go mod tidy # optional +make build +``` + +To customize your build, you can run `make` with these environment Variables (on the same line): + +``` +INSTALL_DIR= +BUILD_FLAGS= +RUN_FLAGS= +``` + +## Install from pre-build binary + +There are currently no pre-build binaries available + +## Add shell completion + +YetAnotherToDoList must be in your `PATH` for this to work. + +### Bash + +```bash +source <(YetAnotherToDoList completion bash) +``` + +For other shells or more info, see `YetAnotherToDoList help completion ` + +## Uninstall + +Run `make uninstall` or + +```bash +rm $HOME/.local/bin/YetAnotherToDoList # replace with your installation path +``` + +> Don't forget to delete the `.log`, `.yaml` and `.sqlite3` files if you don't need them any more. + +### Remove shell completion + +#### Bash + +```bash +complete -r YetAnotherToDoList +``` diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..204db16 --- /dev/null +++ b/Makefile @@ -0,0 +1,71 @@ +# YetAnotherToDoList +# Copyright © 2023 gilex-dev gilex-dev@proton.me + +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, version 3. + +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. + +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . +project:=YetAnotherToDoList +goPackage:=somepi.ddns.net/gitea/gilex-dev/YetAnotherToDoList/globals + +INSTALL_DIR?=$$HOME/.local/bin +BUILD_FLAGS?= +RUN_FLAGS?= + +commitHash=$(shell git rev-parse --short HEAD) +ifneq ($(shell git status --porcelain),) + # working directory not clean + commitHash:=$(commitHash)* +endif + +latestTag:=$(shell git describe --abbrev=0 --tags 2> /dev/null)# suppress possible error + +ifeq ($(shell git tag --points-at HEAD),) + # current commit has no tag + ifeq ($(latestTag),) + # no previous tag exists + latestTag=unknown + else + # found a tag before HEAD + latestTag:=$(latestTag)* + endif +endif + +.PHONY: all +all: install + +.PHONY: build +build: + go build $(BUILD_FLAGS) -ldflags="-X '$(goPackage).Version=$(latestTag)' -X '$(goPackage).CommitHash=$(commitHash)'" -o $(project) . + +.PHONY: run +run: + go run $(BUILD_FLAGS) -ldflags="-X '$(goPackage).Version=$(latestTag)' -X '$(goPackage).CommitHash=$(commitHash)'" . $(RUN_FLAGS) + +$(project): build + +.PHONY: install +install: $(project) + install $(project) $(INSTALL_DIR) + @echo -e "\nIf you want to add shell completion, see INSTALL.md" + +.PHONY: uninstall +uninstall: + @-rm $(INSTALL_DIR)/$(project) + +.PHONY: info +info: + $(info project=$(project)) + $(info goPackage=$(goPackage)) + $(info latestTag=$(latestTag)) + $(info commitHash=$(commitHash)) + $(info BUILD_FLAGS=$(BUILD_FLAGS)) + $(info INSTALL_DIR=$(INSTALL_DIR)) + @echo -e "\nSee INSTALL.md for usage" diff --git a/cmd/root.go b/cmd/root.go index 7971cce..29e6bff 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -26,6 +26,7 @@ import ( "github.com/spf13/cobra" "github.com/spf13/viper" + "somepi.ddns.net/gitea/gilex-dev/YetAnotherToDoList/globals" ) var cfgFile string @@ -33,8 +34,9 @@ var logger *log.Logger // rootCmd represents the base command when called without any subcommands var rootCmd = &cobra.Command{ - Use: "YetAnotherToDoList", - Short: "A simple To Do List Web Application with Go backend, Vue.js frontend and a GraphQL API", + Use: "YetAnotherToDoList", + Version: fmt.Sprintf("%s %s", globals.Version, globals.CommitHash), + Short: "Simple To Do List Web Application with Vue.js frontend and GraphQL API", Long: `YetAnotherToDoList 2023 by gilex-dev A simple To Do List Web Application with Go backend, Vue.js frontend and a GraphQL API `, @@ -53,6 +55,17 @@ func Execute() { } func init() { + rootCmd.CompletionOptions.HiddenDefaultCmd = true + // check if first argument needs init (inspired by https://github.com/spf13/cobra/issues/823#issuecomment-617863653) + if len(os.Args) > 1 { + noSetupRequired := []string{"__complete", "__completeNoDesc", "completion", "help", licenseCmd.Name()} + for _, subcommand := range noSetupRequired { + if subcommand != os.Args[1] { + continue + } + return + } + } cobra.OnInitialize(initConfig, initLog) // Here you will define your flags and configuration settings. @@ -143,7 +156,7 @@ func initLog() { if err != nil { fmt.Println("Failed to write to log file:", err) } else { - logger.Println("Switching to log file", log_path) + logger.Println("Switching to log file", log_path, os.Args, "🌵") logger.SetOutput(log_file) } } diff --git a/cmd/server.go b/cmd/server.go index b5127d7..fdc5b30 100644 --- a/cmd/server.go +++ b/cmd/server.go @@ -16,7 +16,7 @@ import ( var serverCmd = &cobra.Command{ Use: "server", Short: "Start the http server", - Long: ``, + Long: `Starts the http server for the graphql api`, Run: func(cmd *cobra.Command, args []string) { if err := viper.BindPFlag("debug", cmd.Flags().Lookup("debug")); err != nil { diff --git a/globals/globals.go b/globals/globals.go new file mode 100644 index 0000000..e3f43d6 --- /dev/null +++ b/globals/globals.go @@ -0,0 +1,20 @@ +/* +YetAnotherToDoList +Copyright © 2023 gilex-dev gilex-dev@proton.me + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, version 3. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +*/ + +package globals + +var Version, CommitHash string = "unknown", "unknown" diff --git a/server/main.go b/server/main.go index 5099640..033438a 100644 --- a/server/main.go +++ b/server/main.go @@ -17,12 +17,14 @@ along with this program. If not, see . package server import ( + "fmt" "log" "net/http" "strconv" "github.com/99designs/gqlgen/graphql/handler" "github.com/99designs/gqlgen/graphql/playground" + "somepi.ddns.net/gitea/gilex-dev/YetAnotherToDoList/globals" "somepi.ddns.net/gitea/gilex-dev/YetAnotherToDoList/graph" ) @@ -30,6 +32,9 @@ func StartServer(logger *log.Logger, port int) { srv := handler.NewDefaultServer(graph.NewExecutableSchema(graph.Config{Resolvers: &graph.Resolver{}})) http.Handle("/", playground.Handler("GraphQL playground", "/api")) + http.HandleFunc("/version", func(w http.ResponseWriter, r *http.Request) { + fmt.Fprintf(w, "%s %s", globals.Version, globals.CommitHash) + }) http.Handle("/api", srv) logger.Printf("connect to http://localhost:%v/ for GraphQL playground", port)