update cli UI/UX with elapsed time on success | add run command (issue with indentation)

This commit is contained in:
xdrm-brackets 2018-11-12 23:38:37 +01:00
parent 1095283be7
commit 8a08f429d2
9 changed files with 70 additions and 22 deletions

View File

@ -8,6 +8,7 @@ import (
"git.xdrm.io/go/nix-amer/internal/instruction" "git.xdrm.io/go/nix-amer/internal/instruction"
"io" "io"
"strings" "strings"
"time"
) )
// ErrNullContext is raised when the given context is nil // ErrNullContext is raised when the given context is nil
@ -84,19 +85,21 @@ func (r *Reader) Execute() error {
// 3. exec each instruction // 3. exec each instruction
for i, inst := range r.Content { for i, inst := range r.Content {
clifmt.Align(fmt.Sprintf("(%d) %s", i, clifmt.Color(34, inst.Raw()))) clifmt.Align(fmt.Sprintf("(%d) %s", i, clifmt.Color(0, inst.Raw())))
fmt.Printf("%s", clifmt.Color(33, "running")) fmt.Printf("%s", clifmt.Color(33, "processing"))
start := time.Now()
_, err := inst.Exec(*r.Context) _, err := inst.Exec(*r.Context)
if err != nil { if err != nil {
fmt.Printf("\r") fmt.Printf("\r")
clifmt.Align(fmt.Sprintf("(%d) %s", i, clifmt.Color(34, inst.Raw()))) clifmt.Align(fmt.Sprintf("(%d) %s", i, clifmt.Color(0, inst.Raw())))
fmt.Printf("%s \n", clifmt.Color(31, err.Error())) fmt.Printf("%s \n", clifmt.Color(31, err.Error()))
continue continue
} else { } else {
fmt.Printf("\r") fmt.Printf("\r")
clifmt.Align(fmt.Sprintf("(%d) %s", i, clifmt.Color(34, inst.Raw()))) clifmt.Align(fmt.Sprintf("(%d) %s", i, clifmt.Color(34, inst.Raw())))
fmt.Printf("%s \n", clifmt.Color(32, "done")) fmt.Printf("%ss \n", clifmt.Color(32, fmt.Sprintf("%.2f", time.Now().Sub(start).Seconds())))
} }
} }

View File

@ -17,21 +17,18 @@ type T interface {
Exec(ExecutionContext) ([]byte, error) Exec(ExecutionContext) ([]byte, error)
} }
// Executor is the common interface to execute commands
type Executor interface {
Execute(...string) error
}
// ExecutionContext contains system-specific drivers to manage the host // ExecutionContext contains system-specific drivers to manage the host
type ExecutionContext struct { type ExecutionContext struct {
ExecContext Executor
PackageManager pkg.PackageManager PackageManager pkg.PackageManager
ServiceManager ser.ServiceManager ServiceManager ser.ServiceManager
Executor exec.Executor
Alias map[string]string
} }
// CreateContext creates an execution contet with the given package-manager and service-manager // CreateContext creates an execution contet with the given package-manager and service-manager
// default values are taken from each go package (pkg, ser) // default values are taken from each go package (pkg, ser)
func CreateContext(_pkg, _ser string) (*ExecutionContext, error) { // if _exec is set, it will override the default (os) executor
func CreateContext(_pkg, _ser string, _exec ...exec.Executor) (*ExecutionContext, error) {
// 1. fail if no value and no defaults // 1. fail if no value and no defaults
if len(_pkg)+len(pkg.DefaultManager) < 1 { if len(_pkg)+len(pkg.DefaultManager) < 1 {
@ -49,9 +46,14 @@ func CreateContext(_pkg, _ser string) (*ExecutionContext, error) {
_ser = ser.DefaultManager _ser = ser.DefaultManager
} }
executor := new(exec.Default) var executor exec.Executor = new(exec.Default)
// 3. load managers // 3. load custom executor (optional)
if len(_exec) > 0 && _exec[0] != nil {
executor = _exec[0]
}
// 4. load managers
pkg, err := pkg.Load(_pkg, executor) pkg, err := pkg.Load(_pkg, executor)
if err != nil { if err != nil {
return nil, fmt.Errorf("package manager: %s", err) return nil, fmt.Errorf("package manager: %s", err)
@ -65,6 +67,8 @@ func CreateContext(_pkg, _ser string) (*ExecutionContext, error) {
return &ExecutionContext{ return &ExecutionContext{
PackageManager: pkg, PackageManager: pkg,
ServiceManager: ser, ServiceManager: ser,
Executor: executor,
Alias: make(map[string]string),
}, nil }, nil
} }

View File

@ -10,7 +10,7 @@ type delete struct {
Packages []string Packages []string
} }
func (d *delete) Raw() string { return d.raw } func (d *delete) Raw() string { return strings.Join([]string{"delete", d.raw}, " ") }
func (d *delete) Build(_args string) error { func (d *delete) Build(_args string) error {
d.Packages = strings.Split(_args, " ") d.Packages = strings.Split(_args, " ")

View File

@ -10,7 +10,7 @@ type install struct {
Packages []string Packages []string
} }
func (d *install) Raw() string { return d.raw } func (d *install) Raw() string { return strings.Join([]string{"install", d.raw}, " ") }
func (d *install) Build(_args string) error { func (d *install) Build(_args string) error {
d.Packages = strings.Split(_args, " ") d.Packages = strings.Split(_args, " ")

View File

@ -32,6 +32,10 @@ func Parse(raw string) (T, error) {
i := &service{} i := &service{}
err := i.Build(split[1]) err := i.Build(split[1])
return i, err return i, err
case "run":
i := &run{}
err := i.Build(split[1])
return i, err
case "set": case "set":
i := &set{} i := &set{}
err := i.Build(split[1]) err := i.Build(split[1])

View File

@ -0,0 +1,34 @@
package instruction
import (
"fmt"
"os"
"strings"
)
type run struct {
raw string
}
func (d *run) Raw() string { return strings.Join([]string{"run", d.raw}, " ") }
func (d *run) Build(_args string) error {
d.raw = _args
return nil
}
func (d run) Exec(ctx ExecutionContext) ([]byte, error) {
// 1. fail if file not found
if _, err := os.Stat(d.raw); os.IsNotExist(err) {
return nil, fmt.Errorf("cannot find script '%s'", d.raw)
}
// 2. execute script
if err := ctx.Executor.Command(d.raw).Run(); err != nil {
return nil, fmt.Errorf("cannot run '%s' | %s", d.raw, err)
}
return nil, nil
}

View File

@ -13,7 +13,7 @@ type service struct {
Services []string Services []string
} }
func (d *service) Raw() string { return d.raw } func (d *service) Raw() string { return strings.Join([]string{"service", d.raw}, " ") }
func (d *service) Build(_args string) error { func (d *service) Build(_args string) error {

View File

@ -23,7 +23,7 @@ type set struct {
Format *cnf.ConfigurationFormat Format *cnf.ConfigurationFormat
} }
func (d *set) Raw() string { return d.raw } func (d *set) Raw() string { return strings.Join([]string{"set", d.raw}, " ") }
func (d *set) Build(_args string) error { func (d *set) Build(_args string) error {

11
main.go
View File

@ -5,10 +5,13 @@ import (
"git.xdrm.io/go/nix-amer/internal/buildfile" "git.xdrm.io/go/nix-amer/internal/buildfile"
"git.xdrm.io/go/nix-amer/internal/clifmt" "git.xdrm.io/go/nix-amer/internal/clifmt"
"os" "os"
"time"
) )
func main() { func main() {
start := time.Now()
// Manage arguments // Manage arguments
ctx, bf, dryRun, err := GetArgs() ctx, bf, dryRun, err := GetArgs()
if err != nil { if err != nil {
@ -40,14 +43,14 @@ func main() {
} }
// 3. Execute // 3. Execute
clifmt.Align("execution") fmt.Printf("------\n")
fmt.Printf("start\n")
err = instructions.Execute() err = instructions.Execute()
if err != nil { if err != nil {
fmt.Printf("%s\n", clifmt.Warn(err.Error())) fmt.Printf("%s\n", clifmt.Warn(err.Error()))
return return
} }
clifmt.Align("execution") fmt.Printf("------\n")
fmt.Printf("%s\n", clifmt.Color(32, "finished")) clifmt.Align("finished in")
fmt.Printf("%ss\n", clifmt.Color(32, fmt.Sprintf("%.2f", time.Now().Sub(start).Seconds())))
} }