'cnf' working with INI files + loader + instruction executor now works
This commit is contained in:
parent
340fe26d81
commit
151ebce573
|
@ -70,28 +70,38 @@ func NewReader(ctx *instruction.ExecutionContext, buildfile io.Reader) (*Reader,
|
|||
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)
|
||||
}
|
||||
// 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)
|
||||
// // 2. upgrade packages
|
||||
// err = r.Context.PackageManager.Upgrade()
|
||||
// if err != nil {
|
||||
// return fmt.Errorf("cannot upgrade | %s", err)
|
||||
// }
|
||||
for i := 0; i < 30; i++ {
|
||||
fmt.Printf("+")
|
||||
}
|
||||
fmt.Printf("\n")
|
||||
|
||||
// 3. exec each instruction
|
||||
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)
|
||||
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()))
|
||||
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"))
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -9,6 +9,7 @@ import (
|
|||
)
|
||||
|
||||
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
|
||||
func Load(path string) (ConfigurationFormat, error) {
|
||||
|
@ -56,7 +57,7 @@ func Load(path string) (ConfigurationFormat, error) {
|
|||
// 4. Try to guess from the content
|
||||
confFormat = loadFromContent(file)
|
||||
if confFormat == nil {
|
||||
return nil, fmt.Errorf("cannot infer format from content")
|
||||
return nil, ErrUnknownFormat
|
||||
}
|
||||
|
||||
return confFormat, nil
|
||||
|
@ -66,9 +67,9 @@ func loadFromExtension(ext string) ConfigurationFormat {
|
|||
|
||||
// select configuration or fail if not known
|
||||
switch ext {
|
||||
case "json":
|
||||
case ".json":
|
||||
return new(Json)
|
||||
case "ini":
|
||||
case ".ini":
|
||||
return new(Ini)
|
||||
default:
|
||||
return nil
|
||||
|
|
|
@ -3,15 +3,18 @@ package instruction
|
|||
import (
|
||||
"fmt"
|
||||
"git.xdrm.io/go/nix-amer/internal/cnf"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var ErrImpossible = fmt.Errorf("cannot update configuration")
|
||||
|
||||
type config struct {
|
||||
raw string
|
||||
// File is the path to the file
|
||||
File string
|
||||
// Path is the configuration field path
|
||||
Path []string
|
||||
Path string
|
||||
// Value if the value to add or update
|
||||
Value string
|
||||
// Format is the configuration format in use
|
||||
|
@ -40,7 +43,7 @@ func (d *config) Build(_args string) error {
|
|||
|
||||
d.File = splitPath[0]
|
||||
if len(splitPath) > 1 { // add field path only if set
|
||||
d.Path = strings.Split(splitPath[1], ".")
|
||||
d.Path = splitPath[1]
|
||||
}
|
||||
|
||||
// add value
|
||||
|
@ -52,7 +55,26 @@ func (d *config) Build(_args string) 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
|
||||
|
||||
|
|
Loading…
Reference in New Issue