Skip to content

Commit

Permalink
🔀: PR #9 from ali-furkan/refac/arch
Browse files Browse the repository at this point in the history
refac config
  • Loading branch information
ali-furkan committed Dec 1, 2021
2 parents afc9def + c9953e3 commit a00b913
Show file tree
Hide file tree
Showing 39 changed files with 1,123 additions and 666 deletions.
10 changes: 5 additions & 5 deletions cmd/wo/editor/editor.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"github.com/ali-furkan/wo/cmd/wo/editor/list"
"github.com/ali-furkan/wo/cmd/wo/editor/open"
"github.com/ali-furkan/wo/cmd/wo/editor/set"
"github.com/ali-furkan/wo/internal/config"
"github.com/ali-furkan/wo/internal/cmdutil"
"github.com/spf13/cobra"
)

Expand All @@ -14,16 +14,16 @@ const (
CmdLongDesc = "Manage your editors"
)

func NewCmdEditor(cfg *config.Config) *cobra.Command {
func NewCmdEditor(ctx *cmdutil.CmdContext) *cobra.Command {
cmd := &cobra.Command{
Use: CmdUsage,
Short: CmdShortDesc,
Long: CmdLongDesc,
}

cmd.AddCommand(open.NewCmdOpen(cfg))
cmd.AddCommand(set.NewCmdSetEditor(cfg))
cmd.AddCommand(list.NewCmdSetEditor(cfg))
cmd.AddCommand(open.NewCmdOpen(ctx))
cmd.AddCommand(set.NewCmdSetEditor(ctx))
cmd.AddCommand(list.NewCmdSetEditor(ctx))

return cmd
}
21 changes: 13 additions & 8 deletions cmd/wo/editor/list/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"errors"
"fmt"

"github.com/ali-furkan/wo/internal/config"
"github.com/ali-furkan/wo/internal/cmdutil"
"github.com/spf13/cobra"
)

Expand All @@ -16,33 +16,38 @@ const (
ErrNotFoundEditor = "wo cant found editor. you can download one of the following editors"
)

