nix-amer/internal/buildfile/instruction.go

64 lines
1.2 KiB
Go
Raw Normal View History

package buildfile
import (
"strings"
)
type instruction_type byte
const (
// INSTALL one or more package(s)
INSTALL instruction_type = iota
// UPDATE all installed packages candidates
UPDATE
// REMOVE one or more package(s)
REMOVE
// SERVICE management (enable, disable, start, stop, restart, reload)
SERVICE
// CONF edits configuration files
CONF
)
// instruction is self-explanatory
type instruction struct {
Type instruction_type
// Sub command instruction-type-specific
Sub []string
Args string
}
// createInstruction from a raw line
func createInstruction(raw string) (*instruction, error) {
// 1. fail if invalid base syntax 'cmd args...'
// where command is 3 letters
if len(raw) < 5 || len(strings.Trim(raw[0:3], " ")) < 3 || raw[1] == ' ' || raw[3] != ' ' {
return nil, ErrInvalidSyntax
}
// 2. prepare instruction
inst := &instruction{
Type: INSTALL,
Sub: make([]string, 0),
Args: raw[4:],
}
// 3. Extract instruction type
switch raw[0:3] {
case "ins":
inst.Type = INSTALL
case "upd":
inst.Type = UPDATE
case "del":
inst.Type = REMOVE
case "ser":
inst.Type = SERVICE
case "cnf":
inst.Type = CONF
default:
return nil, ErrUnknownInstruction
}
return inst, nil
}