First commit.
This commit is contained in:
commit
fd8025991c
5 changed files with 136 additions and 0 deletions
35
LICENSE
Normal file
35
LICENSE
Normal file
|
@ -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.
|
41
README.md
Normal file
41
README.md
Normal file
|
@ -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
|
||||||
|
```
|
5
go.mod
Normal file
5
go.mod
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
module code.foilhatguy.casa/pub/help
|
||||||
|
|
||||||
|
go 1.19
|
||||||
|
|
||||||
|
require github.com/writeas/go-strip-markdown v2.0.1+incompatible
|
2
go.sum
Normal file
2
go.sum
Normal file
|
@ -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=
|
53
main.go
Normal file
53
main.go
Normal file
|
@ -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)
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue