2018-11-10 11:37:31 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
2018-11-18 21:49:11 +00:00
|
|
|
"git.xdrm.io/go/nix-amer/internal/instruction"
|
2018-11-10 11:37:31 +00:00
|
|
|
)
|
|
|
|
|
2018-11-11 18:01:00 +00:00
|
|
|
// GetArgs manages cli arguments to build executionContext,
|
|
|
|
// extract the buildfile path and return whether to dry-run/execute
|
|
|
|
// + adds error information
|
2018-11-10 11:45:58 +00:00
|
|
|
func GetArgs() (*instruction.ExecutionContext, string, bool, error) {
|
2018-11-10 11:37:31 +00:00
|
|
|
setupFlags(flag.CommandLine)
|
|
|
|
|
|
|
|
longPack := flag.String("package", "", "")
|
2018-11-10 11:45:58 +00:00
|
|
|
dryRun := flag.Bool("dry-run", false, "")
|
2018-11-10 11:37:31 +00:00
|
|
|
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 {
|
2018-11-10 11:45:58 +00:00
|
|
|
return nil, "", false, fmt.Errorf("missing buildfile")
|
2018-11-10 11:37:31 +00:00
|
|
|
}
|
|
|
|
buildfile := flag.Arg(0)
|
|
|
|
|
|
|
|
// 2. override short version with long
|
2018-11-10 11:45:58 +00:00
|
|
|
if len(*longPack) > 0 {
|
2018-11-10 11:37:31 +00:00
|
|
|
pack = longPack
|
|
|
|
}
|
2018-11-10 11:45:58 +00:00
|
|
|
if len(*longServ) > 0 {
|
2018-11-10 11:37:31 +00:00
|
|
|
serv = longServ
|
|
|
|
}
|
|
|
|
|
|
|
|
// 3. fail on missing mandatory fields
|
2018-11-10 11:45:58 +00:00
|
|
|
if len(*pack) < 1 {
|
|
|
|
return nil, "", false, fmt.Errorf("missing -package")
|
2018-11-10 11:37:31 +00:00
|
|
|
}
|
|
|
|
// 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 {
|
2018-11-10 11:45:58 +00:00
|
|
|
return nil, "", false, err
|
2018-11-10 11:37:31 +00:00
|
|
|
}
|
|
|
|
/*DEBUG*/ //fmt.Printf("package: '%s' | '%s'\n", fpackage, lpackage)
|
|
|
|
/*DEBUG*/ //fmt.Printf("service: '%s' | '%s'\n", fservice, lservice)
|
|
|
|
|
2018-11-10 11:45:58 +00:00
|
|
|
return ctx, buildfile, *dryRun, nil
|
2018-11-10 11:37:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func setupFlags(f *flag.FlagSet) {
|
|
|
|
f.Usage = func() {
|
|
|
|
help()
|
|
|
|
}
|
|
|
|
}
|