From 464b2539d254d129c809228fb0a412fa27742b74 Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Sat, 10 Nov 2018 12:45:58 +0100 Subject: [PATCH] add dry-run cli argument | add buildfile.Reader.Execute() error --- cmd/nix-amer/args.go | 17 +++++++++-------- cmd/nix-amer/help.go | 4 +++- cmd/nix-amer/main.go | 22 +++++++++++++++++++--- internal/buildfile/reader.go | 19 +++++++++++++++++++ internal/instruction/upd.go | 28 ---------------------------- 5 files changed, 50 insertions(+), 40 deletions(-) delete mode 100644 internal/instruction/upd.go diff --git a/cmd/nix-amer/args.go b/cmd/nix-amer/args.go index 29b9533..30dbe23 100644 --- a/cmd/nix-amer/args.go +++ b/cmd/nix-amer/args.go @@ -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) { diff --git a/cmd/nix-amer/help.go b/cmd/nix-amer/help.go index abb11f1..f7b438b 100644 --- a/cmd/nix-amer/help.go +++ b/cmd/nix-amer/help.go @@ -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 ] [-s ] \n") + fmt.Printf("\tnix-amer [-p ] [-s ] [-dry-run] \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") diff --git a/cmd/nix-amer/main.go b/cmd/nix-amer/main.go index c99114d..5782286 100644 --- a/cmd/nix-amer/main.go +++ b/cmd/nix-amer/main.go @@ -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")) } diff --git a/internal/buildfile/reader.go b/internal/buildfile/reader.go index b994790..f113688 100644 --- a/internal/buildfile/reader.go +++ b/internal/buildfile/reader.go @@ -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 +} diff --git a/internal/instruction/upd.go b/internal/instruction/upd.go deleted file mode 100644 index 006aa2a..0000000 --- a/internal/instruction/upd.go +++ /dev/null @@ -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 -}