Skip to content

Commit

Permalink
Allow building without being in a sanic env.
Browse files Browse the repository at this point in the history
  • Loading branch information
ColinChartier committed Jul 9, 2019
1 parent bc8a517 commit 7ee2727
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 14 deletions.
17 changes: 12 additions & 5 deletions commands/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,26 +49,33 @@ func buildCommandAction(cliContext *cli.Context) error {
}
}

var buildRoot string
s, err := shell.Current()
if err != nil {
return cli.NewExitError(err.Error(), 1)
fmt.Fprintln(os.Stderr, "[WARNING] sanic is building dockerfiles recursively in your current directory. It's recommended to use a sanic environment for consistency.")
buildRoot, err = os.Getwd()
if err != nil {
return cli.NewExitError(fmt.Sprintf("error while getting current directory: %s", err.Error()), 1)
}
} else {
buildRoot = s.GetSanicRoot()
}

services, err := util.FindServices()
services, err := util.FindServices(buildRoot)
if err != nil {
return cli.NewExitError(err.Error(), 1)
}

if len(services) == 0 {
return cli.NewExitError("you must put sanic.yaml in a directory that contains a Dockerfile somewhere within it.", 1)
return cli.NewExitError(fmt.Sprintf("%s (or some of its subdirectories) should contain a Dockerfile"), 1)
}

err = build.EnsureBuildkitDaemon()
if err != nil {
return cli.NewExitError(err.Error(), 1)
}

buildTag, err := git.GetCurrentTreeHash(s.GetSanicRoot(), services...)
buildTag, err := git.GetCurrentTreeHash(buildRoot, services...)
if err != nil {
return cli.NewExitError(err.Error(), 1)
}
Expand All @@ -82,7 +89,7 @@ func buildCommandAction(cliContext *cli.Context) error {
}
}()

buildLogger := build.NewFlatfileLogger(filepath.Join(s.GetSanicRoot(), "logs"), cliContext.Bool("verbose"))
buildLogger := build.NewFlatfileLogger(filepath.Join(buildRoot, "logs"), cliContext.Bool("verbose"))
buildLogger.AddLogLineListener(buildInterface.ProcessLog)
defer buildLogger.Close()

Expand Down
2 changes: 1 addition & 1 deletion commands/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ func runTemplater(folderIn, folderOut, templaterImage, namespace string) error {
if err != nil {
return err
}
services, err := util.FindServices()
services, err := util.FindServices(shl.GetSanicRoot())
if err != nil {
return err
}
Expand Down
10 changes: 2 additions & 8 deletions util/services.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,16 @@
package util

import (
"github.com/distributed-containers-inc/sanic/shell"
"os"
"path/filepath"
)

//FindServices finds all of the buildable services in the sanic root directory
//(e.g., folders which contain a Dockerfile)
func FindServices() ([]string, error) {
s, err := shell.Current()
if err != nil {
return nil, err
}

func FindServices(dir string) ([]string, error) {
var ret []string

err = filepath.Walk(s.GetSanicRoot(), func(path string, info os.FileInfo, err error) error {
err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
if info.Name() == "Dockerfile" {
ret = append(ret, filepath.Dir(path))
}
Expand Down

0 comments on commit 7ee2727

Please sign in to comment.