Skip to content

Commit

Permalink
feat: add automated version verification function
Browse files Browse the repository at this point in the history
  • Loading branch information
neptship committed Dec 15, 2023
1 parent 6d76f6b commit 9343962
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 1 deletion.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com), and this project adheres to
[Semantic Versioning](https://semver.org).
## v1.4.0

- Added the version check command
- Version verification is automated
- The work of the add command has been optimized

## v1.3.2

Expand Down
7 changes: 6 additions & 1 deletion cmd/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package cmd

import (
"fmt"
"github.com/neptunsk1y/ignore/version"
"github.com/spf13/cobra"
"log"
"os"
Expand All @@ -24,7 +25,11 @@ var addCommand = &cobra.Command{
if err != nil {
log.Fatal(err)
}
pathTemplateFile := dirname + "/go/pkg/mod/github.com/neptunsk1y/ignore@v1.3.2/templates/" + args[1] + ".gitignore"
_, err = version.Latest()
if err != nil {
fmt.Println("Error version check")
}
pathTemplateFile := dirname + "/go/pkg/mod/github.com/neptunsk1y/ignore@v" + version.Version + "/templates/" + args[1] + ".gitignore"
if _, err = os.Stat(pathTemplateFile); err != nil {
if os.IsNotExist(err) {
log.Fatal("The template does not exist")
Expand Down
17 changes: 17 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package cmd

import (
"fmt"
"github.com/charmbracelet/log"
"github.com/spf13/cobra"
"os"
"strings"
)

var rootCmd = &cobra.Command{
Expand All @@ -21,3 +24,17 @@ func Execute() {
func init() {
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}

func handleErr(err error) {
if err == nil {
return
}

log.Error(err)
_, _ = fmt.Fprintf(
os.Stderr,
"%s\n",
strings.Trim(err.Error(), " \n"),
)
os.Exit(1)
}
54 changes: 54 additions & 0 deletions cmd/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package cmd

import (
"fmt"
"github.com/neptunsk1y/ignore/version"
"html/template"
"runtime"

"github.com/charmbracelet/lipgloss"

"github.com/spf13/cobra"
)

func init() {
rootCmd.AddCommand(versionCmd)
}

var versionCmd = &cobra.Command{
Use: "version",
Short: "print the version number of the ignore",
Run: func(cmd *cobra.Command, args []string) {
_, err := version.Latest()
if err != nil {
fmt.Println("Error version check")
}

versionInfo := struct {
Version string
OS string
Arch string
App string
Compiler string
}{
Version: version.Version,
App: "ignore",
OS: runtime.GOOS,
Arch: runtime.GOARCH,
Compiler: runtime.Compiler,
}

t, err := template.New("version").Funcs(map[string]any{
"faint": lipgloss.NewStyle().Faint(true).Render,
"bold": lipgloss.NewStyle().Bold(true).Render,
"magenta": lipgloss.NewStyle().Foreground(lipgloss.Color("#5a6368")).Render,
}).Parse(`{{ magenta "▇▇▇" }} {{ magenta .App }}
{{ faint "Version" }} {{ bold .Version }}
{{ faint "Platform" }} {{ bold .OS }}/{{ bold .Arch }}
{{ faint "Compiler" }} {{ bold .Compiler }}
`)
handleErr(err)
handleErr(t.Execute(cmd.OutOrStdout(), versionInfo))
},
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ require (
github.com/muesli/reflow v0.3.0 // indirect
github.com/muesli/termenv v0.15.2 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
github.com/samber/lo v1.39.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/exp v0.0.0-20231006140011-7918f672742d // indirect
golang.org/x/sys v0.13.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJ
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/samber/lo v1.39.0 h1:4gTz1wUhNYLhFSKl6O+8peW0v2F4BCY034GRpU9WnuA=
github.com/samber/lo v1.39.0/go.mod h1:+m/ZKRl6ClXCE2Lgf3MsQlWfh4bn1bz6CXEOxnEXnEA=
github.com/spf13/cobra v1.8.0 h1:7aJaZx1B85qltLMc546zn58BxxfZdR/W22ej9CFoEf0=
github.com/spf13/cobra v1.8.0/go.mod h1:WXLWApfZ71AjXPya3WOlMsY9yMs7YeiHhFVlvLyhcho=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
Expand Down
38 changes: 38 additions & 0 deletions version/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package version

import (
"encoding/json"
"errors"
"net/http"
)

var Version string

func Latest() (version string, err error) {
if err != nil {
return "", err
}

resp, err := http.Get("https://api.github.com/repos/neptunsk1y/ignore/releases/latest")
if err != nil {
return
}

defer resp.Body.Close()

var release struct {
TagName string `json:"tag_name"`
}

err = json.NewDecoder(resp.Body).Decode(&release)
if err != nil {
return
}

if release.TagName == "" {
err = errors.New("empty tag name")
return
}
Version = release.TagName[1:]
return
}

0 comments on commit 9343962

Please sign in to comment.