Skip to content

Commit

Permalink
Merge pull request #30 from antham/move-workspace-config
Browse files Browse the repository at this point in the history
Move the workspace config
  • Loading branch information
antham authored Jul 16, 2024
2 parents 67c2ec9 + 01e69ad commit 162d5a8
Show file tree
Hide file tree
Showing 10 changed files with 129 additions and 43 deletions.
2 changes: 1 addition & 1 deletion e2e-scripts/init.bash
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ echo '
hello() {
echo "Hello world !"
}
' > ~/.config/wo/api/functions/functions.bash
' > ~/.config/wo/workspaces/api/functions/functions.bash
}
2 changes: 1 addition & 1 deletion e2e-scripts/init.fish
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ function create_function
function hello -d "Hello world function"
echo "Hello world !"
end
' >~/.config/wo/api/functions/functions.fish
' >~/.config/wo/workspaces/api/functions/functions.fish
end
2 changes: 1 addition & 1 deletion e2e-scripts/init.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ echo '
hello() {
echo "Hello world !"
}
' > ~/.config/wo/api/functions/functions.sh
' > ~/.config/wo/workspaces/api/functions/functions.sh
}
2 changes: 1 addition & 1 deletion e2e-scripts/init.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ echo '
hello() {
echo "Hello world !"
}
' > ~/.config/wo/api/functions/functions.zsh
' > ~/.config/wo/workspaces/api/functions/functions.zsh
}
1 change: 1 addition & 0 deletions internal/cmd/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type workspaceManager interface {
SetConfig(string, map[string]string) error
GetSupportedApps() []string
GetConfigDir() string
Migrate() error
}

type completionManager interface {
Expand Down
21 changes: 21 additions & 0 deletions internal/cmd/migrate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package cmd

import (
"github.com/spf13/cobra"
)

func newMigrateCmd(workspaceManager workspaceManager) *cobra.Command {
return &cobra.Command{
Use: "migrate",
Short: "Migrate the existing config",
Hidden: true,
RunE: func(cmd *cobra.Command, args []string) error {
err := workspaceManager.Migrate()
if err != nil {
return err
}
cmd.Printf(regularStyle.Render("Config migrated"))
return nil
},
}
}
18 changes: 18 additions & 0 deletions internal/cmd/mock_workspaceManager_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions internal/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ func newRootCmd() *cobra.Command {
rootCmd.AddCommand(newRunCmd(w, funcCompMgr))
rootCmd.AddCommand(newShowCmd(w, wksCompMgr))
rootCmd.AddCommand(newVersionCmd())
rootCmd.AddCommand(newMigrateCmd(w))
return rootCmd
}

Expand Down
53 changes: 49 additions & 4 deletions internal/workspace/workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
const (
defaultConfigDir = ".config/wo"
envVariablePrefix = "WO"
defaultEnv = "default"
)

const (
Expand Down Expand Up @@ -116,7 +117,7 @@ func (s WorkspaceManager) BuildAliases(prefix string) ([]string, error) {

func (s WorkspaceManager) List() ([]Workspace, error) {
workspaces := []Workspace{}
entries, err := os.ReadDir(s.configDir)
entries, err := os.ReadDir(s.getWorkspacesDir())
if os.IsNotExist(err) {
return workspaces, nil
}
Expand Down Expand Up @@ -156,7 +157,7 @@ func (s WorkspaceManager) Create(name string, path string) error {
if err != nil {
return err
}
err = s.createFile(s.resolveEnvFile(name, "default"))
err = s.createFile(s.resolveEnvFile(name, defaultEnv))
if err != nil {
return err
}
Expand Down Expand Up @@ -285,6 +286,45 @@ func (s WorkspaceManager) CreateEnvVariableStatement(name string, value string)
return ""
}

func (s WorkspaceManager) Migrate() error {
stat, err := os.Stat(fmt.Sprintf("%s/workspaces", s.configDir))
if stat != nil {
return nil
}
if err != nil {
return err
}
entries, err := os.ReadDir(s.configDir)
if os.IsNotExist(err) {
return err
}
err = os.MkdirAll(s.getWorkspacesDir(), 0o777)
if err != nil {
return err
}
for _, e := range entries {
if !e.IsDir() {
continue
}
if strings.HasPrefix(e.Name(), ".") {
continue
}
err := os.Rename(fmt.Sprintf("%s/%s", s.configDir, e.Name()), fmt.Sprintf("%s/%s", s.getWorkspacesDir(), e.Name()))
if err != nil {
return err
}
err = s.createWorkspaceFolder(e.Name())
if err != nil {
return err
}
err = s.createFile(s.resolveEnvFile(e.Name(), defaultEnv))
if err != nil {
return err
}
}
return nil
}

func (s WorkspaceManager) appendLoadStatement(name string, env string, functionAndArgs []string) []string {
data := []string{}
data = append(data, s.CreateEnvVariableStatement(fmt.Sprintf("%s_NAME", envVariablePrefix), name))
Expand Down Expand Up @@ -369,7 +409,7 @@ func (s WorkspaceManager) getExtension() string {
}

func (s WorkspaceManager) createConfigFolder() error {
err := errors.Join(os.MkdirAll(s.configDir, 0o777), os.WriteFile(s.resolveGitignoreFile(), []byte("*/envs/*\n"), 0o666))
err := errors.Join(os.MkdirAll(s.configDir, 0o777), os.WriteFile(s.resolveGitignoreFile(), []byte("**/envs/**\n"), 0o666))
if err != nil {
return nil
}
Expand All @@ -393,13 +433,18 @@ func (s WorkspaceManager) createConfigFolder() error {

func (s WorkspaceManager) createWorkspaceFolder(name string) error {
return errors.Join(
os.MkdirAll(s.getWorkspaceDir(name), 0o777),
os.MkdirAll(s.getWorkspaceFunctionsDir(name), 0o777),
os.MkdirAll(s.getWorkspaceEnvsDir(name), 0o777),
)
}

func (s WorkspaceManager) getWorkspacesDir() string {
return fmt.Sprintf("%s/workspaces", s.configDir)
}

func (s WorkspaceManager) getWorkspaceDir(name string) string {
return fmt.Sprintf("%s/%s", s.configDir, name)
return fmt.Sprintf("%s/%s", s.getWorkspacesDir(), name)
}

func (s WorkspaceManager) getWorkspaceFunctionsDir(name string) string {
Expand Down
Loading

0 comments on commit 162d5a8

Please sign in to comment.