From 934396271455128ba82b25c4a8bcafd4b559469d Mon Sep 17 00:00:00 2001 From: Mikhail Chikankov Date: Fri, 15 Dec 2023 11:10:37 +0400 Subject: [PATCH] feat: add automated version verification function --- CHANGELOG.md | 5 +++++ cmd/add.go | 7 +++++- cmd/root.go | 17 +++++++++++++++ cmd/version.go | 54 ++++++++++++++++++++++++++++++++++++++++++++++ go.mod | 1 + go.sum | 2 ++ version/version.go | 38 ++++++++++++++++++++++++++++++++ 7 files changed, 123 insertions(+), 1 deletion(-) create mode 100644 cmd/version.go create mode 100644 version/version.go diff --git a/CHANGELOG.md b/CHANGELOG.md index fba4903..7758af8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/cmd/add.go b/cmd/add.go index 2b82bc0..a596ceb 100644 --- a/cmd/add.go +++ b/cmd/add.go @@ -2,6 +2,7 @@ package cmd import ( "fmt" + "github.com/neptunsk1y/ignore/version" "github.com/spf13/cobra" "log" "os" @@ -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") diff --git a/cmd/root.go b/cmd/root.go index b6d062b..868b944 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -1,8 +1,11 @@ package cmd import ( + "fmt" + "github.com/charmbracelet/log" "github.com/spf13/cobra" "os" + "strings" ) var rootCmd = &cobra.Command{ @@ -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) +} diff --git a/cmd/version.go b/cmd/version.go new file mode 100644 index 0000000..cf2330e --- /dev/null +++ b/cmd/version.go @@ -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)) + }, +} diff --git a/go.mod b/go.mod index 1fd8712..937f6f9 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index ea5b422..4f9bca0 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/version/version.go b/version/version.go new file mode 100644 index 0000000..62658c9 --- /dev/null +++ b/version/version.go @@ -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 +}