diff --git a/.gitignore b/.gitignore index 2ac9c89..ccd0805 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,2 @@ /vendor /bin - diff --git a/.makeup/makeup-bag-deis b/.makeup/makeup-bag-deis index 60fa1cf..0f50fc4 160000 --- a/.makeup/makeup-bag-deis +++ b/.makeup/makeup-bag-deis @@ -1 +1 @@ -Subproject commit 60fa1cfd1800c29499c14808bfc7e68c61c3ef01 +Subproject commit 0f50fc4bb87e908c2c5f0ed3061dee16c95d7bf4 diff --git a/cmd/add.go b/cmd/add.go index 5bae63e..dac4854 100644 --- a/cmd/add.go +++ b/cmd/add.go @@ -16,12 +16,13 @@ package cmd import ( "fmt" - "os/exec" - "strings" "log" + "os/exec" "path" "path/filepath" + "strings" + "github.com/deis/makeup/cmd/bag" "github.com/spf13/cobra" ) @@ -49,7 +50,7 @@ func AddSubmodule(repo_path string) { log.Printf("[DEBUG] git submodule %s already exists!", repo_name) } else { url := fmt.Sprint("https://", repo_path, ".git") - path := fmt.Sprint(".makeup/", repo_name) + path := fmt.Sprint(bag.SubmoduleDir, "/", repo_name) output, err := exec.Command("git", "submodule", "add", url, path).CombinedOutput() if err != nil { log.Fatalf("[ERROR] git submodule add failed with:\n%s\n", output) @@ -61,7 +62,7 @@ func AddSubmodule(repo_path string) { var addCmd = &cobra.Command{ Use: "add", Short: "Add a makeup kit to this project", - Long: ``, + Long: ``, Run: func(cmd *cobra.Command, args []string) { if len(args) == 1 { AddSubmodule(args[0]) diff --git a/cmd/bag.go b/cmd/bag.go new file mode 100644 index 0000000..13cb108 --- /dev/null +++ b/cmd/bag.go @@ -0,0 +1,31 @@ +// Copyright © 2015 NAME HERE +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package cmd + +import ( + "github.com/deis/makeup/cmd/bag" + "github.com/spf13/cobra" +) + +var bagCmd = &cobra.Command{ + Use: "bag", + Short: "Performs operations on bags", + Long: "", +} + +func init() { + bag.AddVarsCommand(bagCmd) + RootCmd.AddCommand(bagCmd) +} diff --git a/cmd/bag/constants.go b/cmd/bag/constants.go new file mode 100644 index 0000000..e8125b5 --- /dev/null +++ b/cmd/bag/constants.go @@ -0,0 +1,5 @@ +package bag + +const ( + SubmoduleDir = ".makeup" +) diff --git a/cmd/bag/vars.go b/cmd/bag/vars.go new file mode 100644 index 0000000..6e7129a --- /dev/null +++ b/cmd/bag/vars.go @@ -0,0 +1,109 @@ +package bag + +import ( + "fmt" + "log" + "os" + "path/filepath" + "strings" + + "github.com/spf13/cobra" +) + +const ( + dependencies = "DEPENDENCIES" + varListPrefix = "# - " +) + +func lineCommented(text string) string { + return "# " + text +} + +var varsCmd = &cobra.Command{ + Use: "vars", + Short: "List the input variables for a bag or individual Makefile inside a bag", + Long: "", + Run: func(cmd *cobra.Command, args []string) { + if len(args) != 1 { + log.Printf("[ERROR] Bag or individual makefile name not given") + os.Exit(1) + } + if strings.LastIndex(args[0], ".mk") != -1 { + listMakefileVars(args[0]) + } else { + listBagVars(args[0]) + } + }, +} + +func listBagVars(path string) { + makefiles := make(map[string][]variable) + makefileErrs := make(map[string]error) + spl := strings.Split(path, "/") + if len(spl) != 3 { + log.Printf("[ERROR] bag path %s is invalid", path) + os.Exit(1) + } + bagName := spl[len(spl)-1] + relPath := filepath.Join(SubmoduleDir, bagName) + err := filepath.Walk(relPath, func(path string, info os.FileInfo, err error) error { + if info.IsDir() && filepath.Base(path) == ".git" { + return filepath.SkipDir + } else if filepath.Base(path) == ".git" || info.IsDir() { + return nil + } + + fd, err := os.Open(path) + if err != nil { + return err + } + defer fd.Close() + vars, err := parseVars(fd) + if err != nil { + makefileErrs[path] = err + } else { + makefiles[path] = vars + } + return nil + }) + if err != nil && err != filepath.SkipDir { + log.Printf("[ERROR] walking the bag directory (%s)", err) + os.Exit(1) + } + + for makefileName, vars := range makefiles { + fmt.Println("-- ", filepath.Base(makefileName), " --") + for _, v := range vars { + fmt.Println(v.String()) + } + } +} + +func listMakefileVars(path string) { + spl := strings.Split(path, "/") + if len(spl) != 4 { + log.Printf("[ERROR] makefile path %s is invalid", path) + os.Exit(1) + } + bagName := spl[len(spl)-2] + makefileName := spl[len(spl)-1] + relPath := filepath.Join(SubmoduleDir, bagName, makefileName) + fd, err := os.Open(relPath) + if err != nil { + log.Printf("[ERROR] opening makefile %s (%s)", path, err) + os.Exit(1) + } + defer fd.Close() + vars, err := parseVars(fd) + if err != nil { + log.Printf("[ERROR] parsing variables from %s (%s)", path, err) + os.Exit(1) + } + for _, variable := range vars { + fmt.Println(variable.String()) + } +} + +func AddVarsCommand(cmd *cobra.Command) { + cmd.AddCommand(varsCmd) +} diff --git a/cmd/bag/vars_parser.go b/cmd/bag/vars_parser.go new file mode 100644 index 0000000..df2959b --- /dev/null +++ b/cmd/bag/vars_parser.go @@ -0,0 +1,54 @@ +package bag + +import ( + "bufio" + "errors" + "fmt" + "io" + "strings" +) + +var ( + errEmpty = errors.New("reader was empty") + errNoDeps = errors.New("no dependencies prefix found") +) + +type variable struct { + name string + description string +} + +func (v variable) String() string { + if v.description != "" { + return fmt.Sprintf("%s - %s", v.name, v.description) + } + return v.name +} + +func parseVars(reader io.Reader) ([]variable, error) { + scanner := bufio.NewScanner(reader) + if !scanner.Scan() { + return nil, errEmpty + } + + if scanner.Text() != lineCommented("DEPENDENCIES") { + return nil, errNoDeps + } + + var ret []variable + for scanner.Scan() { + line := scanner.Text() + if !strings.HasPrefix(line, varListPrefix) { + continue + } + remainder := line[len(varListPrefix):] + spl := strings.Split(remainder, ": ") + newVar := variable{} + newVar.name = spl[0] + if len(spl) > 1 { + newVar.description = spl[1] + } + ret = append(ret, newVar) + } + return ret, nil +}