Skip to content

Commit

Permalink
fix relative includes
Browse files Browse the repository at this point in the history
  • Loading branch information
RasmusLindroth committed Feb 24, 2020
1 parent c0036d5 commit ad5fe25
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 14 deletions.
2 changes: 1 addition & 1 deletion i3keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/RasmusLindroth/i3keys/internal/web"
)

const version string = "0.0.10"
const version string = "0.0.11"

func helpText(exitCode int) {
fmt.Print("Usage:\n\n\ti3keys [-s] <command> [arguments]\n")
Expand Down
45 changes: 43 additions & 2 deletions internal/helpers/include.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
package helpers

import (
"errors"
"log"
"os"
"os/exec"
"path/filepath"
"strings"
)

type Include struct {
ParentPath string
Path string
}

//Equal to os/exec Expand but changed ${} to $()
func replaceDollar(s string, mapping func(string) string) string {
var buf []byte
Expand Down Expand Up @@ -138,8 +144,13 @@ func checkPath(s string) []string {
return r
}

func GetPaths(s string) ([]string, error) {
s = ExpandCommand(s)
func GetPaths(i Include) ([]string, error) {
s := ExpandCommand(i.Path)
s = os.ExpandEnv(s)
if !filepath.IsAbs(s) {
dir := filepath.Dir(i.ParentPath)
s = filepath.Join(dir, s)
}
matches, err := filepath.Glob(s)
if err != nil {
return nil, err
Expand All @@ -150,3 +161,33 @@ func GetPaths(s string) ([]string, error) {
}
return paths, nil
}

func GetSwayDefaultConfig() (string, error) {
home, _ := os.LookupEnv("HOME")
xdgConfig, exists := os.LookupEnv("XDG_CONFIG_HOME")
if !exists {
xdgConfig = home + "/.config"
}
configs := []string{
home + "/.sway/config",
xdgConfig + "/sway/config",
home + "/.i3/config",
xdgConfig + "/i3/config",
"/etc/sway/config",
"/etc/i3/config",
}
configPath := ""
for _, c := range configs {
_, err := os.Stat(c)
if os.IsNotExist(err) {
continue
}
configPath = c
break
}
var e error
if configPath == "" {
e = errors.New("couldn't find a config file")
}
return configPath, e
}
23 changes: 12 additions & 11 deletions internal/i3parse/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func readLine(reader *bufio.Reader, c context) (string, []string, lineType, erro
return line, lineParts, lineType, err
}

func parseConfig(confReader io.Reader, err error) ([]Mode, []Binding, []Variable, []string, error) {
func parseConfig(confReader io.Reader, confPath string, err error) ([]Mode, []Binding, []Variable, []string, error) {
if err != nil {
return []Mode{}, []Binding{}, []Variable{}, []string{}, errors.New("Couldn't get the config file")
}
Expand All @@ -118,7 +118,7 @@ func parseConfig(confReader io.Reader, err error) ([]Mode, []Binding, []Variable
var modes []Mode
var bindings []Binding
var variables []Variable
var includes []string
var includes []helpers.Include

context := mainContext
var readErr error
Expand All @@ -145,7 +145,11 @@ func parseConfig(confReader io.Reader, err error) ([]Mode, []Binding, []Variable
modes = append(modes, Mode{Name: name})
continue
case includeLine:
includes = append(includes, strings.Join(lineParts[1:], " "))
inc := helpers.Include{
ParentPath: confPath,
Path: strings.Join(lineParts[1:], " "),
}
includes = append(includes, inc)
continue
case bindCodeBracket:
if context == mainContext {
Expand Down Expand Up @@ -210,12 +214,14 @@ func parseConfig(confReader io.Reader, err error) ([]Mode, []Binding, []Variable
}

func parse(confReader io.Reader, err error) ([]Mode, []Binding, error) {
modes, bindings, variables, includes, err := parseConfig(confReader, err)
configPath, _ := helpers.GetSwayDefaultConfig()
modes, bindings, variables, includes, err := parseConfig(confReader, configPath, err)
if err != nil {
return []Mode{}, []Binding{}, errors.New("Couldn't get the config file")
}
var parsedIncludes []string
for _, incl := range includes {
for j := 0; j < len(includes); j++ {
incl := includes[j]
done := false
for _, ap := range parsedIncludes {
if ap == incl {
Expand All @@ -229,7 +235,7 @@ func parse(confReader io.Reader, err error) ([]Mode, []Binding, error) {
if err != nil {
log.Printf("couldn't open the included file %s, got err: %v\n", incl, ferr)
}
m, b, v, i, perr := parseConfig(f, err)
m, b, v, i, perr := parseConfig(f, incl, err)
if err != nil {
log.Printf("couldn't parse the included file %s, got err: %v\n", incl, perr)
}
Expand Down Expand Up @@ -356,10 +362,8 @@ func replaceVariablesInBindings(variables []Variable, bindings []Binding) []Bind
for mkey := range bindings[key].Modifiers {
bindings[key].Modifiers[mkey] = variableNameToValue(variables, bindings[key].Modifiers[mkey])
}

nb = append(nb, bindings[key])
}

return bindings
}

Expand All @@ -368,12 +372,10 @@ func replaceVariablesInModes(variables []Variable, modes []Mode) []Mode {
modes[mkey].Name = variableNameToValue(variables, modes[mkey].Name)
modes[mkey].Bindings = replaceVariablesInBindings(variables, mode.Bindings)
}

return modes
}

func sortModifiers(bindings []Binding) []Binding {

for key := range bindings {
var a []string
var b []string
Expand All @@ -388,6 +390,5 @@ func sortModifiers(bindings []Binding) []Binding {
sort.Strings(b)
bindings[key].Modifiers = append(a, b...)
}

return bindings
}

0 comments on commit ad5fe25

Please sign in to comment.