func Parse()

in rule/flags/flags.go [38:89]


func Parse(s string) (rule.Rule, error) {
	args, err := shellquote.Split(s)
	if err != nil {
		return nil, err
	}

	// Parse the flags.
	ruleFlagSet := newRuleFlagSet()
	if err := ruleFlagSet.flagSet.Parse(args); err != nil {
		return nil, err
	}
	if err := ruleFlagSet.validate(); err != nil {
		return nil, err
	}

	// Build a struct that is specific to the command type.
	var r rule.Rule
	switch ruleFlagSet.Type {
	case rule.DeleteAllRuleType:
		r = &rule.DeleteAllRule{
			Type: rule.DeleteAllRuleType,
			Keys: ruleFlagSet.Key,
		}
	case rule.FileWatchRuleType:
		r = &rule.FileWatchRule{
			Type:        rule.FileWatchRuleType,
			Path:        ruleFlagSet.Path,
			Permissions: ruleFlagSet.Permissions,
			Keys:        ruleFlagSet.Key,
		}
	case rule.AppendSyscallRuleType, rule.PrependSyscallRuleType:
		syscallRule := &rule.SyscallRule{
			Type:     ruleFlagSet.Type,
			Filters:  ruleFlagSet.Filters,
			Syscalls: ruleFlagSet.Syscalls,
			Keys:     ruleFlagSet.Key,
		}
		r = syscallRule

		if ruleFlagSet.Type == rule.AppendSyscallRuleType {
			syscallRule.List = ruleFlagSet.Append.List
			syscallRule.Action = ruleFlagSet.Append.Action
		} else if ruleFlagSet.Type == rule.PrependSyscallRuleType {
			syscallRule.List = ruleFlagSet.Prepend.List
			syscallRule.Action = ruleFlagSet.Prepend.Action
		}
	default:
		return nil, fmt.Errorf("unknown rule type: %v", ruleFlagSet.Type)
	}

	return r, nil
}