Skip to content

Commit

Permalink
comand line init
Browse files Browse the repository at this point in the history
  • Loading branch information
sunwei committed Mar 2, 2024
1 parent 7f9fe8d commit 1ea1763
Show file tree
Hide file tree
Showing 5 changed files with 211 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,7 @@

# Go workspace file
go.work

.idea
hugov

52 changes: 52 additions & 0 deletions cmd/cmd.go
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
}
138 changes: 138 additions & 0 deletions cmd/version.go
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)
}
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/gohugonet/hugoverse

go 1.21.1
14 changes: 14 additions & 0 deletions main.go
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)
}
}

0 comments on commit 1ea1763

Please sign in to comment.