From ad5fe25441f40754b385cd116081de4750f39881 Mon Sep 17 00:00:00 2001 From: Rasmus Lindroth Date: Mon, 24 Feb 2020 18:11:21 +0100 Subject: [PATCH] fix relative includes --- i3keys.go | 2 +- internal/helpers/include.go | 45 +++++++++++++++++++++++++++++++++++-- internal/i3parse/parse.go | 23 ++++++++++--------- 3 files changed, 56 insertions(+), 14 deletions(-) diff --git a/i3keys.go b/i3keys.go index a104fa9..65f0608 100644 --- a/i3keys.go +++ b/i3keys.go @@ -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] [arguments]\n") diff --git a/internal/helpers/include.go b/internal/helpers/include.go index 7467aff..62a3b8c 100644 --- a/internal/helpers/include.go +++ b/internal/helpers/include.go @@ -1,6 +1,7 @@ package helpers import ( + "errors" "log" "os" "os/exec" @@ -8,6 +9,11 @@ import ( "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 @@ -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 @@ -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 +} diff --git a/internal/i3parse/parse.go b/internal/i3parse/parse.go index ec1a899..c56e112 100644 --- a/internal/i3parse/parse.go +++ b/internal/i3parse/parse.go @@ -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") } @@ -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 @@ -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 { @@ -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 { @@ -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) } @@ -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 } @@ -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 @@ -388,6 +390,5 @@ func sortModifiers(bindings []Binding) []Binding { sort.Strings(b) bindings[key].Modifiers = append(a, b...) } - return bindings }