From fd8025991c7fcd6e3cd2dfbed638fa3e4a276d6a Mon Sep 17 00:00:00 2001 From: Foil-hat-guy Date: Mon, 21 Apr 2025 21:37:03 +0300 Subject: [PATCH] First commit. --- LICENSE | 35 +++++++++++++++++++++++++++++++++++ README.md | 41 +++++++++++++++++++++++++++++++++++++++++ go.mod | 5 +++++ go.sum | 2 ++ main.go | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 136 insertions(+) create mode 100644 LICENSE create mode 100644 README.md create mode 100644 go.mod create mode 100644 go.sum create mode 100644 main.go diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..6ccdb6b --- /dev/null +++ b/LICENSE @@ -0,0 +1,35 @@ +3-Clause BSD NON-AI License + +Copyright 2025 The foilhatguy.casa domain name owner. + +Redistribution and use in source and binary forms, with or without modification, +are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this +list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright notice, +this list of conditions and the following disclaimer in the documentation and/or +other materials provided with the distribution. + +3. Neither the name of the copyright holder nor the names of its contributors +may be used to endorse or promote products derived from this software without +specific prior written permission. + +4. The source code and the binary form, and any modifications made to them may +not be used for the purpose of training or improving machine learning algorithms, +including but not limited to artificial intelligence, natural language processing, +or data mining. This condition applies to any derivatives, modifications, or +updates based on the Software code. Any usage of the source code or the binary +form in an AI-training dataset is considered a breach of this License. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND +ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED +WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. +IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, +INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF +LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE +OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED +OF THE POSSIBILITY OF SUCH DAMAGE. \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..07137a8 --- /dev/null +++ b/README.md @@ -0,0 +1,41 @@ +### The "Version" package + +This package provides --version flag feature to your cli. +It recognize both short and long flags. Version number is +automatically added from the latest tag in your git repository. + +------------- + +### Usage +1. Download the package by **go get** command: +```shell +go get code.foilhatguy.casa/pub/version +``` +2. Add the package as a tool dependency to your project in +tools.go: +```go +// +build tools + +package tools + +import ( + _ "code.foilhatguy.casa/pub/version" +) +``` +3. In the main package add a go:generate directive: +```go +//go:generate go run code.foilhatguy.casa/pub/version +``` +4. Run code generator by **go generate** command: +```shell +go generate ./... +``` +The package will detect a version number from the latest git +tag from your project. Then it will generate a **version.go** +file as a part of the main package. It will contain a version +number as a constant and code for --version flag detection. + +5. Build and run your application with the --version flag. +```shell +go run . --version +``` \ No newline at end of file diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..614a536 --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module code.foilhatguy.casa/pub/help + +go 1.19 + +require github.com/writeas/go-strip-markdown v2.0.1+incompatible diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..ce45d81 --- /dev/null +++ b/go.sum @@ -0,0 +1,2 @@ +github.com/writeas/go-strip-markdown v2.0.1+incompatible h1:IIqxTM5Jr7RzhigcL6FkrCNfXkvbR+Nbu1ls48pXYcw= +github.com/writeas/go-strip-markdown v2.0.1+incompatible/go.mod h1:Rsyu10ZhbEK9pXdk8V6MVnZmTzRG0alMNLMwa0J01fE= diff --git a/main.go b/main.go new file mode 100644 index 0000000..feacf40 --- /dev/null +++ b/main.go @@ -0,0 +1,53 @@ +package main + +import ( + "fmt" + "io/ioutil" + "os" + + markdown "github.com/writeas/go-strip-markdown" +) + +const helpGOcode = ` +//WARNING! DO NOT EDIT THIS FILE BY HAND. IT IS AUTO GENERATED. +package main + +import ( + "fmt" + "os" + "strings" + "embed" +) + +//go:embed help +var goFS embed.FS + +func containFlag(arg string, sfortFlag string) bool { + if len(arg)>1 { + if arg[0] == "-"[0] && arg[1] != "-"[0] { + return strings.Contains(arg, sfortFlag) + } + } + return false +} + +func init() { + for _, arg := range os.Args { + if containFlag(arg, "h") || arg == "--help" { + fmt.Println(gitVersion) + os.Exit(0) + } + } +}` + +func main() { + helpMarkDown, err := ioutil.ReadFile(os.Args[1]) + if err != nil { + fmt.Println(err) + os.Exit(0) + } + helpPlainText := markdown.Strip(string(helpMarkDown)) + + ioutil.WriteFile("help", []byte(helpPlainText), 0644) + ioutil.WriteFile("help.go", []byte(helpGOcode), 0644) +}