Skip to content

Commit 32afd8f

Browse files
committed
feat: add version flag and subcommand
1 parent 3ea3b15 commit 32afd8f

4 files changed

Lines changed: 55 additions & 3 deletions

File tree

.github/workflows/build-test.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ jobs:
2222
go-version-file: 'go.mod'
2323

2424
- name: Build
25-
run: go build -v ./...
25+
run: |
26+
VERSION=$(git describe --tags --always --dirty 2>/dev/null || echo "dev-$(git rev-parse --short HEAD)")
27+
go build -v -ldflags "-X github.com/xbglowx/github-org-repos-sync/cmd.Version=$VERSION" ./...
2628
2729
- name: Test
2830
run: go test -v ./...

.github/workflows/create-release.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ jobs:
4040
go-version-file: 'go.mod'
4141

4242
- name: Build
43-
run: GOOS=${{ matrix.goos }} GOARCH=${{ matrix.goarch }} go build -v -o github-org-repos-sync-${{ matrix.goos }}-${{ matrix.goarch }}
43+
run: GOOS=${{ matrix.goos }} GOARCH=${{ matrix.goarch }} go build -v -ldflags "-X github.com/xbglowx/github-org-repos-sync/cmd.Version=${{ needs.release-please.outputs.tag_name }}" -o github-org-repos-sync-${{ matrix.goos }}-${{ matrix.goarch }}
4444

4545
- name: Upload artifact
4646
uses: actions/upload-artifact@v4

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,17 @@ Download the latest release for your platform from the [releases page](https://g
3434
go build .
3535
```
3636

37+
**Optional**: Build with version from git:
38+
```bash
39+
VERSION=$(git describe --tags --always --dirty 2>/dev/null || echo "dev-$(git rev-parse --short HEAD)")
40+
go build -ldflags "-X github.com/xbglowx/github-org-repos-sync/cmd.Version=$VERSION" .
41+
```
42+
43+
**Optional**: Build with custom version:
44+
```bash
45+
go build -ldflags "-X github.com/xbglowx/github-org-repos-sync/cmd.Version=1.0.0" .
46+
```
47+
3748
## Prerequisites
3849

3950
### Authentication
@@ -95,6 +106,16 @@ Download the latest release for your platform from the [releases page](https://g
95106
./github-org-repos-sync --help
96107
```
97108

109+
#### Check version
110+
```bash
111+
# Using version subcommand
112+
./github-org-repos-sync version
113+
114+
# Using version flag
115+
./github-org-repos-sync --version
116+
./github-org-repos-sync -v
117+
```
118+
98119
## How It Works
99120

100121
1. **Repository Discovery**: Fetches a list of all repositories in the specified organization that you have access to

cmd/root.go

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ import (
2424
"github.com/spf13/cobra"
2525
)
2626

27+
// Version can be set at build time using ldflags:
28+
// go build -ldflags "-X github.com/xbglowx/github-org-repos-sync/cmd.Version=1.0.0"
29+
var Version = "dev"
30+
2731
var destPath string
2832
var excludeRepoString string
2933
var includeRepoString string
@@ -62,25 +66,50 @@ var rootCmd = &cobra.Command{
6266
Long: "Sync github org repos.",
6367

6468
PreRunE: func(cmd *cobra.Command, args []string) error {
69+
// Check if version flag is set
70+
if versionFlag, _ := cmd.Flags().GetBool("version"); versionFlag {
71+
fmt.Printf("github-org-repos-sync version %s\n", Version)
72+
os.Exit(0)
73+
}
6574
return checkRequirements()
6675
},
6776
Run: func(cmd *cobra.Command, args []string) {
6877
main(args)
6978
},
70-
Args: cobra.ExactArgs(1),
79+
Args: func(cmd *cobra.Command, args []string) error {
80+
// If version flag is set, don't require args
81+
if versionFlag, _ := cmd.Flags().GetBool("version"); versionFlag {
82+
return nil
83+
}
84+
return cobra.ExactArgs(1)(cmd, args)
85+
},
7186
Example: "github-org-repos-sync floorpunch",
7287
}
7388

89+
// versionCmd represents the version command
90+
var versionCmd = &cobra.Command{
91+
Use: "version",
92+
Short: "Print the version number",
93+
Long: "Print the version number of github-org-repos-sync",
94+
Run: func(cmd *cobra.Command, args []string) {
95+
fmt.Printf("github-org-repos-sync version %s\n", Version)
96+
},
97+
}
98+
7499
// Execute adds all child commands to the root command and sets flags appropriately.
75100
// This is called by main.main(). It only needs to happen once to the rootCmd.
76101
func Execute() {
77102
cobra.CheckErr(rootCmd.Execute())
78103
}
79104

80105
func init() {
106+
rootCmd.AddCommand(versionCmd)
81107
rootCmd.Flags().BoolVar(&skipArchived, "skip-archived", false, "Skip archived repos?")
82108
rootCmd.Flags().StringVar(&excludeRepoString, "exclude-repos", "", "Exclude repos that contain string")
83109
rootCmd.Flags().StringVarP(&destPath, "destination-path", "d", ".", "Destionation path for repos")
84110
rootCmd.Flags().StringVar(&includeRepoString, "include-repos", "", "Include only repos that contain string")
85111
rootCmd.Flags().IntVarP(&parallelism, "parallelism", "p", 1, "Number of parallel git operations")
112+
113+
// Add version flag that prints version and exits
114+
rootCmd.Flags().BoolP("version", "v", false, "Print version information")
86115
}

0 commit comments

Comments
 (0)