func NewCmdSetEditor(cfg *config.Config) *cobra.Command {
func NewCmdSetEditor(ctx *cmdutil.CmdContext) *cobra.Command {
cmd := &cobra.Command{
Use: CmdUsage,
Short: CmdShortDesc,
Long: CmdLongDesc,
Args: cobra.NoArgs,
RunE: func(cmd *cobra.Command, args []string) error {
return listEditors(cfg)
return listEditors(ctx)
},
}

return cmd
}

func listEditors(cfg *config.Config) error {
editors := cfg.Config().Editors
func listEditors(ctx *cmdutil.CmdContext) error {
c, err := ctx.Config()
if err != nil {
return err
}

editors := c.Get("editors").(map[string]map[string]string)
if len(editors) == 0 {
return errors.New(ErrNotFoundEditor)
}

res := fmt.Sprintf("Showing %d list of edit\n\n", len(editors))

def_editor := cfg.Config().Workspace.DefaultEditor
def_editor := c.GetString("defaults.editor")

for _, e := range editors {
res += fmt.Sprintf("%s - %s", e.Name, e.Exec)
if e.Name == def_editor {
res += fmt.Sprintf("%s - %s", e["id"], e["exec"])
if e["id"] == def_editor {
res += fmt.Sprintln(" 'current editor'")
} else {
res += "\n"
Expand Down
19 changes: 11 additions & 8 deletions cmd/wo/editor/open/open.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package open
import (
"errors"

"github.com/ali-furkan/wo/internal/config"
"github.com/ali-furkan/wo/internal/cmdutil"
"github.com/ali-furkan/wo/internal/editor"
"github.com/spf13/cobra"
)
Expand All @@ -15,15 +15,15 @@ const (
)

type OpenOpts struct {
Config *config.Config
Ctx *cmdutil.CmdContext

SelectedEditor string
Path string
}

func NewCmdOpen(cfg *config.Config) *cobra.Command {
func NewCmdOpen(ctx *cmdutil.CmdContext) *cobra.Command {
opts := &OpenOpts{
Config: cfg,
Ctx: ctx,
}

cmd := &cobra.Command{
Expand All @@ -32,7 +32,7 @@ func NewCmdOpen(cfg *config.Config) *cobra.Command {
Long: CmdLongDesc,
Args: cobra.MaximumNArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
opts.SelectedEditor = cfg.Config().Workspace.DefaultEditor
opts.SelectedEditor = ctx.Defaults()["editor"]
if len(args) > 0 {
opts.SelectedEditor = args[0]
}
Expand All @@ -48,11 +48,14 @@ func NewCmdOpen(cfg *config.Config) *cobra.Command {
}

func openEditor(opts *OpenOpts) error {
editors := opts.Config.Config().Editors
editors := opts.Ctx.Editors()

for _, e := range editors {
if e.Name == opts.SelectedEditor {
return editor.OpenEditor(e, opts.Path)
if e["id"] == opts.SelectedEditor {
return editor.OpenEditor(editor.Editor{
Name: e["id"],
Exec: e["exec"],
}, opts.Path)
}
}

Expand Down
33 changes: 18 additions & 15 deletions cmd/wo/editor/set/set.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package set

import (
"errors"
"fmt"

"github.com/ali-furkan/wo/internal/config"
"github.com/ali-furkan/wo/internal/cmdutil"
"github.com/spf13/cobra"
)

Expand All @@ -15,14 +14,14 @@ const (
)

type SetEditorOpts struct {
Config *config.Config
Ctx *cmdutil.CmdContext

SelectedEditor string
}

func NewCmdSetEditor(cfg *config.Config) *cobra.Command {
func NewCmdSetEditor(ctx *cmdutil.CmdContext) *cobra.Command {
opts := &SetEditorOpts{
Config: cfg,
Ctx: ctx,
}

cmd := &cobra.Command{
Expand All @@ -41,18 +40,22 @@ func NewCmdSetEditor(cfg *config.Config) *cobra.Command {
}

func setEditor(opts *SetEditorOpts) error {
c := opts.Config.Config()

for _, editor := range c.Editors {
if editor.Name == opts.SelectedEditor {
c.Workspace.DefaultEditor = editor.Name
config, err := opts.Ctx.Config()
if err != nil {
return err
}

fmt.Printf("\n%s set default editor\n", editor.Name)
field := fmt.Sprintf("editors.%s", opts.SelectedEditor)
editor := config.Get(field).(map[string]string)
if editor == nil {
return fmt.Errorf("specified '%s' editor not found", opts.SelectedEditor)
}

return nil
}
err = config.Set("defaults.editor", editor["id"])
if err != nil {
return err
}

err := fmt.Sprintf("specified `%s` editor not found", opts.SelectedEditor)
return errors.New(err)
fmt.Printf("\n%s set default editor\n", editor["id"])
return nil
}
14 changes: 5 additions & 9 deletions cmd/wo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,20 @@ import (
"fmt"

"github.com/ali-furkan/wo/cmd/wo/root"
"github.com/ali-furkan/wo/internal/config"
"github.com/ali-furkan/wo/internal/cmdutil"
"github.com/ali-furkan/wo/internal/cycle"
cycle_pkg "github.com/ali-furkan/wo/pkg/cycle"
)

func Run() int {
cfg, err := config.NewConfig()
if err != nil {
fmt.Println(err)
return 1
}
ctx := cmdutil.NewCmdContext()

rootCycle := cycle.NewCycleRoot(cfg)
rootCycle := cycle.NewCycleRoot(ctx)

rootCycle.Run(cycle_pkg.OnCycleStart)

rootCmd := root.NewCmdRoot(cfg)
err = rootCmd.Execute()
rootCmd := root.NewCmdRoot(ctx)
err := rootCmd.Execute()
if err != nil {
fmt.Println(err)
return 1
Expand Down
8 changes: 4 additions & 4 deletions cmd/wo/root/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import (
cmdUpdate "github.com/ali-furkan/wo/cmd/wo/update"
cmdVersion "github.com/ali-furkan/wo/cmd/wo/version"
cmdWorks "github.com/ali-furkan/wo/cmd/wo/works"
"github.com/ali-furkan/wo/internal/config"
"github.com/ali-furkan/wo/internal/cmdutil"
"github.com/ali-furkan/wo/internal/version"
"github.com/spf13/cobra"
)

func NewCmdRoot(cfg *config.Config) *cobra.Command {
func NewCmdRoot(ctx *cmdutil.CmdContext) *cobra.Command {
cmd := &cobra.Command{
Use: CmdUsage,
Short: CmdShortDesc,
Expand All @@ -27,8 +27,8 @@ func NewCmdRoot(cfg *config.Config) *cobra.Command {

cmd.AddCommand(cmdUpdate.NewCmdUpdate())
cmd.AddCommand(cmdVersion.NewCmdVersion())
cmd.AddCommand(cmdEditor.NewCmdEditor(cfg))
cmdWorks.InitCmdWorks(cmd, cfg)
cmd.AddCommand(cmdEditor.NewCmdEditor(ctx))
cmdWorks.InitCmdWorks(cmd, ctx)

return cmd
}
Loading

0 comments on commit a00b913

Please sign in to comment.