Skip to content

Commit

Permalink
refactor: simplify XDG usage
Browse files Browse the repository at this point in the history
  • Loading branch information
sn3d committed Dec 5, 2022
1 parent 0b0d10e commit dbfb362
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 11 deletions.
3 changes: 1 addition & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@ func main() {
err := sdk.RunToolbx(
sdk.WithBrandLabel("toolbx"),
sdk.WithGitlab(os.Getenv("GITLAB_TOKEN")),
sdk.WithXdgData(),
sdk.WithXdgConfig(),
sdk.WithXdg("toolbx"),
)

if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions pkg/executor/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func Create(cfg config.Configuration) *ToolbxExecutor {
// or user defined command that will invoke tool
func (e *ToolbxExecutor) Execute(args []string) error {
if len(os.Args) >= 2 && strings.HasPrefix(os.Args[1], ".") {
return e.runDotCommand(args)
return e.runDotCommand(e.config, args)
} else {
return e.runCommand(args)
}
Expand Down Expand Up @@ -108,7 +108,7 @@ func (e *ToolbxExecutor) runCommand(args []string) error {
return nil
}

func (e *ToolbxExecutor) runDotCommand(args []string) error {
func (e *ToolbxExecutor) runDotCommand(cfg config.Configuration, args []string) error {
dotCommand := args[1][1:]

for _, c := range cli.DotCommands {
Expand Down
15 changes: 8 additions & 7 deletions sdk/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,19 @@ import (

type ToolbxOption func(cfg *config.Configuration)

// load configuration file from $HOME/.config/{brand label}/{brand label}.yaml
func WithXdgConfig() ToolbxOption {
// WithXdg ensure the toolbx will use XDG directory spec.
// for config files and all data files.
//
// The data will be stored in $HOME/.local/share/{name} and configuration
// will be loaded from $HOME/.config/{name}/{name}.yaml
//
func WithXdg(name string) ToolbxOption {
return func(cfg *config.Configuration) {

configHome := dir.XdgConfigHome()
toolbxConfigFile := path.Join(configHome, cfg.BrandLabel, cfg.BrandLabel+".yaml")
WithConfigFile(toolbxConfigFile)(cfg)
}
}

// data like installations will be stored in $HOME/.local/share/{brand label}
func WithXdgData() ToolbxOption {
return func(cfg *config.Configuration) {
dataHome := dir.XdgDataHome()
toolbxDataHome := path.Join(dataHome, cfg.BrandLabel)
WithDataDir(toolbxDataHome)(cfg)
Expand Down

0 comments on commit dbfb362

Please sign in to comment.