Skip to content

Commit

Permalink
Merge branch 'release/0.9.0'
Browse files Browse the repository at this point in the history
Former-commit-id: 504e4b5
  • Loading branch information
pauldotknopf committed Dec 9, 2017
2 parents df87f7b + a86d6c9 commit 98cce87
Show file tree
Hide file tree
Showing 12 changed files with 459 additions and 22 deletions.
6 changes: 4 additions & 2 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@
"version": "0.2.0",
"configurations": [
{
"name": "Launch file",
"name": "Launch",
"type": "go",
"request": "launch",
"mode": "debug",
"env":{
"GOPATH": "${workspaceRoot}/.go"
},
"cwd": "./",
"cwd": "/home/pknopf/git/darch-images",
"args": [
"inspect",
"plasma"
],
"program": "${workspaceRoot}/darch.go"
}
Expand Down
2 changes: 1 addition & 1 deletion .vscode/tasks.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"version": "2.0.0",
"tasks": [
{
"taskName": "Build",
"label": "Build",
"type": "shell",
"command": "make"
}
Expand Down
10 changes: 8 additions & 2 deletions commands/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ func Command() cli.Command {
Usage: "Prefix for built images. For example, a value of \"pauldotknopf/darch-\" while building image \"base\", the generated image will be named \"pauldotknopf/darch-base\".",
Value: "",
},
cli.StringFlag{
Name: "packageCache, c",
Usage: "Location where package caches are stored. This speeds up builds, preventing downloading.",
Value: "/var/darch/cache/packages",
},
cli.StringSliceFlag{
Name: "environment, e",
},
Expand All @@ -44,7 +49,7 @@ func Command() cli.Command {
if err != nil {
return cli.NewExitError(err, 1)
}
err = build(c.Args().First(), c.String("imagesDir"), strings.Split(c.String("tags"), ","), c.String("imagePrefix"), environmentVaribles)
err = build(c.Args().First(), c.String("imagesDir"), strings.Split(c.String("tags"), ","), c.String("imagePrefix"), c.String("packageCache"), environmentVaribles)
if err != nil {
return cli.NewExitError(err, 1)
}
Expand All @@ -53,7 +58,7 @@ func Command() cli.Command {
}
}

func build(name string, imagesDir string, tags []string, imagePrefix string, environmentVariables map[string]string) error {
func build(name string, imagesDir string, tags []string, imagePrefix string, packageCache string, environmentVariables map[string]string) error {
if len(name) == 0 {
return fmt.Errorf("Name is required")
}
Expand Down Expand Up @@ -87,6 +92,7 @@ func build(name string, imagesDir string, tags []string, imagePrefix string, env
imageDefinition,
tags,
imagePrefix,
packageCache,
environmentVariables)

if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion commands/extract/extract.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func Command() cli.Command {
cli.StringFlag{
Name: "destination, d",
Usage: "The location to extract the image to.",
Value: "/var/darch",
Value: "/var/darch/extracted",
},
},
Action: func(c *cli.Context) error {
Expand Down
272 changes: 272 additions & 0 deletions commands/inspect/inspect.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,272 @@
package inspect

import (
"fmt"
"log"
"strings"

"../../images"
"../../utils"
"github.com/disiqueira/gotree"
"github.com/urfave/cli"
)

func parentsCommand() cli.Command {
return cli.Command{
Name: "parents",
Usage: "The parents (inherited images) of an image.",
ArgsUsage: "IMAGE_NAME",
Flags: []cli.Flag{
cli.StringFlag{
Name: "imagesDir, d",
Usage: "Location of the images.",
Value: ".",
},
cli.BoolFlag{
Name: "excludeExternal",
},
},
Action: func(c *cli.Context) error {
if len(c.Args()) != 1 {
return cli.NewExitError(fmt.Errorf("Unexpected arguements"), 1)
}
err := parents(c.Args().First(), c.String("imagesDir"), c.Bool("excludeExternal"))
if err != nil {
return cli.NewExitError(err, 1)
}
return nil
},
}
}

func childrenCommand() cli.Command {
return cli.Command{
Name: "children",
Usage: "The children that are dependent on the provided image.",
ArgsUsage: "IMAGE_NAME",
Flags: []cli.Flag{
cli.StringFlag{
Name: "imagesDir, d",
Usage: "Location of the images.",
Value: ".",
},
},
Action: func(c *cli.Context) error {
if len(c.Args()) != 1 {
return cli.NewExitError(fmt.Errorf("Unexpected arguements"), 1)
}
err := children(c.Args().First(), c.String("imagesDir"))
if err != nil {
return cli.NewExitError(err, 1)
}
return nil
},
}
}

func treeCommand() cli.Command {
return cli.Command{
Name: "tree",
Usage: "Display all images in a tree.",
Flags: []cli.Flag{
cli.StringFlag{
Name: "imagesDir, d",
Usage: "Location of the images.",
Value: ".",
},
},
Action: func(c *cli.Context) error {
err := tree(c.String("imagesDir"))
if err != nil {
return cli.NewExitError(err, 1)
}
return nil
},
}
}

// Command Returns the command to be passed to a cli context.
func Command() cli.Command {
return cli.Command{
Name: "inspect",
Usage: "Inspect an image.",
ArgsUsage: "IMAGE_NAME",
Flags: []cli.Flag{
cli.StringFlag{
Name: "imagesDir, d",
Usage: "Location of the images.",
Value: ".",
},
},
Subcommands: []cli.Command{
parentsCommand(),
childrenCommand(),
treeCommand(),
},
Action: func(c *cli.Context) error {
if len(c.Args()) != 1 {
return cli.NewExitError(fmt.Errorf("Unexpected arguements"), 1)
}
err := inspect(c.Args().First(), c.String("imagesDir"))
if err != nil {
return cli.NewExitError(err, 1)
}
return nil
},
}
}

func parents(name string, imagesDir string, excludeExternal bool) error {

if len(name) == 0 {
return fmt.Errorf("Name is required")
}

if len(imagesDir) == 0 {
return fmt.Errorf("Images directory is required")
}

imagesDir = utils.ExpandPath(imagesDir)

imageDefinitions, err := images.BuildAllDefinitions(imagesDir)

if err != nil {
return err
}

current, ok := imageDefinitions[name]

if !ok {
return fmt.Errorf("Image %s doesn't exist", name)
}

finished := false
for finished != true {
if strings.HasPrefix(current.Inherits, "external:") {
if !excludeExternal {
log.Println(current.Inherits[len("external:"):len(current.Inherits)])
}
finished = true
} else {
current = imageDefinitions[current.Inherits]
log.Println(current.Name)
}
}

return nil
}

func children(name string, imagesDir string) error {
if len(name) == 0 {
return fmt.Errorf("Name is required")
}

if len(imagesDir) == 0 {
return fmt.Errorf("Images directory is required")
}

imagesDir = utils.ExpandPath(imagesDir)

imageDefinitions, err := images.BuildAllDefinitions(imagesDir)

if err != nil {
return err
}

current, ok := imageDefinitions[name]

if !ok {
return fmt.Errorf("Image %s doesn't exist", name)
}

for _, imageDefinition := range imageDefinitions {
if imageDefinition.Inherits == current.Name {
log.Println(imageDefinition.Name)
}
}

return err
}

func buildTreeRecursively(parentDefinition images.ImageDefinition, imageDefinitions map[string]images.ImageDefinition) []gotree.GTStructure {
children := make([]gotree.GTStructure, 0)

for _, childImageDefinition := range imageDefinitions {
if childImageDefinition.Inherits == parentDefinition.Name {
var childNode gotree.GTStructure
childNode.Name = childImageDefinition.Name

for _, child := range buildTreeRecursively(childImageDefinition, imageDefinitions) {
childNode.Items = append(childNode.Items, child)
}
children = append(children, childNode)
}
}

return children
}

func tree(imagesDir string) error {
if len(imagesDir) == 0 {
return fmt.Errorf("Images directory is required")
}

imagesDir = utils.ExpandPath(imagesDir)

imageDefinitions, err := images.BuildAllDefinitions(imagesDir)

if err != nil {
return err
}

externalImages := make([]string, 0)

for _, imageDefinition := range imageDefinitions {
if imageDefinition.InheritsExternal {
externalImages = append(externalImages, imageDefinition.Inherits)
}
}

// this will be our root items
externalImages = utils.RemoveDuplicates(externalImages)

var rootNode gotree.GTStructure

for _, externalImage := range externalImages {
var externalImageNode gotree.GTStructure
externalImageNode.Name = externalImage
for _, imageDefinition := range imageDefinitions {
if imageDefinition.InheritsExternal && imageDefinition.Inherits == externalImage {
var childNode gotree.GTStructure
childNode.Name = imageDefinition.Name
for _, child := range buildTreeRecursively(imageDefinition, imageDefinitions) {
childNode.Items = append(childNode.Items, child)
}
externalImageNode.Items = append(externalImageNode.Items, childNode)
}
}
rootNode.Items = append(rootNode.Items, externalImageNode)
}

gotree.PrintTree(rootNode)

return nil
}

func inspect(name string, imagesDir string) error {
if len(name) == 0 {
return fmt.Errorf("Name is required")
}

if len(imagesDir) == 0 {
return fmt.Errorf("Images directory is required")
}

imagesDir = utils.ExpandPath(imagesDir)

imageDefinition, err := images.BuildDefinition(name, imagesDir)

log.Println(imageDefinition.Name)

return err
}
2 changes: 1 addition & 1 deletion commands/stage/stage.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func Command() cli.Command {
cli.StringFlag{
Name: "source, s",
Usage: "The location where the extract images are.",
Value: "/var/darch",
Value: "/var/darch/extracted",
},
cli.StringFlag{
Name: "fstab",
Expand Down
2 changes: 2 additions & 0 deletions darch.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"./commands/build"
"./commands/extract"
"./commands/inspect"
"./commands/stage"
"github.com/urfave/cli"
)
Expand All @@ -31,6 +32,7 @@ func main() {
app.Commands = []cli.Command{
build.Command(),
extract.Command(),
inspect.Command(),
stage.Command(),
cli.Command{
Name: "version",
Expand Down
2 changes: 1 addition & 1 deletion gitversion
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

DIRNAME="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

docker run -v $DIRNAME:/repo gittools/gitversion $@
docker run --rm -v $DIRNAME:/repo gittools/gitversion $@
Loading

0 comments on commit 98cce87

Please sign in to comment.