Skip to content

Commit 0e90354

Browse files
committed
Add version command
1 parent 843bc5c commit 0e90354

4 files changed

Lines changed: 40 additions & 2 deletions

File tree

cmd/docksider/main.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,24 @@ package main
22

33
import (
44
"context"
5+
_ "embed"
56
"log"
67
"os"
78
"os/signal"
89

910
"github.com/openclosed-dev/docksider/internal/cmd"
1011
)
1112

13+
//go:embed version.txt
14+
var version string
15+
1216
func main() {
1317

1418
log.SetFlags(0)
1519

1620
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
1721
defer cancel()
18-
if err := cmd.NewRootCmd().ExecuteContext(ctx); err != nil {
22+
if err := cmd.NewRootCmd(version).ExecuteContext(ctx); err != nil {
1923
cancel()
2024
os.Exit(1)
2125
}

cmd/docksider/version.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
0.1.0

internal/cmd/root.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,25 @@
11
package cmd
22

33
import (
4+
"strings"
5+
46
"github.com/openclosed-dev/docksider/internal/cmd/image"
57
"github.com/spf13/cobra"
68
)
79

8-
func NewRootCmd() *cobra.Command {
10+
func NewRootCmd(version string) *cobra.Command {
911

1012
const (
1113
use = "docksider [OPTIONS] COMMAND [ARG...]"
1214
short = "A Docker-style CLI for pulling and pushing images directly to container registries"
1315
)
1416

17+
version = strings.TrimSpace(version)
18+
1519
root := &cobra.Command{
1620
Use: use,
1721
Short: short,
22+
Version: version,
1823
SilenceUsage: true,
1924
SilenceErrors: false,
2025

@@ -33,6 +38,7 @@ func NewRootCmd() *cobra.Command {
3338
image.NewImagesCmd(),
3439
image.NewPullCmd(),
3540
image.NewPushCmd(),
41+
NewVersionCmd(version),
3642
)
3743

3844
return root

internal/cmd/version.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/spf13/cobra"
7+
)
8+
9+
func NewVersionCmd(version string) *cobra.Command {
10+
11+
const (
12+
use = "version"
13+
short = "Print the version"
14+
)
15+
16+
cmd := &cobra.Command{
17+
Use: use,
18+
Short: short,
19+
RunE: func(cmd *cobra.Command, _ []string) error {
20+
fmt.Fprintln(cmd.OutOrStdout(), version)
21+
return nil
22+
},
23+
DisableFlagsInUseLine: true,
24+
}
25+
26+
return cmd
27+
}

0 commit comments

Comments
 (0)