add dry-run cli argument | add buildfile.Reader.Execute() error

This commit is contained in:
xdrm-brackets 2018-11-10 12:45:58 +01:00
parent ff06cccb89
commit 464b2539d2
5 changed files with 50 additions and 40 deletions

View File

@ -6,10 +6,11 @@ import (
"git.xdrm.io/xdrm-brackets/nix-amer/internal/instruction"
)
func GetArgs() (*instruction.ExecutionContext, string, error) {
func GetArgs() (*instruction.ExecutionContext, string, bool, error) {
setupFlags(flag.CommandLine)
longPack := flag.String("package", "", "")
dryRun := flag.Bool("dry-run", false, "")
longServ := flag.String("service", "", "")
pack := flag.String("p", "", "")
serv := flag.String("s", "", "")
@ -17,21 +18,21 @@ func GetArgs() (*instruction.ExecutionContext, string, error) {
// 1. fail on missing build file
if len(flag.Args()) < 1 {
return nil, "", fmt.Errorf("missing buildfile")
return nil, "", false, fmt.Errorf("missing buildfile")
}
buildfile := flag.Arg(0)
// 2. override short version with long
if longPack != nil && len(*longPack) > 0 {
if len(*longPack) > 0 {
pack = longPack
}
if longServ != nil && len(*longServ) > 0 {
if len(*longServ) > 0 {
serv = longServ
}
// 3. fail on missing mandatory fields
if pack == nil || len(*pack) < 1 {
return nil, "", fmt.Errorf("missing -package")
if len(*pack) < 1 {
return nil, "", false, fmt.Errorf("missing -package")
}
// if serv == nil || len(*serv) < 1 { // default service
// return nil, "", fmt.Errorf("missing -service")
@ -40,12 +41,12 @@ func GetArgs() (*instruction.ExecutionContext, string, error) {
// 3. Load context
ctx, err := instruction.CreateContext(*pack, *serv)
if err != nil {
return nil, "", err
return nil, "", false, err
}
/*DEBUG*/ //fmt.Printf("package: '%s' | '%s'\n", fpackage, lpackage)
/*DEBUG*/ //fmt.Printf("service: '%s' | '%s'\n", fservice, lservice)
return ctx, buildfile, nil
return ctx, buildfile, *dryRun, nil
}
func setupFlags(f *flag.FlagSet) {

View File

@ -11,7 +11,7 @@ func help() {
fmt.Printf("\tnix-amer - the automatic setup tool you need\n")
fmt.Printf("\n%s\n", clifmt.Color(1, "SYNOPSIS", true))
fmt.Printf("\tnix-amer [-p <package-manager>] [-s <service-manager>] <buildfile>\n")
fmt.Printf("\tnix-amer [-p <package-manager>] [-s <service-manager>] [-dry-run] <buildfile>\n")
fmt.Printf("\tnix-amer -help\n")
fmt.Printf("\tnix-amer -h\n")
fmt.Printf("\n")
@ -35,6 +35,8 @@ func help() {
fmt.Printf("\t-s, -service\n\t Define which service manager to use ; the default\n")
fmt.Printf("\t value is 'systemd'. initd is not yet supported.\n")
fmt.Printf("\n")
fmt.Printf("\t-dry-run\n\t Do not execute, only check the buildfile and arguments.\n")
fmt.Printf("\n")
fmt.Printf("%s\n", clifmt.Color(1, "AUTHORS", true))
fmt.Printf("\tnix-amer has been entirely designed and developped by xdrm-brackets\n")

View File

@ -10,7 +10,7 @@ import (
func main() {
// Manage arguments
ctx, bf, err := GetArgs()
ctx, bf, dryRun, err := GetArgs()
if err != nil {
fmt.Printf("%s", err)
return
@ -25,13 +25,29 @@ func main() {
defer bfreader.Close()
// 2. parse buildfile
_, err = buildfile.NewReader(ctx, bfreader)
instructions, err := buildfile.NewReader(ctx, bfreader)
if err != nil {
fmt.Printf("%s%s\n", bf, err)
return
}
clifmt.Align("build file")
fmt.Printf("%s\n", clifmt.Color(32, "parsed"))
fmt.Printf("%s\n", clifmt.Color(32, "valid"))
// stop here if dry run
if dryRun {
return
}
// 3. Execute
clifmt.Align("execution")
fmt.Printf("start\n")
err = instructions.Execute()
if err != nil {
fmt.Printf("%s\n", clifmt.Warn(err.Error()))
return
}
clifmt.Align("execution")
fmt.Printf("%s\n", clifmt.Color(32, "finished"))
}

View File

@ -3,6 +3,7 @@ package buildfile
import (
"bufio"
"errors"
"fmt"
"git.xdrm.io/xdrm-brackets/nix-amer/internal/instruction"
"io"
"strings"
@ -63,3 +64,21 @@ func NewReader(ctx *instruction.ExecutionContext, buildfile io.Reader) (*Reader,
return r, nil
}
// Execute the current buildfile instruction by instruction
func (r *Reader) Execute() error {
// 1. update package list
err := r.Context.PackageManager.Fetch()
if err != nil {
return fmt.Errorf("cannot fetch packages | %s", err)
}
// 2. upgrade packages
err = r.Context.PackageManager.Upgrade()
if err != nil {
return fmt.Errorf("cannot upgrade | %s", err)
}
return nil
}

View File

@ -1,28 +0,0 @@
package instruction
import (
"fmt"
)
type update struct{}
func (d *update) Build(_args string) error {
return nil
}
func (d update) Exec(ctx ExecutionContext) ([]byte, error) {
// fetch packages
err := ctx.PackageManager.Fetch()
if err != nil {
return nil, fmt.Errorf("cannot fetch packages | %s", err)
}
// update packages
err = ctx.PackageManager.Upgrade()
if err != nil {
return nil, fmt.Errorf("cannot upgrade | %s", err)
}
return nil, nil
}