Skip to content

Commit 7139fb6

Browse files
committed
Fix test failure
1 parent c4e2981 commit 7139fb6

File tree

10 files changed

+54
-86
lines changed

10 files changed

+54
-86
lines changed

cmd/create.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,3 @@ func newCreateCmd(workspaceManager workspaceManager) *cobra.Command {
3535
},
3636
}
3737
}
38-
39-
func init() {
40-
rootCmd.AddCommand(newCreateCmd(newWorkspaceManager()))
41-
}

cmd/dependencies.go

Lines changed: 0 additions & 31 deletions
This file was deleted.

cmd/edit.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,3 @@ func newEditCmd(workspaceManager workspaceManager) *cobra.Command {
3939
},
4040
}
4141
}
42-
43-
func init() {
44-
rootCmd.AddCommand(
45-
newEditCmd(newWorkspaceManager()),
46-
)
47-
}

cmd/list.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,3 @@ func newListCmd(workspaceManager workspaceManager) *cobra.Command {
4545
},
4646
}
4747
}
48-
49-
func init() {
50-
rootCmd.AddCommand(newListCmd(newWorkspaceManager()))
51-
}

cmd/load.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import (
77

88
var env string
99

10-
func newloadCmd(workspaceManager workspaceManager) *cobra.Command {
11-
return &cobra.Command{
10+
func newLoadCmd(workspaceManager workspaceManager) *cobra.Command {
11+
loadCmd := &cobra.Command{
1212
Use: "load workspace",
1313
Aliases: []string{"l"},
1414
Short: "Load a workspace",
@@ -29,10 +29,6 @@ func newloadCmd(workspaceManager workspaceManager) *cobra.Command {
2929
return workspaceManager.Load(args[0], env)
3030
},
3131
}
32-
}
33-
34-
func init() {
35-
loadCmd := newloadCmd(newWorkspaceManager())
3632
loadCmd.Flags().StringVarP(&env, "env", "e", "", "Environment to use (e.g. prod)")
37-
rootCmd.AddCommand(loadCmd)
33+
return loadCmd
3834
}

cmd/remove.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,3 @@ func newRemoveCmd(workspaceManager workspaceManager) *cobra.Command {
2727
},
2828
}
2929
}
30-
31-
func init() {
32-
rootCmd.AddCommand(newRemoveCmd(newWorkspaceManager()))
33-
}

cmd/root.go

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,58 @@
11
package cmd
22

33
import (
4+
"log"
45
"os"
56

7+
"github.com/antham/wo/workspace"
68
"github.com/spf13/cobra"
9+
"github.com/spf13/viper"
710
)
811

9-
// rootCmd represents the base command when called without any subcommands
10-
var rootCmd = &cobra.Command{
11-
Use: "wo",
12-
Short: "Manage workspace in shell",
12+
func newRootCmd() *cobra.Command {
13+
rootCmd := &cobra.Command{
14+
Use: "wo",
15+
Short: "Manage workspace in shell",
16+
}
17+
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
18+
w, err := newWorkspaceManager()
19+
if err != nil {
20+
log.Fatal(err)
21+
}
22+
23+
rootCmd.AddCommand(newCreateCmd(w))
24+
rootCmd.AddCommand(newEditCmd(w))
25+
rootCmd.AddCommand(newListCmd(w))
26+
rootCmd.AddCommand(newLoadCmd(w))
27+
rootCmd.AddCommand(newRemoveCmd(w))
28+
rootCmd.AddCommand(newRunCmd(w))
29+
rootCmd.AddCommand(newShowCmd(w))
30+
rootCmd.AddCommand(newVersionCmd())
31+
return rootCmd
32+
}
33+
34+
func newWorkspaceManager() (workspaceManager, error) {
35+
viper.AutomaticEnv()
36+
options := []func(*workspace.WorkspaceManager){
37+
workspace.WithEditor(viper.GetString("EDITOR"), viper.GetString("VISUAL")),
38+
workspace.WithShellPath(viper.GetString("SHELL")),
39+
}
40+
viper.SetEnvPrefix("WO")
41+
if viper.IsSet("CONFIG_PATH") {
42+
options = append(options, workspace.WithConfigPath(viper.GetString("CONFIG_PATH")))
43+
}
44+
wksManager, err := workspace.NewWorkspaceManager(options...)
45+
if err != nil {
46+
return nil, err
47+
}
48+
return wksManager, nil
1349
}
1450

1551
// Execute adds all child commands to the root command and sets flags appropriately.
1652
// This is called by main.main(). It only needs to happen once to the rootCmd.
1753
func Execute() {
18-
err := rootCmd.Execute()
54+
err := newRootCmd().Execute()
1955
if err != nil {
2056
os.Exit(1)
2157
}
2258
}
23-
24-
func init() {
25-
rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
26-
}

cmd/run.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
)
1010

1111
func newRunCmd(workspaceManager workspaceManager) *cobra.Command {
12-
return &cobra.Command{
12+
runCmd := &cobra.Command{
1313
Use: "run workspace function [function-args]...",
1414
Aliases: []string{"r"},
1515
Short: "Run a function in a given workspace",
@@ -40,10 +40,6 @@ func newRunCmd(workspaceManager workspaceManager) *cobra.Command {
4040
return nil
4141
},
4242
}
43-
}
44-
45-
func init() {
46-
runCmd := newRunCmd(newWorkspaceManager())
4743
runCmd.Flags().StringVarP(&env, "env", "e", "", "Environment to use (e.g. prod)")
48-
rootCmd.AddCommand(runCmd)
44+
return runCmd
4945
}

cmd/show.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,3 @@ func newShowCmd(workspaceManager workspaceManager) *cobra.Command {
9191
},
9292
}
9393
}
94-
95-
func init() {
96-
rootCmd.AddCommand(newShowCmd(newWorkspaceManager()))
97-
}

cmd/version.go

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,12 @@ import (
88

99
var appVersion = "dev"
1010

11-
// versionCmd represents the version command
12-
var versionCmd = &cobra.Command{
13-
Use: "version",
14-
Short: "Version",
15-
Run: func(cmd *cobra.Command, args []string) {
16-
fmt.Println(appVersion)
17-
},
18-
}
19-
20-
func init() {
21-
rootCmd.AddCommand(versionCmd)
11+
func newVersionCmd() *cobra.Command {
12+
return &cobra.Command{
13+
Use: "version",
14+
Short: "Version",
15+
Run: func(cmd *cobra.Command, args []string) {
16+
fmt.Println(appVersion)
17+
},
18+
}
2219
}

0 commit comments

Comments
 (0)