From 1ea1763619d609207cfa13234e7bbb3b8bf4a9ea Mon Sep 17 00:00:00 2001 From: sunwei Date: Sat, 2 Mar 2024 11:01:24 +0800 Subject: [PATCH] comand line init --- .gitignore | 4 ++ cmd/cmd.go | 52 +++++++++++++++++++ cmd/version.go | 138 +++++++++++++++++++++++++++++++++++++++++++++++++ go.mod | 3 ++ main.go | 14 +++++ 5 files changed, 211 insertions(+) create mode 100644 cmd/cmd.go create mode 100644 cmd/version.go create mode 100644 go.mod create mode 100644 main.go diff --git a/.gitignore b/.gitignore index 3b735ec..07d48a3 100644 --- a/.gitignore +++ b/.gitignore @@ -19,3 +19,7 @@ # Go workspace file go.work + +.idea +hugov + diff --git a/cmd/cmd.go b/cmd/cmd.go new file mode 100644 index 0000000..1a60658 --- /dev/null +++ b/cmd/cmd.go @@ -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 +} diff --git a/cmd/version.go b/cmd/version.go new file mode 100644 index 0000000..001c0e7 --- /dev/null +++ b/cmd/version.go @@ -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) +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..7a9841b --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module github.com/gohugonet/hugoverse + +go 1.21.1 diff --git a/main.go b/main.go new file mode 100644 index 0000000..fd7bd47 --- /dev/null +++ b/main.go @@ -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) + } +}