Skip to content

Commit

Permalink
Add version command, print help on base cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
coadler committed Aug 24, 2024
1 parent ecf46fd commit 32f2838
Show file tree
Hide file tree
Showing 7 changed files with 152 additions and 14 deletions.
6 changes: 3 additions & 3 deletions .goreleaser.yaml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
builds:
- dir: "./cmd/wush"
- main: "./cmd/wush"
env:
- CGO_ENABLED=0
mod_timestamp: "{{ .CommitTimestamp }}"
flags:
- -trimpath
ldflags:
- "-s -w -X main.version={{.Version}} -X main.commit={{.Commit}}"
- "-s -w -X main.version={{.Version}} -X main.commit={{.Commit}} -X main.commitDate={{.CommitTimestamp}}"
goos:
- freebsd
- windows
Expand All @@ -32,7 +32,7 @@ nfpms:
description: |-
Wush installer package.
Wush creates secure Wireguard tunnels between two devices.
license: CC0
license: CC0-1.0
contents:
- src: LICENSE
dst: "/usr/share/doc/{{ .ProjectName }}/copyright"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ On the client machine:
```bash
$ wush
┃ Enter the receiver's Auth key:
| > 112v1RyL5KPzsbMbhT7fkEGrcfpygxtnvwjR5kMLGxDHGeLTK1BvoPqsUcjo7xyMkFn46KLTdedKuPCG5trP84mz9kx
> 112v1RyL5KPzsbMbhT7fkEGrcfpygxtnvwjR5kMLGxDHGeLTK1BvoPqsUcjo7xyMkFn46KLTdedKuPCG5trP84mz9kx
Auth information:
> Server overlay STUN address: Disabled
> Server overlay DERP home: Toronto
Expand Down
118 changes: 114 additions & 4 deletions cmd/wush/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,129 @@ package main
import (
"fmt"
"os"
"strconv"
"strings"
"time"

"github.com/coder/pretty"
"github.com/coder/serpent"
"github.com/coder/wush/cliui"
"github.com/mitchellh/go-wordwrap"
)

func main() {
cmd := sshCmd()
cmd.Children = []*serpent.Command{
receiveCmd(),
rsyncCmd(),
var (
showVersion bool

fmtLong = "wush %s - peer-to-peer file transfers and shells\n"
)
cmd := &serpent.Command{
Use: "wush <subcommand>",
Long: fmt.Sprintf(fmtLong, getBuildInfo().version) + formatExamples(
example{
Description: "Start the wush server",
Command: "wush receive",
},
example{
Description: "Open a shell to the wush host",
Command: "wush ssh",
},
example{
Description: "Transfer files to the wush host using rsync",
Command: "wush rsync local-file.txt :/path/to/remote/file",
},
),
Handler: func(i *serpent.Invocation) error {
if showVersion {
return versionCmd().Handler(i)
}
return serpent.DefaultHelpFn()(i)
},
Children: []*serpent.Command{
versionCmd(),
sshCmd(),
receiveCmd(),
rsyncCmd(),
},
Options: []serpent.Option{
{
Flag: "version",
Description: "Print the version and exit.",
Value: serpent.BoolOf(&showVersion),
},
},
}

err := cmd.Invoke().WithOS().Run()
if err != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", err)
os.Exit(1)
}
}

// example represents a standard example for command usage, to be used
// with formatExamples.
type example struct {
Description string
Command string
}

// formatExamples formats the examples as width wrapped bulletpoint
// descriptions with the command underneath.
func formatExamples(examples ...example) string {
var sb strings.Builder

padStyle := cliui.DefaultStyles.Wrap.With(pretty.XPad(4, 0))
for i, e := range examples {
if len(e.Description) > 0 {
wordwrap.WrapString(e.Description, 80)
_, _ = sb.WriteString(
" - " + pretty.Sprint(padStyle, e.Description+":")[4:] + "\n\n ",
)
}
// We add 1 space here because `cliui.DefaultStyles.Code` adds an extra
// space. This makes the code block align at an even 2 or 6
// spaces for symmetry.
_, _ = sb.WriteString(" " + pretty.Sprint(cliui.DefaultStyles.Code, fmt.Sprintf("$ %s", e.Command)))
if i < len(examples)-1 {
_, _ = sb.WriteString("\n\n")
}
}
return sb.String()
}

var (
version string
commit string
commitDate string
)

type buildInfo struct {
version string
commitHash string
commitTime time.Time
}

func getBuildInfo() buildInfo {
bi := buildInfo{
version: "v0.0.0-devel",
commitHash: "0000000000000000000000000000000000000000",
commitTime: time.Now(),
}

if version != "" {
bi.version = version
}
if commit != "" {
bi.commitHash = commit
}
if commitDate != "" {
dateUnix, err := strconv.ParseInt(commitDate, 10, 64)
if err != nil {
panic("invalid commitDate: " + err.Error())
}
bi.commitTime = time.Unix(dateUnix, 0)
}

return bi
}
4 changes: 2 additions & 2 deletions cmd/wush/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ func sshCmd() *serpent.Command {
sshStdio bool
)
return &serpent.Command{
Use: "wush",
Aliases: []string{"ssh"},
Use: "ssh",
Aliases: []string{},
Long: "Opens an SSH connection to a " + cliui.Code("wush") + " peer. " +
"Use " + cliui.Code("wush receive") + " on the computer you would like to connect to.",
Handler: func(inv *serpent.Invocation) error {
Expand Down
24 changes: 24 additions & 0 deletions cmd/wush/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package main

import (
"fmt"
"time"

"github.com/coder/serpent"
)

func versionCmd() *serpent.Command {
cmd := &serpent.Command{
Use: "version",
Short: "Show wush version",
Handler: func(inv *serpent.Invocation) error {
bi := getBuildInfo()
fmt.Printf("Wush %s-%s %s\n", bi.version, bi.commitHash[:7], bi.commitTime.Format(time.RFC1123))
fmt.Printf("https://github.com/coder/wush/commit/%s\n", commit)
return nil
},
Options: serpent.OptionSet{},
}

return cmd
}
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@ require (
github.com/charmbracelet/huh v0.4.2
github.com/coder/coder/v2 v2.14.1
github.com/coder/pretty v0.0.0-20230908205945-e89ba86370e0
github.com/coder/serpent v0.7.0
github.com/coder/serpent v0.7.1-0.20240823180923-556f4d27e175
github.com/go-chi/chi/v5 v5.1.0
github.com/google/uuid v1.6.0
github.com/klauspost/compress v1.17.9
github.com/mattn/go-isatty v0.0.20
github.com/mitchellh/go-wordwrap v1.0.1
github.com/muesli/termenv v0.15.2
github.com/pion/stun/v3 v3.0.0
github.com/prometheus/client_golang v1.19.1
Expand Down Expand Up @@ -141,7 +142,6 @@ require (
github.com/mitchellh/copystructure v1.2.0 // indirect
github.com/mitchellh/go-ps v1.0.0 // indirect
github.com/mitchellh/go-testing-interface v1.14.1 // indirect
github.com/mitchellh/go-wordwrap v1.0.1 // indirect
github.com/mitchellh/mapstructure v1.5.1-0.20231216201459-8508981c8b6c // indirect
github.com/mitchellh/reflectwalk v1.0.2 // indirect
github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6 // indirect
Expand Down
8 changes: 6 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -153,8 +153,8 @@ github.com/coder/coder/v2 v2.14.1 h1:tSYe7H4pNRL8hh9ynwHK7QYTOvG5hcuZJEOkn4ZPASE
github.com/coder/coder/v2 v2.14.1/go.mod h1:dO79BI5XlP8rrtne1JpRcVehe27bNMXdZKyn1NsWbjA=
github.com/coder/pretty v0.0.0-20230908205945-e89ba86370e0 h1:3A0ES21Ke+FxEM8CXx9n47SZOKOpgSE1bbJzlE4qPVs=
github.com/coder/pretty v0.0.0-20230908205945-e89ba86370e0/go.mod h1:5UuS2Ts+nTToAMeOjNlnHFkPahrtDkmpydBen/3wgZc=
github.com/coder/serpent v0.7.0 h1:zGpD2GlF3lKIVkMjNGKbkip88qzd5r/TRcc30X/SrT0=
github.com/coder/serpent v0.7.0/go.mod h1:REkJ5ZFHQUWFTPLExhXYZ1CaHFjxvGNRlLXLdsI08YA=
github.com/coder/serpent v0.7.1-0.20240823180923-556f4d27e175 h1:/YekbSbvOFGlkfnDC8VMIV3fWZC7jm1i+RiEDs12fWs=
github.com/coder/serpent v0.7.1-0.20240823180923-556f4d27e175/go.mod h1:cZFW6/fP+kE9nd/oRkEHJpG6sXCtQ+AX7WMMEHv0Y3Q=
github.com/coder/ssh v0.0.0-20231128192721-70855dedb788 h1:YoUSJ19E8AtuUFVYBpXuOD6a/zVP3rcxezNsoDseTUw=
github.com/coder/ssh v0.0.0-20231128192721-70855dedb788/go.mod h1:aGQbuCLyhRLMzZF067xc84Lh7JDs1FKwCmF1Crl9dxQ=
github.com/coder/terraform-provider-coder v0.23.0 h1:DuNLWxhnGlXyG0g+OCAZRI6xd8+bJjIEnE4F3hYgA4E=
Expand Down Expand Up @@ -376,6 +376,8 @@ github.com/miekg/dns v1.1.58 h1:ca2Hdkz+cDg/7eNF6V56jjzuZ4aCAE+DbVkILdQWG/4=
github.com/miekg/dns v1.1.58/go.mod h1:Ypv+3b/KadlvW9vJfXOTf300O4UqaHFzFCuHz+rPkBY=
github.com/mitchellh/copystructure v1.2.0 h1:vpKXTN4ewci03Vljg/q9QvCGUDttBOGBIa15WveJJGw=
github.com/mitchellh/copystructure v1.2.0/go.mod h1:qLl+cE2AmVv+CoeAwDPye/v+N2HKCj9FbZEVFJRxO9s=
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
github.com/mitchellh/go-ps v1.0.0 h1:i6ampVEEF4wQFF+bkYfwYgY+F/uYJDktmvLPf7qIgjc=
github.com/mitchellh/go-ps v1.0.0/go.mod h1:J4lOc8z8yJs6vUwklHw2XEIiT4z4C40KtWVN3nvg8Pg=
github.com/mitchellh/go-testing-interface v1.14.1 h1:jrgshOhYAUVNMAJiKbEu7EqAwgJJ2JqpQmpLJOu07cU=
Expand All @@ -394,6 +396,8 @@ github.com/muesli/cancelreader v0.2.2 h1:3I4Kt4BQjOR54NavqnDogx/MIoWBFa0StPA8ELU
github.com/muesli/cancelreader v0.2.2/go.mod h1:3XuTXfFS2VjM+HTLZY9Ak0l6eUKfijIfMUZ4EgX0QYo=
github.com/muesli/termenv v0.15.2 h1:GohcuySI0QmI3wN8Ok9PtKGkgkFIk7y6Vpb5PvrY+Wo=
github.com/muesli/termenv v0.15.2/go.mod h1:Epx+iuz8sNs7mNKhxzH4fWXGNpZwUaJKRS1noLXviQ8=
github.com/natefinch/atomic v1.0.1 h1:ZPYKxkqQOx3KZ+RsbnP/YsgvxWQPGxjC0oBt2AhwV0A=
github.com/natefinch/atomic v1.0.1/go.mod h1:N/D/ELrljoqDyT3rZrsUmtsuzvHkeB/wWjHV22AZRbM=
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646 h1:zYyBkD/k9seD2A7fsi6Oo2LfFZAehjjQMERAvZLEDnQ=
github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646/go.mod h1:jpp1/29i3P1S/RLdc7JQKbRpFeM1dOBd8T9ki5s+AY8=
github.com/nsf/jsondiff v0.0.0-20200515183724-f29ed568f4ce h1:RPclfga2SEJmgMmz2k+Mg7cowZ8yv4Trqw9UsJby758=
Expand Down

0 comments on commit 32f2838

Please sign in to comment.