-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
211 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,3 +19,7 @@ | |
|
||
# Go workspace file | ||
go.work | ||
|
||
.idea | ||
hugov | ||
|
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,52 @@ | ||
package cmd | ||
|
||
import ( | ||
"errors" | ||
"flag" | ||
"fmt" | ||
"os" | ||
) | ||
|
||
func New() error { | ||
topLevel := flag.NewFlagSet("hugov", flag.ExitOnError) | ||
topLevel.Usage = func() { | ||
fmt.Println("Usage:\n hugov [command]") | ||
fmt.Println("\nCommands:") | ||
fmt.Println(" version: show hugoverse command version") | ||
|
||
fmt.Println("\nExample:") | ||
fmt.Println(" hugov version") | ||
} | ||
|
||
err := topLevel.Parse(os.Args[1:]) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if topLevel.Parsed() { | ||
if len(topLevel.Args()) == 0 { | ||
topLevel.Usage() | ||
return errors.New("please specify a sub-command") | ||
} | ||
|
||
// 获取子命令及参数 | ||
subCommand := topLevel.Args()[0] | ||
|
||
switch subCommand { | ||
case "version": | ||
versionCmd, err := NewVersionCmd(topLevel) | ||
if err != nil { | ||
return err | ||
} | ||
if err := versionCmd.Run(); err != nil { | ||
return err | ||
} | ||
|
||
default: | ||
topLevel.Usage() | ||
return errors.New("invalid sub-command") | ||
} | ||
} | ||
|
||
return nil | ||
} |
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,138 @@ | ||
package cmd | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
"runtime/debug" | ||
"sync" | ||
) | ||
|
||
type versionCmd struct { | ||
parent *flag.FlagSet | ||
cmd *flag.FlagSet | ||
} | ||
|
||
func NewVersionCmd(parent *flag.FlagSet) (*versionCmd, error) { | ||
nCmd := &versionCmd{ | ||
parent: parent, | ||
} | ||
|
||
nCmd.cmd = flag.NewFlagSet("version", flag.ExitOnError) | ||
err := nCmd.cmd.Parse(parent.Args()[1:]) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return nCmd, nil | ||
} | ||
|
||
func (oc *versionCmd) Usage() { | ||
oc.cmd.Usage() | ||
} | ||
|
||
func (oc *versionCmd) Run() error { | ||
fmt.Println(BuildVersionString()) | ||
return nil | ||
} | ||
|
||
func BuildVersionString() string { | ||
program := "hugoverse" | ||
|
||
version := "v" + CurrentVersion.String() | ||
|
||
bi := getBuildInfo() | ||
if bi == nil { | ||
return version | ||
} | ||
if bi.Revision != "" { | ||
version += "-" + bi.Revision | ||
} | ||
|
||
osArch := bi.GoOS + "/" + bi.GoArch | ||
|
||
date := bi.RevisionTime | ||
if date == "" { | ||
date = "unknown" | ||
} | ||
|
||
versionString := fmt.Sprintf("%s %s %s BuildDate=%s", | ||
program, version, osArch, date) | ||
|
||
return versionString | ||
} | ||
|
||
var ( | ||
bInfo *buildInfo | ||
bInfoInit sync.Once | ||
) | ||
|
||
type buildInfo struct { | ||
VersionControlSystem string | ||
Revision string | ||
RevisionTime string | ||
Modified bool | ||
|
||
GoOS string | ||
GoArch string | ||
|
||
*debug.BuildInfo | ||
} | ||
|
||
func getBuildInfo() *buildInfo { | ||
bInfoInit.Do(func() { | ||
bi, ok := debug.ReadBuildInfo() | ||
if !ok { | ||
return | ||
} | ||
|
||
bInfo = &buildInfo{BuildInfo: bi} | ||
|
||
for _, s := range bInfo.Settings { | ||
switch s.Key { | ||
case "vcs": | ||
bInfo.VersionControlSystem = s.Value | ||
case "vcs.revision": | ||
bInfo.Revision = s.Value | ||
case "vcs.time": | ||
bInfo.RevisionTime = s.Value | ||
case "vcs.modified": | ||
bInfo.Modified = s.Value == "true" | ||
case "GOOS": | ||
bInfo.GoOS = s.Value | ||
case "GOARCH": | ||
bInfo.GoArch = s.Value | ||
} | ||
} | ||
}) | ||
|
||
return bInfo | ||
} | ||
|
||
var CurrentVersion = Version{ | ||
Major: 0, | ||
Minor: 1, | ||
PatchLevel: 0, | ||
Suffix: "-DEV", | ||
} | ||
|
||
// Version represents the Hugo build version. | ||
type Version struct { | ||
Major int | ||
|
||
Minor int | ||
|
||
// Increment this for bug releases | ||
PatchLevel int | ||
|
||
// HugoVersionSuffix is the suffix used in the Hugo version string. | ||
// It will be blank for release versions. | ||
Suffix string | ||
} | ||
|
||
func (v Version) String() string { | ||
return version(v.Major, v.Minor, v.PatchLevel, v.Suffix) | ||
} | ||
|
||
func version(major, minor, patch int, suffix string) string { | ||
return fmt.Sprintf("%d.%d.%d%s", major, minor, patch, suffix) | ||
} |
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,3 @@ | ||
module github.com/gohugonet/hugoverse | ||
|
||
go 1.21.1 |
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,14 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/gohugonet/hugoverse/cmd" | ||
"log" | ||
) | ||
|
||
func main() { | ||
log.SetFlags(0) | ||
err := cmd.New() | ||
if err != nil { | ||
log.Fatalf("\nError: %s", err) | ||
} | ||
} |