Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions cmd/go-cli-github/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,26 @@ import (

// CLI represents the command-line interface.
type CLI struct {
Version VersionCmd `kong:"cmd,help='Print version information'"`
Serve ServeCmd `kong:"cmd,default=1,help='(default) Example serve command'"`
Version kong.VersionFlag `kong:"help='Print version information'"`
Serve ServeCmd `kong:"cmd,default=1,help='(default) Example serve command'"`
}

func main() {
ctx, stop := signal.NotifyContext(
context.Background(), os.Interrupt, syscall.SIGTERM)
defer stop()
verJSON, err := formatVersionJSON()
if err != nil {
panic(err)
}
// parse CLI config
cli := CLI{}
kctx := kong.Parse(&cli,
kong.UsageOnError(),
kong.BindFor(ctx),
kong.Vars{
"version": verJSON,
},
)
// execute CLI
kctx.FatalIfErrorf(kctx.Run())
Expand Down
16 changes: 5 additions & 11 deletions cmd/go-cli-github/version.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package main

import (
"context"
"encoding/json"
"fmt"
"encoding/json/v2"
"runtime"
)

Expand All @@ -15,11 +13,8 @@ var (
version string
)

// VersionCmd represents the `version` command.
type VersionCmd struct{}

// Run the Version command.
func (*VersionCmd) Run(ctx context.Context) error {
// formatVersionJSON formats version information as a JSON string.
func formatVersionJSON() (string, error) {
v, err := json.Marshal(
struct {
ProjectName string
Expand All @@ -35,8 +30,7 @@ func (*VersionCmd) Run(ctx context.Context) error {
runtime.Version(),
})
if err != nil {
return err
return "", err
}
_, err = fmt.Println(string(v))
return err
return string(v), nil
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/smlx/go-cli-github

go 1.22.2
go 1.27

require (
github.com/alecthomas/assert/v2 v2.11.0
Expand Down
Loading