Skip to content

Commit a11d0b5

Browse files
committed
Adds changes to support --version and version as a command
1 parent 05e15a8 commit a11d0b5

4 files changed

Lines changed: 53 additions & 13 deletions

File tree

.github/workflows/release.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,12 @@ jobs:
2222
with:
2323
go-version: '1.24.1'
2424

25+
- name: Set VERSION from tag
26+
id: get_version
27+
run: echo "VERSION=${GITHUB_REF#refs/tags/}" >> $GITHUB_ENV
28+
2529
- name: Build the project
26-
run: go build -o build/dloom
30+
run: go build -ldflags "-X github.com/dloomorg/dloom/cmd.Version=${VERSION}" -o build/dloom
2731

2832
- name: Check if prerelease
2933
id: prerelease

cmd/root.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import (
99
"path/filepath"
1010
)
1111

12+
var Version = "dev"
13+
1214
var (
1315
cfg *internal.Config
1416
logger *logging.Logger
@@ -26,6 +28,14 @@ var (
2628
var rootCmd = &cobra.Command{
2729
Use: "dloom",
2830
Short: "Dotfile manager and system bootstrapper",
31+
RunE: func(cmd *cobra.Command, args []string) error {
32+
if v, _ := cmd.Flags().GetBool("version"); v {
33+
fmt.Println("dloom version:", Version)
34+
return nil
35+
}
36+
// If no version flag, show help
37+
return cmd.Help()
38+
},
2939
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
3040
logger = &logging.Logger{UseColors: !noColor}
3141

@@ -70,7 +80,7 @@ func Execute() {
7080
}
7181

7282
func init() {
73-
// Persistent Flags (global)
83+
rootCmd.PersistentFlags().BoolP("version", "V", false, "Print the version of dloom and exit")
7484
rootCmd.PersistentFlags().StringVarP(&configPath, "config", "c", "", "Path to config file")
7585
rootCmd.PersistentFlags().BoolVar(&noColor, "no-color", false, "Disable color output")
7686
rootCmd.PersistentFlags().BoolVarP(&force, "force", "f", false, "Force overwrite existing files")

cmd/version.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package cmd
2+
3+
import (
4+
"fmt"
5+
"github.com/spf13/cobra"
6+
)
7+
8+
var versionCmd = &cobra.Command{
9+
Use: "version",
10+
Short: "Print the version of dloom",
11+
Run: func(cmd *cobra.Command, args []string) {
12+
fmt.Println("dloom version: ", Version)
13+
},
14+
}
15+
16+
func init() {
17+
rootCmd.AddCommand(versionCmd)
18+
}

internal/config.go

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,13 @@ func DefaultConfig() *Config {
6060
}
6161

6262
return &Config{
63-
SourceDir: ".", // Current directory by default
64-
TargetDir: homeDir, // User's home directory by default
65-
BackupDir: filepath.Join(homeDir, ".dloom/backups"),
66-
Force: false,
67-
Verbose: false,
68-
DryRun: false,
69-
Packages: make(map[string]*PackageConfig),
63+
SourceDir: ".", // Current directory by default
64+
TargetDir: homeDir, // User's home directory by default
65+
BackupDir: filepath.Join(homeDir, ".dloom/backups"),
66+
Force: false,
67+
Verbose: false,
68+
DryRun: false,
69+
Packages: make(map[string]*PackageConfig),
7070
IgnorePackages: []string{
7171
".git",
7272
".idea",
@@ -82,19 +82,27 @@ func LoadConfig(configPath string, logger *logging.Logger) (*Config, error) {
8282
// If no config path specified, look in default locations
8383
// First try to find a
8484
if configPath == "" {
85-
logger.LogTrace("No config file path specified, using defaults")
85+
if config.Verbose {
86+
logger.LogTrace("No config file path specified, using defaults")
87+
}
8688
// First, try current directory and see if a dloom/config.yaml exists
8789
// If not, try ~/.config/dloom/config.yaml
8890
currentDir, err := os.Getwd()
8991
if err == nil {
9092
configPath = filepath.Join(currentDir, "dloom", "config.yaml")
91-
logger.LogTrace("Attempting to load config file: %s", configPath)
93+
if config.Verbose {
94+
logger.LogTrace("Attempting to load config file: %s", configPath)
95+
}
9296
if _, err := os.Stat(configPath); os.IsNotExist(err) {
9397
// Next, try ~/.config/dloom/config.yaml
9498
homeDir, err := os.UserHomeDir()
9599
if err == nil {
96-
configPath = filepath.Join(homeDir, ".config", "dloom", "config.yaml")
97-
logger.LogTrace("Attempting to load config file: %s", configPath)
100+
if config.Verbose {
101+
configPath = filepath.Join(homeDir, ".config", "dloom", "config.yaml")
102+
}
103+
if config.Verbose {
104+
logger.LogTrace("Attempting to load config file: %s", configPath)
105+
}
98106
if _, err := os.Stat(configPath); os.IsNotExist(err) {
99107
// No config file found, use defaults
100108
return config, nil

0 commit comments

Comments
 (0)