'cnf' working with INI files + loader + instruction executor now works

This commit is contained in:
xdrm-brackets 2018-11-10 19:04:16 +01:00
parent 340fe26d81
commit 151ebce573
3 changed files with 50 additions and 17 deletions

View File

@ -70,28 +70,38 @@ func NewReader(ctx *instruction.ExecutionContext, buildfile io.Reader) (*Reader,
func (r *Reader) Execute() error { func (r *Reader) Execute() error {
// 1. update package list // 1. update package list
err := r.Context.PackageManager.Fetch() // err := r.Context.PackageManager.Fetch()
if err != nil { // if err != nil {
return fmt.Errorf("cannot fetch packages | %s", err) // return fmt.Errorf("cannot fetch packages | %s", err)
} // }
// 2. upgrade packages // // 2. upgrade packages
err = r.Context.PackageManager.Upgrade() // err = r.Context.PackageManager.Upgrade()
if err != nil { // if err != nil {
return fmt.Errorf("cannot upgrade | %s", err) // return fmt.Errorf("cannot upgrade | %s", err)
// }
for i := 0; i < 30; i++ {
fmt.Printf("+")
} }
fmt.Printf("\n")
// 3. exec each instruction // 3. exec each instruction
for i, inst := range r.Content { for i, inst := range r.Content {
clifmt.AlignLimited(fmt.Sprintf("(%d) %s", i, clifmt.Color(33, inst.Raw()))) clifmt.Align(fmt.Sprintf("(%d) %s", i, clifmt.Color(34, inst.Raw())))
fmt.Printf("%s", clifmt.Color(33, "running"))
_, err := inst.Exec(*r.Context) _, err := inst.Exec(*r.Context)
if err != nil { if err != nil {
fmt.Printf("\r")
clifmt.Align(fmt.Sprintf("\r(%d) %s", i, clifmt.Color(34, inst.Raw())))
fmt.Printf("%s \n", clifmt.Color(31, err.Error())) fmt.Printf("%s \n", clifmt.Color(31, err.Error()))
continue continue
} } else {
fmt.Printf("\r")
clifmt.Align(fmt.Sprintf("\r(%d) %s", i, clifmt.Color(34, inst.Raw())))
fmt.Printf("%s \n", clifmt.Color(32, "done")) fmt.Printf("%s \n", clifmt.Color(32, "done"))
} }
}
return nil return nil
} }

View File

@ -9,6 +9,7 @@ import (
) )
var ErrUnknownExtension = errors.New("unknown extension format") var ErrUnknownExtension = errors.New("unknown extension format")
var ErrUnknownFormat = errors.New("cannot infer format from content")
// Load the current file and create the configuration format accordingly // Load the current file and create the configuration format accordingly
func Load(path string) (ConfigurationFormat, error) { func Load(path string) (ConfigurationFormat, error) {
@ -56,7 +57,7 @@ func Load(path string) (ConfigurationFormat, error) {
// 4. Try to guess from the content // 4. Try to guess from the content
confFormat = loadFromContent(file) confFormat = loadFromContent(file)
if confFormat == nil { if confFormat == nil {
return nil, fmt.Errorf("cannot infer format from content") return nil, ErrUnknownFormat
} }
return confFormat, nil return confFormat, nil
@ -66,9 +67,9 @@ func loadFromExtension(ext string) ConfigurationFormat {
// select configuration or fail if not known // select configuration or fail if not known
switch ext { switch ext {
case "json": case ".json":
return new(Json) return new(Json)
case "ini": case ".ini":
return new(Ini) return new(Ini)
default: default:
return nil return nil

View File

@ -3,15 +3,18 @@ package instruction
import ( import (
"fmt" "fmt"
"git.xdrm.io/go/nix-amer/internal/cnf" "git.xdrm.io/go/nix-amer/internal/cnf"
"os"
"strings" "strings"
) )
var ErrImpossible = fmt.Errorf("cannot update configuration")
type config struct { type config struct {
raw string raw string
// File is the path to the file // File is the path to the file
File string File string
// Path is the configuration field path // Path is the configuration field path
Path []string Path string
// Value if the value to add or update // Value if the value to add or update
Value string Value string
// Format is the configuration format in use // Format is the configuration format in use
@ -40,7 +43,7 @@ func (d *config) Build(_args string) error {
d.File = splitPath[0] d.File = splitPath[0]
if len(splitPath) > 1 { // add field path only if set if len(splitPath) > 1 { // add field path only if set
d.Path = strings.Split(splitPath[1], ".") d.Path = splitPath[1]
} }
// add value // add value
@ -52,7 +55,26 @@ func (d *config) Build(_args string) error {
func (d config) Exec(ctx ExecutionContext) ([]byte, error) { func (d config) Exec(ctx ExecutionContext) ([]byte, error) {
fmt.Printf("path is '%v'\n", d.Path) // 1. try to load format
format, err := cnf.Load(d.File)
if err != nil {
return nil, err
}
// 2. try to update value
if !format.Set(d.Path, d.Value) {
return nil, ErrImpossible
}
// 3. Update file
file, err := os.OpenFile(d.File, os.O_WRONLY, 0)
if err != nil {
return nil, err
}
defer file.Close()
if err = format.Write(file); err != nil {
return nil, err
}
return nil, nil return nil, nil