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
15 changes: 13 additions & 2 deletions cmd/toolkit-assume-role/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,21 @@ import (
"os"

"github.com/kxue43/cli-toolkit/auth"
"github.com/kxue43/cli-toolkit/version"
)

var (
logger = log.New(os.Stderr, "toolkit-assume-role: ", 0)
logger = log.New(os.Stderr, "toolkit-assume-role: ", 0)

roleArn string
mfaSerial string
profile string
region string
roleSessionName string
durationSeconds int
helpMsg = `Usage: %s -mfa-serial=STRING -profile=STRING [flags] <RoleArn>
showVersion bool

helpMsg = `Usage: %s -mfa-serial=STRING -profile=STRING [flags] <RoleArn>

Run AWS CLI credential process by assuming a role.

Expand All @@ -34,6 +38,7 @@ func registerFlagsAndHelp() {
flag.StringVar(&region, "region", "us-east-1", "The regional STS service endpoint to call.")
flag.StringVar(&roleSessionName, "role-session-name", "ToolkitCLI", "Role session name.")
flag.IntVar(&durationSeconds, "duration-seconds", 3600, "Role session duration seconds.")
flag.BoolVar(&showVersion, "version", false, "Show version information and quit.")

flag.Usage = func() {
_, _ = fmt.Fprintf(flag.CommandLine.Output(), helpMsg, os.Args[0])
Expand Down Expand Up @@ -67,6 +72,12 @@ func main() {

flag.Parse()

if showVersion {
_, _ = fmt.Fprintln(flag.CommandLine.Output(), version.FromBuildInfo())

os.Exit(0)
}

validateInputs()

tty, err := os.OpenFile("/dev/tty", os.O_RDWR|os.O_SYNC, 0600)
Expand Down
16 changes: 14 additions & 2 deletions cmd/toolkit-serve-static/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,16 @@ import (
"log"
"net/http"
"os"

"github.com/kxue43/cli-toolkit/version"
)

var (
port string
logger = log.New(os.Stderr, "toolkit-serve-static: ", 0)
logger = log.New(os.Stderr, "toolkit-serve-static: ", 0)

port string
showVersion bool

helpMsg = `Usage: %s [flags] <DIR>

Serve static contents from the <DIR> folder.
Expand All @@ -24,6 +29,7 @@ Flags:

func main() {
flag.StringVar(&port, "port", "8090", "HTTP port of the local static files server.")
flag.BoolVar(&showVersion, "version", false, "Show version information and quit.")

flag.Usage = func() {
_, _ = fmt.Fprintf(flag.CommandLine.Output(), helpMsg, os.Args[0])
Expand All @@ -32,6 +38,12 @@ func main() {

flag.Parse()

if showVersion {
_, _ = fmt.Fprintln(flag.CommandLine.Output(), version.FromBuildInfo())

os.Exit(0)
}

args := flag.Args()
if len(args) == 0 {
logger.Fatalf("The <DIR> argument is required.")
Expand Down
20 changes: 18 additions & 2 deletions cmd/toolkit-show-md/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,33 +13,49 @@ import (
"github.com/pkg/browser"
"github.com/yuin/goldmark"
"github.com/yuin/goldmark/extension"

"github.com/kxue43/cli-toolkit/version"
)

var (
logger = log.New(os.Stderr, "toolkit-show-md: ", 0)

//go:embed .github.style.tmplt
gitHubMarkdownTemplate []byte

showVersion bool

helpMsg = `Usage: %s <PATH>

Convert Markdown file to GitHub style HTML and display HTML in the default browser.

Arguments:
<PATH> Path to the Markdown file to convert.

Flags:
`
//go:embed .github.style.tmplt
gitHubMarkdownTemplate []byte
)

func main() {
var stat os.FileInfo

var err error

flag.BoolVar(&showVersion, "version", false, "Show version information and quit.")

flag.Usage = func() {
_, _ = fmt.Fprintf(flag.CommandLine.Output(), helpMsg, os.Args[0])
flag.PrintDefaults()
}

flag.Parse()

if showVersion {
_, _ = fmt.Fprintln(flag.CommandLine.Output(), version.FromBuildInfo())

os.Exit(0)
}

args := flag.Args()
if len(args) != 1 {
logger.Fatalln("Exactly one argument is permitted and it is <PATH>.")
Expand Down
5 changes: 4 additions & 1 deletion cmd/toolkit/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,24 @@ import (
"github.com/alecthomas/kong"

"github.com/kxue43/cli-toolkit/scaffold"
"github.com/kxue43/cli-toolkit/version"
)

func main() {
var cli struct {
StartTsCdkProject scaffold.TsCdkProjectCmd `cmd:"" name:"start-ts-cdk-project" help:"Start a TypeScript CDK project in the current directory."`
StartPythonProject scaffold.PythonProjectCmd `cmd:"" name:"start-python-project" help:"Start a Python project in the current directory."`
StartGoProject scaffold.GoProjectCmd `cmd:"" name:"start-go-project" help:"Start a Go project in the current directory."`
Version kong.VersionFlag `name:"version" short:"v" help:"Show version information and quit."`
}

ctx := kong.Parse(
&cli,
kong.Name("toolkit"),
kong.Description("Personal CLI toolkit."),
kong.Description("Create starter files for Go, Python or AWS CDK projects."),
kong.UsageOnError(),
kong.ConfigureHelp(kong.HelpOptions{Compact: true}),
kong.Vars{"version": version.FromBuildInfo()},
)

err := ctx.Run()
Expand Down
40 changes: 40 additions & 0 deletions version/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package version

import (
"fmt"
"runtime/debug"
)

func FromBuildInfo() (version string) {
version = "unavailable"

info, ok := debug.ReadBuildInfo()
if !ok {
return version
}

var vcs, revision, ts string

for i := range info.Settings {
switch info.Settings[i].Key {
case "vcs":
vcs = info.Settings[i].Value
case "vcs.revision":
revision = info.Settings[i].Value
case "vcs.time":
ts = info.Settings[i].Value
default:
continue
}
}

if revision == "" {
return version
}

if ts == "" {
return fmt.Sprintf("built from %s revision %s", vcs, revision)
}

return fmt.Sprintf("built from %s revision %s at %s", vcs, revision, ts)
}