nix-amer/args.go

60 lines
1.4 KiB
Go

package main
import (
"flag"
"fmt"
"git.xdrm.io/go/nix-amer/internal/instruction"
)
// GetArgs manages cli arguments to build executionContext,
// extract the buildfile path and return whether to dry-run/execute
// + adds error information
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", "", "")
flag.Parse()
// 1. fail on missing build file
if len(flag.Args()) < 1 {
return nil, "", false, fmt.Errorf("missing buildfile")
}
buildfile := flag.Arg(0)
// 2. override short version with long
if len(*longPack) > 0 {
pack = longPack
}
if len(*longServ) > 0 {
serv = longServ
}
// 3. fail on missing mandatory fields
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")
// }
// 3. Load context
ctx, err := instruction.CreateContext(*pack, *serv)
if err != nil {
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, *dryRun, nil
}
func setupFlags(f *flag.FlagSet) {
f.Usage = func() {
help()
}
}