mirror of
https://github.com/mudler/luet.git
synced 2025-04-27 19:25:15 +00:00
It holds necessary state plus additional information relative to the context which we are being run to (e.g. if we are in a terminal or not). Besides in the future we can use it also as a contextual logger to provide more smart logging capabilities. This also replace the general global configuration instance that previously was share between the core components.
82 lines
1.9 KiB
Go
82 lines
1.9 KiB
Go
// Copyright © 2020 Ettore Di Giacinto <mudler@gentoo.org>
|
|
//
|
|
// This program is free software; you can redistribute it and/or modify
|
|
// it under the terms of the GNU General Public License as published by
|
|
// the Free Software Foundation; either version 2 of the License, or
|
|
// (at your option) any later version.
|
|
//
|
|
// This program is distributed in the hope that it will be useful,
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
// GNU General Public License for more details.
|
|
//
|
|
// You should have received a copy of the GNU General Public License along
|
|
// with this program; if not, see <http://www.gnu.org/licenses/>.
|
|
|
|
package backend
|
|
|
|
import (
|
|
"os/exec"
|
|
|
|
"github.com/mudler/luet/pkg/api/core/types"
|
|
|
|
"github.com/google/go-containerregistry/pkg/crane"
|
|
"github.com/pkg/errors"
|
|
)
|
|
|
|
const (
|
|
ImgBackend = "img"
|
|
DockerBackend = "docker"
|
|
)
|
|
|
|
func imageAvailable(image string) bool {
|
|
_, err := crane.Digest(image)
|
|
return err == nil
|
|
}
|
|
|
|
type Options struct {
|
|
ImageName string
|
|
SourcePath string
|
|
DockerFileName string
|
|
Destination string
|
|
Context string
|
|
BackendArgs []string
|
|
}
|
|
|
|
func runCommand(ctx *types.Context, cmd *exec.Cmd) error {
|
|
output := ""
|
|
buffered := !ctx.Config.GetGeneral().ShowBuildOutput
|
|
writer := NewBackendWriter(buffered, ctx)
|
|
|
|
cmd.Stdout = writer
|
|
cmd.Stderr = writer
|
|
|
|
if buffered {
|
|
ctx.Spinner()
|
|
defer ctx.SpinnerStop()
|
|
}
|
|
|
|
err := cmd.Start()
|
|
if err != nil {
|
|
return errors.Wrap(err, "Failed starting command")
|
|
}
|
|
|
|
err = cmd.Wait()
|
|
if err != nil {
|
|
output = writer.GetCombinedOutput()
|
|
return errors.Wrapf(err, "Failed running command: %s", output)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func genBuildCommand(opts Options) []string {
|
|
context := opts.Context
|
|
|
|
if context == "" {
|
|
context = "."
|
|
}
|
|
buildarg := append(opts.BackendArgs, "-f", opts.DockerFileName, "-t", opts.ImageName, context)
|
|
return append([]string{"build"}, buildarg...)
|
|
}
|