Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
/vendor
/bin

2 changes: 1 addition & 1 deletion .makeup/makeup-bag-deis
9 changes: 5 additions & 4 deletions cmd/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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)
Expand All @@ -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])
Expand Down
31 changes: 31 additions & 0 deletions cmd/bag.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright © 2015 NAME HERE <EMAIL ADDRESS>
//
// 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)
}
5 changes: 5 additions & 0 deletions cmd/bag/constants.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package bag

const (
SubmoduleDir = ".makeup"
)
109 changes: 109 additions & 0 deletions cmd/bag/vars.go
Original file line number Diff line number Diff line change
@@ -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)
}
54 changes: 54 additions & 0 deletions cmd/bag/vars_parser.go
Original file line number Diff line number Diff line change
@@ -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
}