-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add automated version verification function
- Loading branch information
Showing
7 changed files
with
123 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |